Skip to main content

jsonld_expansion/
expanded.rs

1use jsonld_core::{Id, IndexedObject};
2use std::hash::Hash;
3
4/// Result of expanding a fragment of a JSON-LD document.
5///
6/// Expansion may leave nothing behind, produce a single object, or turn one
7/// element into several — which is why it is not simply an
8/// [`IndexedObject`].
9pub enum Expanded<T, B> {
10    /// The fragment expanded to nothing, and is dropped.
11    Null,
12    /// The fragment expanded to a single object.
13    Object(IndexedObject<T, B>),
14    /// The fragment expanded to any number of objects.
15    Array(Vec<IndexedObject<T, B>>),
16}
17
18impl<T, B> Expanded<T, B> {
19    /// Returns the number of objects the fragment expanded to.
20    pub fn len(&self) -> usize {
21        match self {
22            Expanded::Null => 0,
23            Expanded::Object(_) => 1,
24            Expanded::Array(ary) => ary.len(),
25        }
26    }
27
28    /// Checks whether the fragment expanded to no object at all, either
29    /// because it expanded to nothing or to an empty array.
30    pub fn is_empty(&self) -> bool {
31        self.len() == 0
32    }
33
34    /// Checks whether the fragment expanded to nothing, as opposed to an empty
35    /// array of objects.
36    pub fn is_null(&self) -> bool {
37        matches!(self, Expanded::Null)
38    }
39
40    /// Checks whether the fragment expanded to a single list object (a `@list`
41    /// entry).
42    pub fn is_list(&self) -> bool {
43        match self {
44            Expanded::Object(o) => o.is_list(),
45            _ => false,
46        }
47    }
48
49    /// Returns an iterator over the expanded objects.
50    pub fn iter(&self) -> Iter<'_, T, B> {
51        match self {
52            Expanded::Null => Iter::Null,
53            Expanded::Object(o) => Iter::Object(Some(o)),
54            Expanded::Array(ary) => Iter::Array(ary.iter()),
55        }
56    }
57
58    /// Rewrites every IRI and identifier of this expansion result
59    /// (recursively) with the given functions.
60    ///
61    /// `map_iri` translates the datatype IRI of the literal values, `map_id`
62    /// the node identifiers, node types and property identifiers. Use it to
63    /// move a result produced against one vocabulary into the identifier space
64    /// of another.
65    pub fn map_ids<U, C>(self, mut map_iri: impl FnMut(T) -> U, mut map_id: impl FnMut(Id<T, B>) -> Id<U, C>) -> Expanded<U, C>
66    where
67        U: Eq + Hash,
68        C: Eq + Hash,
69    {
70        match self {
71            Expanded::Null => Expanded::Null,
72            Expanded::Object(object) => Expanded::Object(object.map_inner(|o| o.map_ids(&mut map_iri, &mut map_id))),
73            Expanded::Array(array) => Expanded::Array(
74                array
75                    .into_iter()
76                    .map(|object| object.map_inner(|o| o.map_ids(&mut map_iri, &mut map_id)))
77                    .collect(),
78            ),
79        }
80    }
81}
82
83impl<T, B> IntoIterator for Expanded<T, B> {
84    type Item = IndexedObject<T, B>;
85    type IntoIter = IntoIter<T, B>;
86
87    fn into_iter(self) -> IntoIter<T, B> {
88        match self {
89            Expanded::Null => IntoIter::Null,
90            Expanded::Object(o) => IntoIter::Object(Some(o)),
91            Expanded::Array(ary) => IntoIter::Array(ary.into_iter()),
92        }
93    }
94}
95
96impl<'a, T, B> IntoIterator for &'a Expanded<T, B> {
97    type Item = &'a IndexedObject<T, B>;
98    type IntoIter = Iter<'a, T, B>;
99
100    fn into_iter(self) -> Iter<'a, T, B> {
101        self.iter()
102    }
103}
104
105/// Iterator over the objects of an expansion result.
106pub enum Iter<'a, T, B> {
107    /// Iterator over an expansion result that produced nothing.
108    Null,
109    /// Iterator over a single expanded object, until it has been yielded.
110    Object(Option<&'a IndexedObject<T, B>>),
111    /// Iterator over the objects of an expanded array.
112    Array(std::slice::Iter<'a, IndexedObject<T, B>>),
113}
114
115impl<'a, T, B> Iterator for Iter<'a, T, B> {
116    type Item = &'a IndexedObject<T, B>;
117
118    fn next(&mut self) -> Option<&'a IndexedObject<T, B>> {
119        match self {
120            Iter::Null => None,
121            Iter::Object(o) => {
122                let mut result = None;
123                std::mem::swap(o, &mut result);
124                result
125            }
126            Iter::Array(it) => it.next(),
127        }
128    }
129}
130
131/// Owning iterator over the objects of an expansion result.
132pub enum IntoIter<T, B> {
133    /// Iterator over an expansion result that produced nothing.
134    Null,
135    /// Iterator over a single expanded object, until it has been yielded.
136    Object(Option<IndexedObject<T, B>>),
137    /// Iterator over the objects of an expanded array.
138    Array(std::vec::IntoIter<IndexedObject<T, B>>),
139}
140
141impl<T, B> Iterator for IntoIter<T, B> {
142    type Item = IndexedObject<T, B>;
143
144    fn next(&mut self) -> Option<IndexedObject<T, B>> {
145        match self {
146            IntoIter::Null => None,
147            IntoIter::Object(o) => {
148                let mut result = None;
149                std::mem::swap(o, &mut result);
150                result
151            }
152            IntoIter::Array(it) => it.next(),
153        }
154    }
155}
156
157impl<T, B> From<IndexedObject<T, B>> for Expanded<T, B> {
158    fn from(obj: IndexedObject<T, B>) -> Expanded<T, B> {
159        Expanded::Object(obj)
160    }
161}
162
163impl<T, B> From<Vec<IndexedObject<T, B>>> for Expanded<T, B> {
164    fn from(list: Vec<IndexedObject<T, B>>) -> Expanded<T, B> {
165        Expanded::Array(list)
166    }
167}