libyaml_safer/
event.rs

1use crate::{
2    Encoding, MappingStyle, Mark, ScalarStyle, SequenceStyle, TagDirective, VersionDirective,
3};
4
5/// The event structure.
6#[derive(Debug, PartialEq)]
7#[non_exhaustive]
8pub struct Event {
9    /// The event data.
10    pub data: EventData,
11    /// The beginning of the event.
12    pub start_mark: Mark,
13    /// The end of the event.
14    pub end_mark: Mark,
15}
16
17#[derive(Debug, PartialEq)]
18pub enum EventData {
19    /// The stream parameters (for `YAML_STREAM_START_EVENT`).
20    StreamStart {
21        /// The document encoding.
22        encoding: Encoding,
23    },
24    StreamEnd,
25    /// The document parameters (for `YAML_DOCUMENT_START_EVENT`).
26    DocumentStart {
27        /// The version directive.
28        version_directive: Option<VersionDirective>,
29        /// The tag directives list.
30        tag_directives: Vec<TagDirective>,
31        /// Is the document indicator implicit?
32        implicit: bool,
33    },
34    /// The document end parameters (for `YAML_DOCUMENT_END_EVENT`).
35    DocumentEnd {
36        implicit: bool,
37    },
38    /// The alias parameters (for `YAML_ALIAS_EVENT`).
39    Alias {
40        /// The anchor.
41        anchor: String,
42    },
43    /// The scalar parameters (for `YAML_SCALAR_EVENT`).
44    Scalar {
45        /// The anchor.
46        anchor: Option<String>,
47        /// The tag.
48        tag: Option<String>,
49        /// The scalar value.
50        value: String,
51        /// Is the tag optional for the plain style?
52        plain_implicit: bool,
53        /// Is the tag optional for any non-plain style?
54        quoted_implicit: bool,
55        /// The scalar style.
56        style: ScalarStyle,
57    },
58    /// The sequence parameters (for `YAML_SEQUENCE_START_EVENT`).
59    SequenceStart {
60        /// The anchor.
61        anchor: Option<String>,
62        /// The tag.
63        tag: Option<String>,
64        /// Is the tag optional?
65        implicit: bool,
66        /// The sequence style.
67        style: SequenceStyle,
68    },
69    SequenceEnd,
70    /// The mapping parameters (for `YAML_MAPPING_START_EVENT`).
71    MappingStart {
72        /// The anchor.
73        anchor: Option<String>,
74        /// The tag.
75        tag: Option<String>,
76        /// Is the tag optional?
77        implicit: bool,
78        /// The mapping style.
79        style: MappingStyle,
80    },
81    MappingEnd,
82}
83
84impl Event {
85    /// Make an event from its data, setting both marks to zero.
86    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    /// Create the STREAM-START event.
95    pub fn stream_start(encoding: Encoding) -> Self {
96        Self::new(EventData::StreamStart { encoding })
97    }
98
99    /// Create the STREAM-END event.
100    pub fn stream_end() -> Self {
101        Self::new(EventData::StreamEnd)
102    }
103
104    /// Create the DOCUMENT-START event.
105    ///
106    /// The `implicit` argument is considered as a stylistic parameter and may be
107    /// ignored by the emitter.
108    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    /// Create the DOCUMENT-END event.
123    ///
124    /// The `implicit` argument is considered as a stylistic parameter and may be
125    /// ignored by the emitter.
126    pub fn document_end(implicit: bool) -> Self {
127        Self::new(EventData::DocumentEnd { implicit })
128    }
129
130    /// Create an ALIAS event.
131    pub fn alias(anchor: &str) -> Self {
132        Self::new(EventData::Alias {
133            anchor: String::from(anchor),
134        })
135    }
136
137    /// Create a SCALAR event.
138    ///
139    /// The `style` argument may be ignored by the emitter.
140    ///
141    /// Either the `tag` attribute or one of the `plain_implicit` and
142    /// `quoted_implicit` flags must be set.
143    ///
144    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    /// Create a SEQUENCE-START event.
173    ///
174    /// The `style` argument may be ignored by the emitter.
175    ///
176    /// Either the `tag` attribute or the `implicit` flag must be set.
177    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    /// Create a SEQUENCE-END event.
202    pub fn sequence_end() -> Self {
203        Self::new(EventData::SequenceEnd)
204    }
205
206    /// Create a MAPPING-START event.
207    ///
208    /// The `style` argument may be ignored by the emitter.
209    ///
210    /// Either the `tag` attribute or the `implicit` flag must be set.
211    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    /// Create a MAPPING-END event.
237    pub fn mapping_end() -> Self {
238        Self::new(EventData::MappingEnd)
239    }
240}