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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use crate::{repr::*, *};
use alloc::string::ToString;
use core::{
    fmt::{Debug, Formatter, Result as FmtResult},
    hash::Hash,
    iter::FromIterator,
    ops::Index,
    str::FromStr,
};

macro_rules! as_method {
    {$(#[$meta:meta])* fn $id:ident = $ty:ident$(($op:ident))?
        $(| ($default:expr)?)?
        $(| $ty2:ident)* -> $r:ty} => {
        $(#[$meta])*
        pub fn $id(&self) -> Result<$r, u64> {
            match self.yaml() {
                YamlBase::$ty(v) $(| YamlBase::$ty2(v))* => Ok(v$(.$op())?),
                $(YamlBase::Null => Ok($default),)?
                _ => Err(self.pos()),
            }
        }
    };
}

macro_rules! as_num_method {
    {$(#[$meta:meta])* fn $id:ident = $ty1:ident $(| $ty2:ident)*} => {
        $(#[$meta])*
        pub fn $id<N: FromStr>(&self) -> Result<N, u64> {
            match self.yaml() {
                YamlBase::$ty1(n) $(| YamlBase::$ty2(n))* => match n.parse() {
                    Ok(v) => Ok(v),
                    Err(_) => Err(self.pos()),
                },
                _ => Err(self.pos()),
            }
        }
    };
}

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

/// A node with [`alloc::rc::Rc`] holder.
pub type Node = NodeBase<RcRepr>;
/// A node with [`alloc::sync::Arc`] holder.
pub type ArcNode = NodeBase<ArcRepr>;

/// Readonly node, including line number, column number, type assertion and anchor.
/// You can access [`YamlBase`] type through [`NodeBase::yaml`] method.
///
/// This type will ignore additional information when comparison and hashing.
///
/// ```
/// use std::collections::HashSet;
/// use yaml_peg::{Node, Yaml};
///
/// let mut s = HashSet::new();
/// s.insert(Node::new(Yaml::from("a"), 0, "", ""));
/// s.insert(Node::new("a", 1, "my-tag", ""));
/// s.insert(Node::new("a", 2, "", "my-anchor"));
/// assert_eq!(s.len(), 1);
/// ```
///
/// There is also a convenient macro [`node!`] to create nodes literally.
/// Please see the macro description for more information.
///
/// Nodes can be indexing by convertable values, or sequence indicator [`Ind`],
/// but it will be panic if the index is not contained.
///
/// ```
/// use yaml_peg::{node, Ind};
///
/// let n = node!(["a", "b", "c"]);
/// assert_eq!(node!("b"), n[Ind(1)]);
/// ```
///
/// ```should_panic
/// use yaml_peg::{node, Ind};
///
/// let n = node!(());
/// let n = &n["a"][Ind(0)]["b"];
/// ```
///
/// Same as containers, to prevent panic, the [`NodeBase::get`] method is the best choice.
/// The [`NodeBase::get_default`] can provide missing key value when indexing.
///
/// There are `as_*` methods provide `Result<T, u64>` returns with node position,
/// default options can be created by [`Result::unwrap_or`],
/// additional error message can be attach by [`Result::map_err`],
/// and the optional [`Option`] can be return by [`Result::ok`],
/// which shown as following example:
///
/// ```
/// use yaml_peg::node;
///
/// fn main() -> Result<(), (&'static str, u64)> {
///     let n = node!({
///         "title" => 12.
///     });
///     let n = n.get("title").map_err(|p| ("missing \"title\"", p))?;
///     assert_eq!(
///         Err(("title", 0)),
///         n.as_str().map_err(|p| ("title", p))
///     );
///     assert_eq!(
///         Option::<&str>::None,
///         n.as_str().ok()
///     );
///     Ok(())
/// }
/// ```
///
/// # Anchor
///
/// The anchors can be infer from [`AnchorBase`], and attach with [`NodeBase::as_anchor`] method.
///
/// # Clone
///
/// Since the YAML data is wrapped by reference counter [`alloc::rc::Rc`] and [`alloc::sync::Arc`],
/// cloning node just increase the reference counter,
/// the entire data structure are still shared together.
///
/// ```
/// use std::rc::Rc;
/// use yaml_peg::node;
///
/// let a = node!("a");
/// {
///     let b = a.clone();
///     assert_eq!(2, Rc::strong_count(&b));
/// }
/// assert_eq!(1, Rc::strong_count(&a));
/// ```
///
/// If you want to copy data, please get the data first.
#[derive(Hash, Eq, PartialEq, Clone)]
pub struct NodeBase<R: Repr>(pub(crate) R);

impl<R: Repr> NodeBase<R> {
    /// Create node from YAML data.
    pub fn new<Y>(yaml: Y, pos: u64, tag: &str, anchor: &str) -> Self
    where
        Y: Into<YamlBase<R>>,
    {
        let yaml = yaml.into();
        Self(R::repr(yaml, pos, tag.to_string(), anchor.to_string()))
    }

    /// Document position.
    #[inline(always)]
    pub fn pos(&self) -> u64 {
        self.0.pos
    }

    /// Tag. If the tag is not specified, returns a default tag from core schema.
    ///
    /// Anchor has no tag.
    #[inline(always)]
    pub fn tag(&self) -> &str {
        match self.0.tag.as_str() {
            "" => match self.yaml() {
                YamlBase::Null => concat!(parser::tag_prefix!(), "null"),
                YamlBase::Bool(_) => concat!(parser::tag_prefix!(), "bool"),
                YamlBase::Int(_) => concat!(parser::tag_prefix!(), "int"),
                YamlBase::Float(_) => concat!(parser::tag_prefix!(), "float"),
                YamlBase::Str(_) => concat!(parser::tag_prefix!(), "str"),
                YamlBase::Seq(_) => concat!(parser::tag_prefix!(), "seq"),
                YamlBase::Map(_) => concat!(parser::tag_prefix!(), "map"),
                YamlBase::Anchor(_) => "",
            },
            s => s,
        }
    }

    /// Anchor reference.
    #[inline(always)]
    pub fn anchor(&self) -> &str {
        &self.0.anchor
    }

    /// YAML data.
    #[inline(always)]
    pub fn yaml(&self) -> &YamlBase<R> {
        &self.0.yaml
    }

    /// Check the value is null.
    pub fn is_null(&self) -> bool {
        *self.yaml() == YamlBase::Null
    }

    as_method! {
        /// Convert to boolean.
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// assert!(node!(true).as_bool().unwrap());
        /// ```
        fn as_bool = Bool(clone) -> bool
    }

    as_num_method! {
        /// Convert to integer.
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// assert_eq!(60, node!(60).as_int().unwrap());
        /// ```
        fn as_int = Int
    }

    as_num_method! {
        /// Convert to float.
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// assert_eq!(20.06, node!(20.06).as_float().unwrap());
        /// ```
        fn as_float = Float
    }

    as_num_method! {
        /// Convert to number.
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// assert_eq!(60, node!(60).as_number().unwrap());
        /// assert_eq!(20.06, node!(20.06).as_number().unwrap());
        /// ```
        fn as_number = Int | Float
    }

    as_method! {
        /// Convert to string pointer.
        ///
        /// This method allows null, it represented as empty string.
        /// You can check them by [`str::is_empty`].
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// assert_eq!("abc", node!("abc").as_str().unwrap());
        /// assert!(node!(()).as_str().unwrap().is_empty());
        /// ```
        fn as_str = Str | ("")? -> &str
    }

    /// Convert to string pointer for string, null, bool, int, and float type.
    ///
    /// This method is useful when the option mixed with digit values.
    ///
    /// ```
    /// use yaml_peg::node;
    ///
    /// assert_eq!("abc", node!("abc").as_value().unwrap());
    /// assert_eq!("123", node!(123).as_value().unwrap());
    /// assert_eq!("12.04", node!(12.04).as_value().unwrap());
    /// assert_eq!("true", node!(true).as_value().unwrap());
    /// assert_eq!("false", node!(false).as_value().unwrap());
    /// assert!(node!(()).as_value().unwrap().is_empty());
    /// ```
    pub fn as_value(&self) -> Result<&str, u64> {
        match self.yaml() {
            YamlBase::Str(s) | YamlBase::Int(s) | YamlBase::Float(s) => Ok(s),
            YamlBase::Bool(true) => Ok("true"),
            YamlBase::Bool(false) => Ok("false"),
            YamlBase::Null => Ok(""),
            _ => Err(self.pos()),
        }
    }

    /// Infer an anchor with anchor visitor.
    ///
    /// If the node is not a anchor, or the anchor does not exist, the original node is returned.
    /// Since the anchor type is invalid except for this method, missing anchor will still return an error.
    ///
    /// ```
    /// use yaml_peg::{node, anchors};
    ///
    /// let node_a = node!(*"a");
    /// let v = anchors!["a" => 20.];
    /// assert_eq!(20., node_a.as_anchor(&v).as_float().unwrap());
    /// ```
    pub fn as_anchor<'a, 'b: 'a>(&'a self, v: &'b AnchorBase<R>) -> &'a Self {
        match self.yaml() {
            YamlBase::Anchor(s) if v.contains_key(s) => v.get(s).unwrap(),
            _ => self,
        }
    }

    as_method! {
        /// Convert to sequence.
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// let n = node!(["55"]);
        /// assert_eq!(node!("55"), n.as_seq().unwrap()[0]);
        /// for n in n.as_seq().unwrap() {
        ///     assert_eq!(node!("55"), n);
        /// }
        /// ```
        fn as_seq = Seq(clone) -> Seq<R>
    }

    as_method! {
        /// Convert to map.
        ///
        /// ```
        /// use yaml_peg::node;
        ///
        /// let n = node!({1 => 2});
        /// assert_eq!(node!(2), n.as_map().unwrap()[&node!(1)]);
        /// for (k, v) in n.as_map().unwrap() {
        ///     assert_eq!(node!(1), k);
        ///     assert_eq!(node!(2), v);
        /// }
        /// ```
        fn as_map = Map(clone) -> Map<R>
    }

    /// Convert to map and try to get the value by key.
    ///
    /// If any key is missing, return `Err` with node position.
    ///
    /// ```
    /// # fn main() -> Result<(), u64> {
    /// use yaml_peg::node;
    ///
    /// let n = node!({node!("a") => node!({node!("b") => node!(30.)})});
    /// assert_eq!(&node!(30.), n.get("a")?.get("b")?);
    /// # Ok::<(), u64>(()) }
    /// ```
    pub fn get<Y: Into<Self>>(&self, key: Y) -> Result<&Self, u64> {
        if let YamlBase::Map(m) = self.yaml() {
            if let Some(n) = m.get(&key.into()) {
                Ok(n)
            } else {
                Err(self.pos())
            }
        } else {
            Err(self.pos())
        }
    }

    /// Same as [`NodeBase::get`] but provide default value if the key is missing.
    /// For this method, a transform method `as_*` is required.
    ///
    /// + If the value exist, return the value.
    /// + If value is a wrong type, return `Err` with node position.
    /// + If the value is not exist, return the default value.
    ///
    /// ```
    /// # fn main() -> Result<(), u64> {
    /// use yaml_peg::{node, NodeBase};
    ///
    /// let a = node!({node!("a") => node!({node!("b") => node!("c")})});
    /// assert_eq!(
    ///     "c",
    ///     a.get("a")?.get_default("b", "d", NodeBase::as_str)?
    /// );
    /// let b = node!({node!("a") => node!({})});
    /// assert_eq!(
    ///     "d",
    ///     b.get("a")?.get_default("b", "d", NodeBase::as_str)?
    /// );
    /// let c = node!({node!("a") => node!({node!("b") => node!(20.)})});
    /// assert_eq!(
    ///     Err(0),
    ///     c.get("a")?.get_default("b", "d", NodeBase::as_str)
    /// );
    /// # Ok::<(), u64>(()) }
    /// ```
    ///
    /// ```
    /// # fn main() -> Result<(), u64> {
    /// use yaml_peg::{node, NodeBase};
    ///
    /// let n = node!({node!("a") => node!([node!(1), node!(2), node!(3)])});
    /// let a = n.get_default("c", vec![], NodeBase::as_seq)?;
    /// assert_eq!(a, vec![]);
    /// # Ok::<(), u64>(()) }
    /// ```
    pub fn get_default<'a, Y, Ret, F>(
        &'a self,
        key: Y,
        default: Ret,
        factory: F,
    ) -> Result<Ret, u64>
    where
        Y: Into<Self>,
        F: Fn(&'a Self) -> Result<Ret, u64>,
    {
        if let YamlBase::Map(m) = self.yaml() {
            if let Some(n) = m.get(&key.into()) {
                factory(n)
            } else {
                Ok(default)
            }
        } else {
            Err(self.pos())
        }
    }

    /// Trait as map then get a key-value with anchor visitor,
    /// returns default value if key is not found.
    ///
    /// If the value type is incorrect, returns [`Err`].
    /// Same as [`NodeBase::get_default`] but support anchor visitor.
    ///
    /// ```
    /// # fn main() -> Result<(), u64> {
    /// use yaml_peg::{anchors, node, Node};
    ///
    /// let v = anchors!["c" => 10];
    /// let a = node!({node!("a") => node!({node!("b") => node!(*"c")})});
    /// let c = a.get("a")?.with(&v, "b", 0, Node::as_int)?;
    /// assert_eq!(10, c);
    /// let c = a.get("a")?.with(&v, "b", "default", Node::as_str);
    /// assert_eq!(Err(0), c);
    /// # Ok::<(), u64>(()) }
    /// ```
    pub fn with<'a, 'b: 'a, Y, Ret, F>(
        &'a self,
        v: &'b AnchorBase<R>,
        key: Y,
        default: Ret,
        factory: F,
    ) -> Result<Ret, u64>
    where
        Y: Into<Self>,
        F: Fn(&'a Self) -> Result<Ret, u64>,
    {
        if let YamlBase::Map(m) = self.yaml() {
            if let Some(n) = m.get(&key.into()) {
                factory(n.as_anchor(v))
            } else {
                Ok(default)
            }
        } else {
            Err(self.pos())
        }
    }

    /// Get node through index indicator. Only suitable for sequence.
    ///
    /// ```
    /// # fn main() -> Result<(), u64> {
    /// use yaml_peg::{node, Ind};
    ///
    /// let n = node!([node!("a"), node!("b"), node!("c")]);
    /// assert_eq!(&node!("b"), n.get_ind(Ind(1))?);
    /// # Ok::<(), u64>(()) }
    /// ```
    pub fn get_ind(&self, ind: Ind) -> Result<&Self, u64> {
        if let YamlBase::Seq(a) = self.yaml() {
            if let Some(n) = a.get(ind.0) {
                Ok(n)
            } else {
                Err(self.pos())
            }
        } else {
            Err(self.pos())
        }
    }
}

impl<R: Repr> Debug for NodeBase<R> {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_fmt(format_args!("Node{:?}", &self.0))
    }
}

/// Node index indicator use to indicate sequence position.
pub struct Ind(pub usize);

impl<R: Repr> Index<Ind> for NodeBase<R> {
    type Output = Self;

    fn index(&self, index: Ind) -> &Self::Output {
        if let YamlBase::Seq(a) = self.yaml() {
            a.index(index.0)
        } else {
            panic!("out of bound!")
        }
    }
}

impl<R, I> Index<I> for NodeBase<R>
where
    R: Repr,
    I: Into<Self>,
{
    type Output = Self;

    fn index(&self, index: I) -> &Self::Output {
        if let YamlBase::Map(m) = self.yaml() {
            m.get(&index.into())
                .unwrap_or_else(|| panic!("out of bound!"))
        } else {
            panic!("out of bound!")
        }
    }
}

impl<R, Y> From<Y> for NodeBase<R>
where
    R: Repr,
    Y: Into<YamlBase<R>>,
{
    fn from(yaml: Y) -> Self {
        Self::new(yaml, 0, "", "")
    }
}

impl_iter! {
    impl Self
    impl (Self, Self)
}