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
use alloc::{
    slice::{from_ref, Iter},
    vec,
    vec::Vec,
};
use serde::{Deserialize, Serialize};

/// A data type that can support listed items,
/// or inline it if there is single item.
///
/// ```
/// use serde::Deserialize;
/// use yaml_peg::{node, serde::InlineList};
///
/// #[derive(Deserialize)]
/// struct Content {
///     img: InlineList<Img>,
/// }
///
/// #[derive(Deserialize)]
/// struct Img {
///     src: String,
/// }
///
/// let n_listed = node!({"img" => node!([node!({"src" => "img/1.png"}), node!({"src" => "img/2.png"})])});
/// let n_inline = node!({"img" => node!({"src" => "img/1.png"})});
/// let listed = Content::deserialize(n_listed).unwrap();
/// let inline = Content::deserialize(n_inline).unwrap();
/// for (i, img) in listed.img.iter().enumerate() {
///     assert_eq!(format!("img/{}.png", i + 1), img.src);
/// }
/// for (i, img) in inline.img.into_iter().enumerate() {
///     assert_eq!(format!("img/{}.png", i + 1), img.src);
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum InlineList<T> {
    /// Listed representation.
    List(Vec<T>),
    /// Inline representation.
    Inline(T),
}

impl<T> InlineList<T> {
    /// Return the iterator over the items.
    pub fn iter(&self) -> Iter<T> {
        match self {
            Self::List(v) => v.iter(),
            Self::Inline(e) => from_ref(e).iter(),
        }
    }

    /// Get the length of the list.
    pub fn len(&self) -> usize {
        match self {
            Self::List(v) => v.len(),
            Self::Inline(_) => 1,
        }
    }

    /// Return true if the list is empty.
    pub fn is_empty(&self) -> bool {
        match self {
            Self::List(v) => v.is_empty(),
            Self::Inline(_) => false,
        }
    }

    /// Return true if the list has only one item.
    pub fn is_single(&self) -> bool {
        match self {
            Self::List(v) => v.len() == 1,
            Self::Inline(_) => true,
        }
    }
}

impl<T> IntoIterator for InlineList<T> {
    type Item = T;
    type IntoIter = <Vec<T> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        match self {
            Self::List(v) => v.into_iter(),
            Self::Inline(e) => vec![e].into_iter(),
        }
    }
}

impl<T> Default for InlineList<T> {
    fn default() -> Self {
        Self::List(Vec::new())
    }
}