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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! The default loader.
use std::{borrow::Cow, collections::BTreeMap, marker::PhantomData, sync::Arc};
use hashlink::LinkedHashMap;
use saphyr_parser::{
BufferedInput, Event, Input, Marker, Parser, ScanError, Span, SpannedEventReceiver, Tag,
};
use crate::{Mapping, Yaml};
/// Main structure for parsing YAML.
///
/// The `YamlLoader` may load raw YAML documents or add metadata if needed. The type of the `Node`
/// dictates what data and metadata the loader will add to the `Node`.
///
/// Each node must implement [`LoadableYamlNode`]. The methods are required for the loader to
/// manipulate and populate the `Node`.
#[allow(clippy::module_name_repetitions)]
pub struct YamlLoader<'input, Node>
where
Node: LoadableYamlNode<'input>,
{
/// The different YAML documents that are loaded.
docs: Vec<Node>,
// states
// (current node, anchor_id, tag) tuple
doc_stack: Vec<(Node, usize, Option<Cow<'input, Tag>>)>,
key_stack: Vec<Node>,
anchor_map: BTreeMap<usize, Node>,
marker: PhantomData<&'input u32>,
/// See [`Self::early_parse()`]
early_parse: bool,
}
/// A trait providing methods used by the [`YamlLoader`].
///
/// This trait must be implemented on YAML node types (i.e.: [`Yaml`] and annotated YAML nodes). It
/// provides the necessary methods for [`YamlLoader`] to load data into the node.
pub trait LoadableYamlNode<'input>: Clone + std::hash::Hash + Eq {
/// The type of the key for the hash variant of the YAML node.
///
/// The `HashKey` must be [`Eq`] and [`Hash`] to satisfy the hash map requirements.
/// It must also be [`Borrow<Self>`] so the hash map can borrow the key to a node and compare
/// it with a node.
/// Furthermore, it must be [`From<Self>`] so we can create a key from a node.
/// Finally, if indexing mappings with `&str` is desired, it must also implement
/// [`PartialEq<Self>`].
/// These constraints are also highlighted in [`AnnotatedNode`].
///
/// This indirection is required to solve lifetime issues with the hash map in annotated YAMLs.
/// More details about the issue and possible workarounds can be found
/// [here](https://github.com/rust-lang/rust/issues/124614#issuecomment-2090725842). A previous
/// attempt at solving lifetimes used capsules, but [`AnnotatedNode`] is sufficient.
///
/// [`Hash`]: std::hash::Hash
/// [`Borrow<Self>`]: std::borrow::Borrow
/// [`From<Self>`]: From
/// [`PartialEq<Self>`]: PartialEq
/// [`AnnotatedNode`]: crate::annotated::AnnotatedNode
type HashKey: Eq + std::hash::Hash + std::borrow::Borrow<Self> + From<Self>;
/// Create an instance of `Self` from a [`Yaml`].
///
/// Nodes must implement this to be built. The optional metadata that they contain will be
/// later provided by the loader and can be default initialized. The [`Yaml`] object passed as
/// parameter may be of the [`Sequence`] or [`Mapping`] variants. In this event, the inner
/// container will always be empty. There is no need to traverse all elements to convert them
/// from [`Yaml`] to `Self`.
///
/// [`Sequence`]: `Yaml::Sequence`
/// [`Mapping`]: `Yaml::Mapping`
fn from_bare_yaml(yaml: Yaml<'input>) -> Self;
/// Return whether the YAML node is an array.
///
/// If the YAML node is a tagged variant, this function must inspect the underlying node.
fn is_sequence(&self) -> bool;
/// Return whether the YAML node is a hash.
///
/// If the YAML node is a tagged variant, this function must inspect the underlying node.
fn is_mapping(&self) -> bool;
/// Return whether the YAML node is `BadValue`.
fn is_badvalue(&self) -> bool;
/// Retrieve the sequence variant of the YAML node.
///
/// If the YAML node is a tagged variant, this function must inspect the underlying node.
///
/// # Panics
/// This function panics if `self` is not a sequence.
fn sequence_mut(&mut self) -> &mut Vec<Self>;
/// Retrieve the mapping variant of the YAML node.
///
/// If the YAML node is a tagged variant, this function must inspect the underlying node.
///
/// # Panics
/// This function panics if `self` is not a mapping.
fn mapping_mut(&mut self) -> &mut LinkedHashMap<Self::HashKey, Self>;
/// Turn `self` into a `Tagged` node, using the given tag.
///
/// # Return
/// Returns a new instance of `Self` of `Tagged` variant with `tag` as the tag and `self` as
/// the value.
#[must_use]
fn into_tagged(self, tag: Cow<'input, Tag>) -> Self;
/// Take the contained node out of `Self`, leaving a `BadValue` in its place.
#[must_use]
fn take(&mut self) -> Self;
/// Provide the span for the node (builder-style).
///
/// Either [`with_span`] is used (typically for scalars) or both [`with_start_marker`] and
/// [`with_end_marker`] are used (typically for collections).
///
/// [`with_span`]: `LoadableYamlNode::with_span`
/// [`with_start_marker`]: `LoadableYamlNode::with_start_marker`
/// [`with_end_marker`]: `LoadableYamlNode::with_end_marker`
#[inline]
#[must_use]
fn with_span(self, _: Span) -> Self {
self
}
/// Provide the start-marker for the node (builder-style).
///
/// If this method is used by the loader, a call to [`with_end_marker`] will follow later.
///
/// [`with_end_marker`]: `LoadableYamlNode::with_end_marker`
#[inline]
#[must_use]
fn with_start_marker(self, _: Marker) -> Self {
self
}
/// Provide the end-marker for the node (builder-style).
///
/// This method is called after a call to [`with_start_marker`].
///
/// [`with_start_marker`]: `LoadableYamlNode::with_start_marker`
#[inline]
#[must_use]
fn with_end_marker(self, _: Marker) -> Self {
self
}
/// Load the given string as an array of YAML documents.
///
/// The `source` is interpreted as YAML documents and is parsed. Parsing succeeds if and only
/// if all documents are parsed successfully. An error in a latter document prevents the former
/// from being returned.
///
/// Most often, only one document is loaded in a YAML string. In this case, only the first element
/// of the returned `Vec` will be used. Otherwise, each element in the `Vec` is a document:
///
/// ```
/// use saphyr::{LoadableYamlNode, Scalar, Yaml};
///
/// let docs = Yaml::load_from_str(r#"
/// First document
/// ---
/// - Second document
/// "#).unwrap();
/// let first_document = &docs[0]; // Select the first YAML document
/// // The document is a string containing "First document".
/// assert_eq!(*first_document, Yaml::Value(Scalar::String("First document".into())));
///
/// let second_document = &docs[1]; // Select the second YAML document
/// // The document is an array containing a single string, "Second document".
/// assert_eq!(second_document[0], Yaml::Value(Scalar::String("Second document".into())));
/// ```
///
/// # Errors
/// Returns [`ScanError`] when loading fails.
fn load_from_str(source: &str) -> Result<Vec<Self>, ScanError> {
Self::load_from_iter(source.chars())
}
/// Load the contents of the given iterator as an array of YAML documents.
///
/// See [`load_from_str`] for details.
///
/// # Errors
/// Returns [`ScanError`] when loading fails.
///
/// [`load_from_str`]: LoadableYamlNode::load_from_str
fn load_from_iter<I: Iterator<Item = char>>(source: I) -> Result<Vec<Self>, ScanError> {
let mut parser = Parser::new(BufferedInput::new(source));
Self::load_from_parser(&mut parser)
}
/// Load the contents from the specified [`Parser`] as an array of YAML documents.
///
/// See [`load_from_str`] for details.
///
/// # Errors
/// Returns [`ScanError`] when loading fails.
///
/// [`load_from_str`]: LoadableYamlNode::load_from_str
fn load_from_parser<I: Input>(parser: &mut Parser<'input, I>) -> Result<Vec<Self>, ScanError> {
let mut loader = YamlLoader::default();
parser.load(&mut loader, true)?;
Ok(loader.into_documents())
}
}
impl<'input, Node> YamlLoader<'input, Node>
where
Node: LoadableYamlNode<'input>,
{
/// Whether to parse scalars into their value while loading a YAML.
///
/// If set to `true` (default), the loader will attempt to parse scalars into [`Scalar`]s. The
/// loaded [`Yaml`] nodes will use the [`Value`] variant.
/// If set to `false`, the loader will skip scalar parsing and only store the string
/// representation in [`Representation`].
///
/// [`Value`]: Yaml::Value
/// [`Representation`]: Yaml::Representation
/// [`Scalar`]: crate::Scalar
pub fn early_parse(&mut self, enabled: bool) {
self.early_parse = enabled;
}
/// Return the document nodes from `self`, consuming it in the process.
#[must_use]
pub fn into_documents(self) -> Vec<Node> {
self.docs
}
}
impl<'input, Node> SpannedEventReceiver<'input> for YamlLoader<'input, Node>
where
Node: LoadableYamlNode<'input>,
{
fn on_event(&mut self, ev: Event<'input>, span: Span) {
let mark = span.start;
match ev {
Event::DocumentStart(_) | Event::Nothing | Event::StreamStart | Event::StreamEnd => {
// do nothing
}
Event::DocumentEnd => {
match self.doc_stack.len() {
// empty document
0 => self
.docs
.push(Node::from_bare_yaml(Yaml::BadValue).with_span(span)),
1 => self.docs.push(self.doc_stack.pop().unwrap().0),
_ => unreachable!(),
}
}
Event::SequenceStart(aid, tag) => {
self.doc_stack.push((
Node::from_bare_yaml(Yaml::Sequence(Vec::new())).with_start_marker(mark),
aid,
tag,
));
}
Event::MappingStart(aid, tag) => {
self.doc_stack.push((
Node::from_bare_yaml(Yaml::Mapping(Mapping::new())).with_start_marker(mark),
aid,
tag,
));
self.key_stack.push(Node::from_bare_yaml(Yaml::BadValue));
}
Event::MappingEnd | Event::SequenceEnd => {
if ev == Event::MappingEnd {
self.key_stack.pop().unwrap();
}
let (mut node, anchor_id, tag) = self.doc_stack.pop().unwrap();
node = node.with_end_marker(mark);
if let Some(tag) = tag {
if !tag.is_yaml_core_schema() {
node = node.into_tagged(tag);
}
}
self.insert_new_node(node, anchor_id, None);
}
Event::Scalar(v, style, aid, tag) => {
let node = if self.early_parse {
Yaml::value_from_cow_and_metadata(v, style, tag.as_ref())
} else {
Yaml::Representation(v, style, tag.clone())
};
self.insert_new_node(Node::from_bare_yaml(node).with_span(span), aid, tag);
}
Event::Alias(id) => {
let n = match self.anchor_map.get(&id) {
Some(v) => v.clone(),
None => Node::from_bare_yaml(Yaml::BadValue),
};
self.insert_new_node(n.with_span(span), 0, None);
}
}
}
}
impl<'input, Node> YamlLoader<'input, Node>
where
Node: LoadableYamlNode<'input>,
{
fn insert_new_node(&mut self, mut node: Node, anchor_id: usize, tag: Option<Cow<'input, Tag>>) {
// valid anchor id starts from 1
if anchor_id > 0 {
self.anchor_map.insert(anchor_id, node.clone());
}
if let Some((parent_node, _, _)) = self.doc_stack.last_mut() {
if let Some(tag) = tag {
if (node.is_sequence() || node.is_mapping()) && !tag.is_yaml_core_schema() {
node = node.into_tagged(tag);
}
}
if parent_node.is_sequence() {
parent_node.sequence_mut().push(node);
} else if parent_node.is_mapping() {
let cur_key = self.key_stack.last_mut().unwrap();
if cur_key.is_badvalue() {
// current node is a key
*cur_key = node;
} else {
// current node is a value
let hash = parent_node.mapping_mut();
hash.insert(cur_key.take().into(), node);
}
}
} else {
self.doc_stack.push((node, anchor_id, tag));
}
}
}
// For some reason, rustc wants `Node: Default` if I `#[derive(Default)]`.
impl<'input, Node> Default for YamlLoader<'input, Node>
where
Node: LoadableYamlNode<'input>,
{
fn default() -> Self {
Self {
docs: vec![],
doc_stack: vec![],
key_stack: vec![],
anchor_map: BTreeMap::new(),
marker: PhantomData,
early_parse: true,
}
}
}
/// An error that happened when loading a YAML document.
#[derive(Debug, Clone)]
pub enum LoadError {
/// An I/O error.
IO(Arc<std::io::Error>),
/// An error within the scanner. This indicates a malformed YAML input.
Scan(ScanError),
/// A decoding error (e.g.: Invalid UTF-8).
Decode(std::borrow::Cow<'static, str>),
}
impl From<std::io::Error> for LoadError {
fn from(error: std::io::Error) -> Self {
LoadError::IO(Arc::new(error))
}
}
impl std::error::Error for LoadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(match &self {
LoadError::IO(e) => e,
LoadError::Scan(e) => e,
LoadError::Decode(_) => return None,
})
}
}
impl std::fmt::Display for LoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LoadError::IO(e) => e.fmt(f),
LoadError::Scan(e) => e.fmt(f),
LoadError::Decode(e) => e.fmt(f),
}
}
}