1use crate::{
2 Encoding, MappingStyle, Mark, ScalarStyle, SequenceStyle, TagDirective, VersionDirective,
3};
4
5#[derive(Debug, PartialEq)]
7#[non_exhaustive]
8pub struct Event {
9 pub data: EventData,
11 pub start_mark: Mark,
13 pub end_mark: Mark,
15}
16
17#[derive(Debug, PartialEq)]
18pub enum EventData {
19 StreamStart {
21 encoding: Encoding,
23 },
24 StreamEnd,
25 DocumentStart {
27 version_directive: Option<VersionDirective>,
29 tag_directives: Vec<TagDirective>,
31 implicit: bool,
33 },
34 DocumentEnd {
36 implicit: bool,
37 },
38 Alias {
40 anchor: String,
42 },
43 Scalar {
45 anchor: Option<String>,
47 tag: Option<String>,
49 value: String,
51 plain_implicit: bool,
53 quoted_implicit: bool,
55 style: ScalarStyle,
57 },
58 SequenceStart {
60 anchor: Option<String>,
62 tag: Option<String>,
64 implicit: bool,
66 style: SequenceStyle,
68 },
69 SequenceEnd,
70 MappingStart {
72 anchor: Option<String>,
74 tag: Option<String>,
76 implicit: bool,
78 style: MappingStyle,
80 },
81 MappingEnd,
82}
83
84impl Event {
85 pub(crate) fn new(data: EventData) -> Self {
87 Self {
88 data,
89 start_mark: Mark::default(),
90 end_mark: Mark::default(),
91 }
92 }
93
94 pub fn stream_start(encoding: Encoding) -> Self {
96 Self::new(EventData::StreamStart { encoding })
97 }
98
99 pub fn stream_end() -> Self {
101 Self::new(EventData::StreamEnd)
102 }
103
104 pub fn document_start(
109 version_directive: Option<VersionDirective>,
110 tag_directives_in: &[TagDirective],
111 implicit: bool,
112 ) -> Self {
113 let tag_directives = tag_directives_in.to_vec();
114
115 Self::new(EventData::DocumentStart {
116 version_directive,
117 tag_directives,
118 implicit,
119 })
120 }
121
122 pub fn document_end(implicit: bool) -> Self {
127 Self::new(EventData::DocumentEnd { implicit })
128 }
129
130 pub fn alias(anchor: &str) -> Self {
132 Self::new(EventData::Alias {
133 anchor: String::from(anchor),
134 })
135 }
136
137 pub fn scalar(
145 anchor: Option<&str>,
146 tag: Option<&str>,
147 value: &str,
148 plain_implicit: bool,
149 quoted_implicit: bool,
150 style: ScalarStyle,
151 ) -> Self {
152 let mut anchor_copy: Option<String> = None;
153 let mut tag_copy: Option<String> = None;
154
155 if let Some(anchor) = anchor {
156 anchor_copy = Some(String::from(anchor));
157 }
158 if let Some(tag) = tag {
159 tag_copy = Some(String::from(tag));
160 }
161
162 Self::new(EventData::Scalar {
163 anchor: anchor_copy,
164 tag: tag_copy,
165 value: String::from(value),
166 plain_implicit,
167 quoted_implicit,
168 style,
169 })
170 }
171
172 pub fn sequence_start(
178 anchor: Option<&str>,
179 tag: Option<&str>,
180 implicit: bool,
181 style: SequenceStyle,
182 ) -> Self {
183 let mut anchor_copy: Option<String> = None;
184 let mut tag_copy: Option<String> = None;
185
186 if let Some(anchor) = anchor {
187 anchor_copy = Some(String::from(anchor));
188 }
189 if let Some(tag) = tag {
190 tag_copy = Some(String::from(tag));
191 }
192
193 Self::new(EventData::SequenceStart {
194 anchor: anchor_copy,
195 tag: tag_copy,
196 implicit,
197 style,
198 })
199 }
200
201 pub fn sequence_end() -> Self {
203 Self::new(EventData::SequenceEnd)
204 }
205
206 pub fn mapping_start(
212 anchor: Option<&str>,
213 tag: Option<&str>,
214 implicit: bool,
215 style: MappingStyle,
216 ) -> Self {
217 let mut anchor_copy: Option<String> = None;
218 let mut tag_copy: Option<String> = None;
219
220 if let Some(anchor) = anchor {
221 anchor_copy = Some(String::from(anchor));
222 }
223
224 if let Some(tag) = tag {
225 tag_copy = Some(String::from(tag));
226 }
227
228 Self::new(EventData::MappingStart {
229 anchor: anchor_copy,
230 tag: tag_copy,
231 implicit,
232 style,
233 })
234 }
235
236 pub fn mapping_end() -> Self {
238 Self::new(EventData::MappingEnd)
239 }
240}