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
use crate::{repr::*, *};
use alloc::{
    string::{String, ToString},
    vec::Vec,
};
use core::{
    fmt::{Debug, Formatter},
    hash::{Hash, Hasher},
    iter::FromIterator,
};
use ritelinked::LinkedHashMap;

macro_rules! impl_from {
    ($(impl $($from_ty:ty),+ => $ty:ident)+) => {
        $($(impl<R: Repr> From<$from_ty> for Yaml<R> {
            fn from(s: $from_ty) -> Self {
                Self::$ty(s.to_string())
            }
        })+)+
    };
}

macro_rules! impl_iter {
    ($(impl $($item:ty),+ => $ty:ident)+) => {
        $($(impl<R: Repr> FromIterator<$item> for Yaml<R> {
            fn from_iter<T: IntoIterator<Item = $item>>(iter: T) -> Self {
                Self::$ty(iter.into_iter().collect())
            }
        })+)+
    };
}

/// A YAML data with [`alloc::rc::Rc`] holder.
pub type YamlRc = Yaml<RcRepr>;
/// A YAML data with [`alloc::sync::Arc`] holder.
pub type YamlArc = Yaml<ArcRepr>;
/// The sequence data structure of YAML.
pub type Seq<R> = Vec<Node<R>>;
/// The map data structure of YAML.
pub type Map<R> = LinkedHashMap<Node<R>, Node<R>>;

pub(crate) fn to_i64(s: &str) -> Result<i64, core::num::ParseIntError> {
    if s.contains("0x") {
        i64::from_str_radix(&s.replace("0x", ""), 16)
    } else if s.contains("0o") {
        i64::from_str_radix(&s.replace("0o", ""), 8)
    } else {
        s.parse()
    }
}

pub(crate) fn to_f64(s: &str) -> Result<f64, core::num::ParseFloatError> {
    s.parse()
}

/// YAML data types, but it is recommended to use [`Node`] for shorten code.
///
/// This type can convert from primitive types by `From` and `Into` traits.
///
/// ```
/// use yaml_peg::YamlRc;
///
/// assert_eq!(YamlRc::Int("20".to_string()), YamlRc::from(20));
/// assert_eq!(YamlRc::Float("0.001".to_string()), 1e-3.into());
/// ```
///
/// Also, the iterators can turned to sequence and map.
///
/// ```
/// use std::iter::FromIterator;
/// use yaml_peg::{node, YamlRc};
///
/// let v = vec![node!(1), node!(2), node!(3)];
/// assert_eq!(YamlRc::Seq(v.clone()), YamlRc::from_iter(v));
/// let m = vec![(node!(1), node!(2)), (node!(3), node!(4))];
/// assert_eq!(
///     YamlRc::Map(m.clone().into_iter().collect()),
///     YamlRc::from_iter(m)
/// );
/// ```
///
/// The digit NaN (not-a-number) will be equal in the comparison.
pub enum Yaml<R: Repr> {
    /// Null
    Null,
    /// Boolean
    Bool(bool),
    /// Integer
    Int(String),
    /// Float
    Float(String),
    /// String
    Str(String),
    /// Sequence
    Seq(Seq<R>),
    /// Map
    Map(Map<R>),
    /// Alias (anchor insertion)
    Alias(String),
}

impl<R: Repr> Debug for Yaml<R> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Null => f.write_str("Null"),
            Self::Bool(b) => f.debug_tuple("Bool").field(b).finish(),
            Self::Int(s) => f.debug_tuple("Int").field(s).finish(),
            Self::Float(s) => f.debug_tuple("Float").field(s).finish(),
            Self::Str(s) => f.debug_tuple("Str").field(s).finish(),
            Self::Seq(s) => f.debug_tuple("Seq").field(s).finish(),
            Self::Map(m) => f.debug_tuple("Map").field(m).finish(),
            Self::Alias(a) => f.debug_tuple("Alias").field(a).finish(),
        }
    }
}

impl<R: Repr> Clone for Yaml<R> {
    fn clone(&self) -> Self {
        match self {
            Self::Null => Self::Null,
            Self::Bool(b) => Self::Bool(*b),
            Self::Int(s) => Self::Int(s.clone()),
            Self::Float(s) => Self::Float(s.clone()),
            Self::Str(s) => Self::Str(s.clone()),
            Self::Seq(s) => Self::Seq(s.clone()),
            Self::Map(m) => Self::Map(m.clone()),
            Self::Alias(a) => Self::Alias(a.clone()),
        }
    }
}

impl<R: Repr> Hash for Yaml<R> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Self::Null => state.write_u8(1),
            Self::Bool(b) => {
                state.write_u8(2);
                b.hash(state)
            }
            Self::Int(s) => {
                state.write_u8(3);
                s.hash(state)
            }
            Self::Float(s) => {
                state.write_u8(4);
                s.hash(state)
            }
            Self::Str(s) => {
                state.write_u8(5);
                s.hash(state)
            }
            Self::Seq(s) => {
                state.write_u8(6);
                s.hash(state)
            }
            Self::Map(m) => {
                state.write_u8(7);
                m.hash(state)
            }
            Self::Alias(a) => {
                state.write_u8(8);
                a.hash(state)
            }
        }
    }
}

impl<R: Repr> PartialEq for Yaml<R> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Null, Self::Null) => true,
            (Self::Bool(b1), Self::Bool(b2)) => b1 == b2,
            (Self::Int(s1), Self::Int(s2)) => to_i64(s1).unwrap() == to_i64(s2).unwrap(),
            (Self::Float(s1), Self::Float(s2)) => {
                let f1 = to_f64(s1).unwrap();
                let f2 = to_f64(s2).unwrap();
                if f1.is_nan() && f2.is_nan() {
                    true
                } else {
                    f1 == f2
                }
            }
            (Self::Str(s1), Self::Str(s2)) => s1 == s2,
            (Self::Seq(s1), Self::Seq(s2)) => s1 == s2,
            (Self::Map(m1), Self::Map(m2)) => m1 == m2,
            (Self::Alias(a1), Self::Alias(a2)) => a1 == a2,
            _ => false,
        }
    }
}

impl<R: Repr> Eq for Yaml<R> {}

impl<R: Repr> From<()> for Yaml<R> {
    fn from(_: ()) -> Self {
        Self::Null
    }
}

impl<R: Repr> From<bool> for Yaml<R> {
    fn from(b: bool) -> Self {
        Self::Bool(b)
    }
}

impl_from! {
    impl char, &str, String, &String => Str
    impl usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128 => Int
    impl f32, f64 => Float
}

impl<R: Repr> From<Seq<R>> for Yaml<R> {
    fn from(a: Seq<R>) -> Self {
        Self::Seq(a)
    }
}

impl<R: Repr> From<Map<R>> for Yaml<R> {
    fn from(m: Map<R>) -> Self {
        Self::Map(m)
    }
}

impl_iter! {
    impl Node<R> => Seq
    impl (Node<R>, Node<R>) => Map
}