json_ld_expansion_next/
expanded.rs1use json_ld_core_next::IndexedObject;
2
3pub enum Expanded<T, B> {
4 Null,
5 Object(IndexedObject<T, B>),
6 Array(Vec<IndexedObject<T, B>>),
7}
8
9impl<T, B> Expanded<T, B> {
10 pub fn len(&self) -> usize {
11 match self {
12 Expanded::Null => 0,
13 Expanded::Object(_) => 1,
14 Expanded::Array(ary) => ary.len(),
15 }
16 }
17
18 pub fn is_empty(&self) -> bool {
19 self.len() == 0
20 }
21
22 pub fn is_null(&self) -> bool {
23 matches!(self, Expanded::Null)
24 }
25
26 pub fn is_list(&self) -> bool {
27 match self {
28 Expanded::Object(o) => o.is_list(),
29 _ => false,
30 }
31 }
32
33 pub fn iter(&self) -> Iter<T, B> {
34 match self {
35 Expanded::Null => Iter::Null,
36 Expanded::Object(ref o) => Iter::Object(Some(o)),
37 Expanded::Array(ary) => Iter::Array(ary.iter()),
38 }
39 }
40}
41
42impl<T, B> IntoIterator for Expanded<T, B> {
43 type Item = IndexedObject<T, B>;
44 type IntoIter = IntoIter<T, B>;
45
46 fn into_iter(self) -> IntoIter<T, B> {
47 match self {
48 Expanded::Null => IntoIter::Null,
49 Expanded::Object(o) => IntoIter::Object(Some(o)),
50 Expanded::Array(ary) => IntoIter::Array(ary.into_iter()),
51 }
52 }
53}
54
55impl<'a, T, B> IntoIterator for &'a Expanded<T, B> {
56 type Item = &'a IndexedObject<T, B>;
57 type IntoIter = Iter<'a, T, B>;
58
59 fn into_iter(self) -> Iter<'a, T, B> {
60 self.iter()
61 }
62}
63
64pub enum Iter<'a, T, B> {
65 Null,
66 Object(Option<&'a IndexedObject<T, B>>),
67 Array(std::slice::Iter<'a, IndexedObject<T, B>>),
68}
69
70impl<'a, T, B> Iterator for Iter<'a, T, B> {
71 type Item = &'a IndexedObject<T, B>;
72
73 fn next(&mut self) -> Option<&'a IndexedObject<T, B>> {
74 match self {
75 Iter::Null => None,
76 Iter::Object(ref mut o) => {
77 let mut result = None;
78 std::mem::swap(o, &mut result);
79 result
80 }
81 Iter::Array(ref mut it) => it.next(),
82 }
83 }
84}
85
86pub enum IntoIter<T, B> {
87 Null,
88 Object(Option<IndexedObject<T, B>>),
89 Array(std::vec::IntoIter<IndexedObject<T, B>>),
90}
91
92impl<T, B> Iterator for IntoIter<T, B> {
93 type Item = IndexedObject<T, B>;
94
95 fn next(&mut self) -> Option<IndexedObject<T, B>> {
96 match self {
97 IntoIter::Null => None,
98 IntoIter::Object(ref mut o) => {
99 let mut result = None;
100 std::mem::swap(o, &mut result);
101 result
102 }
103 IntoIter::Array(ref mut it) => it.next(),
104 }
105 }
106}
107
108impl<T, B> From<IndexedObject<T, B>> for Expanded<T, B> {
109 fn from(obj: IndexedObject<T, B>) -> Expanded<T, B> {
110 Expanded::Object(obj)
111 }
112}
113
114impl<T, B> From<Vec<IndexedObject<T, B>>> for Expanded<T, B> {
115 fn from(list: Vec<IndexedObject<T, B>>) -> Expanded<T, B> {
116 Expanded::Array(list)
117 }
118}