1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use crate::{
Encoding, MappingStyle, Mark, ScalarStyle, SequenceStyle, TagDirective, VersionDirective,
};
/// The event structure.
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub struct Event {
/// The event data.
pub data: EventData,
/// The beginning of the event.
pub start_mark: Mark,
/// The end of the event.
pub end_mark: Mark,
}
#[derive(Debug, PartialEq)]
pub enum EventData {
/// The stream parameters (for `YAML_STREAM_START_EVENT`).
StreamStart {
/// The document encoding.
encoding: Encoding,
},
StreamEnd,
/// The document parameters (for `YAML_DOCUMENT_START_EVENT`).
DocumentStart {
/// The version directive.
version_directive: Option<VersionDirective>,
/// The tag directives list.
tag_directives: Vec<TagDirective>,
/// Is the document indicator implicit?
implicit: bool,
},
/// The document end parameters (for `YAML_DOCUMENT_END_EVENT`).
DocumentEnd {
implicit: bool,
},
/// The alias parameters (for `YAML_ALIAS_EVENT`).
Alias {
/// The anchor.
anchor: String,
},
/// The scalar parameters (for `YAML_SCALAR_EVENT`).
Scalar {
/// The anchor.
anchor: Option<String>,
/// The tag.
tag: Option<String>,
/// The scalar value.
value: String,
/// Is the tag optional for the plain style?
plain_implicit: bool,
/// Is the tag optional for any non-plain style?
quoted_implicit: bool,
/// The scalar style.
style: ScalarStyle,
},
/// The sequence parameters (for `YAML_SEQUENCE_START_EVENT`).
SequenceStart {
/// The anchor.
anchor: Option<String>,
/// The tag.
tag: Option<String>,
/// Is the tag optional?
implicit: bool,
/// The sequence style.
style: SequenceStyle,
},
SequenceEnd,
/// The mapping parameters (for `YAML_MAPPING_START_EVENT`).
MappingStart {
/// The anchor.
anchor: Option<String>,
/// The tag.
tag: Option<String>,
/// Is the tag optional?
implicit: bool,
/// The mapping style.
style: MappingStyle,
},
MappingEnd,
}
impl Event {
/// Make an event from its data, setting both marks to zero.
pub(crate) fn new(data: EventData) -> Self {
Self {
data,
start_mark: Mark::default(),
end_mark: Mark::default(),
}
}
/// Create the STREAM-START event.
pub fn stream_start(encoding: Encoding) -> Self {
Self::new(EventData::StreamStart { encoding })
}
/// Create the STREAM-END event.
pub fn stream_end() -> Self {
Self::new(EventData::StreamEnd)
}
/// Create the DOCUMENT-START event.
///
/// The `implicit` argument is considered as a stylistic parameter and may be
/// ignored by the emitter.
pub fn document_start(
version_directive: Option<VersionDirective>,
tag_directives_in: &[TagDirective],
implicit: bool,
) -> Self {
let tag_directives = tag_directives_in.to_vec();
Self::new(EventData::DocumentStart {
version_directive,
tag_directives,
implicit,
})
}
/// Create the DOCUMENT-END event.
///
/// The `implicit` argument is considered as a stylistic parameter and may be
/// ignored by the emitter.
pub fn document_end(implicit: bool) -> Self {
Self::new(EventData::DocumentEnd { implicit })
}
/// Create an ALIAS event.
pub fn alias(anchor: &str) -> Self {
Self::new(EventData::Alias {
anchor: String::from(anchor),
})
}
/// Create a SCALAR event.
///
/// The `style` argument may be ignored by the emitter.
///
/// Either the `tag` attribute or one of the `plain_implicit` and
/// `quoted_implicit` flags must be set.
///
pub fn scalar(
anchor: Option<&str>,
tag: Option<&str>,
value: &str,
plain_implicit: bool,
quoted_implicit: bool,
style: ScalarStyle,
) -> Self {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
if let Some(anchor) = anchor {
anchor_copy = Some(String::from(anchor));
}
if let Some(tag) = tag {
tag_copy = Some(String::from(tag));
}
Self::new(EventData::Scalar {
anchor: anchor_copy,
tag: tag_copy,
value: String::from(value),
plain_implicit,
quoted_implicit,
style,
})
}
/// Create a SEQUENCE-START event.
///
/// The `style` argument may be ignored by the emitter.
///
/// Either the `tag` attribute or the `implicit` flag must be set.
pub fn sequence_start(
anchor: Option<&str>,
tag: Option<&str>,
implicit: bool,
style: SequenceStyle,
) -> Self {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
if let Some(anchor) = anchor {
anchor_copy = Some(String::from(anchor));
}
if let Some(tag) = tag {
tag_copy = Some(String::from(tag));
}
Self::new(EventData::SequenceStart {
anchor: anchor_copy,
tag: tag_copy,
implicit,
style,
})
}
/// Create a SEQUENCE-END event.
pub fn sequence_end() -> Self {
Self::new(EventData::SequenceEnd)
}
/// Create a MAPPING-START event.
///
/// The `style` argument may be ignored by the emitter.
///
/// Either the `tag` attribute or the `implicit` flag must be set.
pub fn mapping_start(
anchor: Option<&str>,
tag: Option<&str>,
implicit: bool,
style: MappingStyle,
) -> Self {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
if let Some(anchor) = anchor {
anchor_copy = Some(String::from(anchor));
}
if let Some(tag) = tag {
tag_copy = Some(String::from(tag));
}
Self::new(EventData::MappingStart {
anchor: anchor_copy,
tag: tag_copy,
implicit,
style,
})
}
/// Create a MAPPING-END event.
pub fn mapping_end() -> Self {
Self::new(EventData::MappingEnd)
}
}