yam_core/treebuild/
mod.rs

1use alloc::borrow::Cow;
2use alloc::fmt::Display;
3use alloc::vec::Vec;
4
5use crate::tokenizer::ErrorType;
6use crate::treebuild::YamlToken::Scalar;
7
8pub enum YamlToken<'a, TAG = ()> {
9    // strings, booleans, numbers, nulls, all treated the same
10    Scalar(Cow<'a, [u8]>, TAG),
11
12    // flow style like `[x, x, x]`
13    // or block style like:
14    //     - x
15    //     - x
16    Sequence(Vec<YamlToken<'a, TAG>>, TAG),
17
18    // flow style like `{x: Y, a: B}`
19    // or block style like:
20    //     x: Y
21    //     a: B
22    Mapping(Vec<Entry<'a, TAG>>, TAG),
23}
24
25impl<'a, TAG> Display for YamlToken<'a, TAG> {
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        match self {
28            Scalar(val, _) => write!(f, "SCAL: {val:?}"),
29            Self::Sequence(val, _) => {
30                write!(f, "SEQ:")?;
31                for el in val {
32                    write!(f, "{el}")?;
33                }
34                Ok(())
35            }
36            Self::Mapping(val, _) => {
37                write!(f, "MAP:")?;
38                for entry in val {
39                    write!(f, "{} = {}", entry.key, entry.value)?;
40                }
41                Ok(())
42            }
43        }
44    }
45}
46
47impl<'a, TAG: Default> Default for YamlToken<'a, TAG> {
48    fn default() -> Self {
49        Scalar(Cow::default(), TAG::default())
50    }
51}
52
53pub struct Entry<'a, TAG> {
54    key: YamlToken<'a, TAG>,
55    value: YamlToken<'a, TAG>,
56}
57
58impl<'a, TAG: Default> Default for Entry<'a, TAG> {
59    fn default() -> Self {
60        Entry {
61            key: YamlToken::default(),
62            value: YamlToken::default(),
63        }
64    }
65}
66
67pub struct YamlTokenError<'a, T> {
68    _partial: YamlToken<'a, T>,
69    _error: Vec<ErrorType>,
70}