1use super::{BindingsIter, Definition, EntryValueSubItems, Key, Type, Version, Vocab};
2use crate::{context::TermDefinition, Direction, LenientLangTagBuf, Nullable};
3
4use iref::IriRef;
5
6impl Definition {
7 pub fn iter(&self) -> Entries {
8 Entries {
9 base: self.base.as_ref().map(Nullable::as_deref),
10 import: self.import.as_deref(),
11 language: self.language.as_ref().map(Nullable::as_ref),
12 direction: self.direction,
13 propagate: self.propagate,
14 protected: self.protected,
15 type_: self.type_,
16 version: self.version,
17 vocab: self.vocab.as_ref().map(Nullable::as_ref),
18 bindings: self.bindings.iter(),
19 }
20 }
21}
22
23pub struct Entries<'a> {
24 base: Option<Nullable<&'a IriRef>>,
25 import: Option<&'a IriRef>,
26 language: Option<Nullable<&'a LenientLangTagBuf>>,
27 direction: Option<Nullable<Direction>>,
28 propagate: Option<bool>,
29 protected: Option<bool>,
30 type_: Option<Type>,
31 version: Option<Version>,
32 vocab: Option<Nullable<&'a Vocab>>,
33 bindings: BindingsIter<'a>,
34}
35
36impl<'a> Iterator for Entries<'a> {
37 type Item = EntryRef<'a>;
38
39 fn size_hint(&self) -> (usize, Option<usize>) {
40 let mut len = self.bindings.len();
41
42 if self.base.is_some() {
43 len += 1
44 }
45
46 if self.import.is_some() {
47 len += 1
48 }
49
50 if self.language.is_some() {
51 len += 1
52 }
53
54 if self.direction.is_some() {
55 len += 1
56 }
57
58 if self.propagate.is_some() {
59 len += 1
60 }
61
62 if self.protected.is_some() {
63 len += 1
64 }
65
66 if self.type_.is_some() {
67 len += 1
68 }
69
70 if self.version.is_some() {
71 len += 1
72 }
73
74 if self.vocab.is_some() {
75 len += 1
76 }
77
78 (len, Some(len))
79 }
80
81 fn next(&mut self) -> Option<Self::Item> {
82 match self.base.take() {
83 Some(value) => Some(EntryRef::Base(value)),
84 None => match self.import.take() {
85 Some(value) => Some(EntryRef::Import(value)),
86 None => match self.language.take() {
87 Some(value) => Some(EntryRef::Language(value)),
88 None => match self.direction.take() {
89 Some(value) => Some(EntryRef::Direction(value)),
90 None => match self.propagate.take() {
91 Some(value) => Some(EntryRef::Propagate(value)),
92 None => match self.protected.take() {
93 Some(value) => Some(EntryRef::Protected(value)),
94 None => match self.type_.take() {
95 Some(value) => Some(EntryRef::Type(value)),
96 None => match self.version.take() {
97 Some(value) => Some(EntryRef::Version(value)),
98 None => match self.vocab.take() {
99 Some(value) => Some(EntryRef::Vocab(value)),
100 None => self
101 .bindings
102 .next()
103 .map(|(k, v)| EntryRef::Definition(k, v)),
104 },
105 },
106 },
107 },
108 },
109 },
110 },
111 },
112 }
113 }
114}
115
116impl<'a> ExactSizeIterator for Entries<'a> {}
117
118pub enum EntryValueRef<'a> {
119 Base(Nullable<&'a IriRef>),
120 Import(&'a IriRef),
121 Language(Nullable<&'a LenientLangTagBuf>),
122 Direction(Nullable<Direction>),
123 Propagate(bool),
124 Protected(bool),
125 Type(Type),
126 Version(Version),
127 Vocab(Nullable<&'a Vocab>),
128 Definition(Nullable<&'a TermDefinition>),
129}
130
131impl<'a> EntryValueRef<'a> {
132 pub fn is_object(&self) -> bool {
133 match self {
134 Self::Type(_) => true,
135 Self::Definition(Nullable::Some(d)) => d.is_object(),
136 _ => false,
137 }
138 }
139
140 pub fn sub_items(&self) -> EntryValueSubItems<'a> {
141 match self {
142 Self::Definition(Nullable::Some(TermDefinition::Expanded(e))) => {
143 EntryValueSubItems::TermDefinitionFragment(Box::new(e.iter()))
144 }
145 _ => EntryValueSubItems::None,
146 }
147 }
148}
149
150pub enum EntryRef<'a> {
151 Base(Nullable<&'a IriRef>),
152 Import(&'a IriRef),
153 Language(Nullable<&'a LenientLangTagBuf>),
154 Direction(Nullable<Direction>),
155 Propagate(bool),
156 Protected(bool),
157 Type(Type),
158 Version(Version),
159 Vocab(Nullable<&'a Vocab>),
160 Definition(&'a Key, Nullable<&'a TermDefinition>),
161}
162
163#[derive(Clone, Copy, PartialEq, Eq)]
164pub enum EntryKeyRef<'a> {
165 Base,
166 Import,
167 Language,
168 Direction,
169 Propagate,
170 Protected,
171 Type,
172 Version,
173 Vocab,
174 Definition(&'a Key),
175}
176
177impl<'a> EntryKeyRef<'a> {
178 pub fn as_str(&self) -> &'a str {
179 match self {
180 Self::Base => "@base",
181 Self::Import => "@import",
182 Self::Language => "@language",
183 Self::Direction => "@direction",
184 Self::Propagate => "@propagate",
185 Self::Protected => "@protected",
186 Self::Type => "@type",
187 Self::Version => "@version",
188 Self::Vocab => "@vocab",
189 Self::Definition(d) => d.as_str(),
190 }
191 }
192}
193
194impl<'a> EntryRef<'a> {
195 pub fn into_key(self) -> EntryKeyRef<'a> {
196 match self {
197 Self::Base(_) => EntryKeyRef::Base,
198 Self::Import(_) => EntryKeyRef::Import,
199 Self::Language(_) => EntryKeyRef::Language,
200 Self::Direction(_) => EntryKeyRef::Direction,
201 Self::Propagate(_) => EntryKeyRef::Propagate,
202 Self::Protected(_) => EntryKeyRef::Protected,
203 Self::Type(_) => EntryKeyRef::Type,
204 Self::Version(_) => EntryKeyRef::Version,
205 Self::Vocab(_) => EntryKeyRef::Vocab,
206 Self::Definition(key, _) => EntryKeyRef::Definition(key),
207 }
208 }
209
210 pub fn key(&self) -> EntryKeyRef<'a> {
211 match self {
212 Self::Base(_) => EntryKeyRef::Base,
213 Self::Import(_) => EntryKeyRef::Import,
214 Self::Language(_) => EntryKeyRef::Language,
215 Self::Direction(_) => EntryKeyRef::Direction,
216 Self::Propagate(_) => EntryKeyRef::Propagate,
217 Self::Protected(_) => EntryKeyRef::Protected,
218 Self::Type(_) => EntryKeyRef::Type,
219 Self::Version(_) => EntryKeyRef::Version,
220 Self::Vocab(_) => EntryKeyRef::Vocab,
221 Self::Definition(key, _) => EntryKeyRef::Definition(key),
222 }
223 }
224
225 pub fn into_value(self) -> EntryValueRef<'a> {
226 match self {
227 Self::Base(v) => EntryValueRef::Base(v),
228 Self::Import(v) => EntryValueRef::Import(v),
229 Self::Language(v) => EntryValueRef::Language(v),
230 Self::Direction(v) => EntryValueRef::Direction(v),
231 Self::Propagate(v) => EntryValueRef::Propagate(v),
232 Self::Protected(v) => EntryValueRef::Protected(v),
233 Self::Type(v) => EntryValueRef::Type(v),
234 Self::Version(v) => EntryValueRef::Version(v),
235 Self::Vocab(v) => EntryValueRef::Vocab(v),
236 Self::Definition(_, b) => EntryValueRef::Definition(b),
237 }
238 }
239
240 pub fn value(&self) -> EntryValueRef<'a> {
241 match self {
242 Self::Base(v) => EntryValueRef::Base(*v),
243 Self::Import(v) => EntryValueRef::Import(v),
244 Self::Language(v) => EntryValueRef::Language(*v),
245 Self::Direction(v) => EntryValueRef::Direction(*v),
246 Self::Propagate(v) => EntryValueRef::Propagate(*v),
247 Self::Protected(v) => EntryValueRef::Protected(*v),
248 Self::Type(v) => EntryValueRef::Type(*v),
249 Self::Version(v) => EntryValueRef::Version(*v),
250 Self::Vocab(v) => EntryValueRef::Vocab(*v),
251 Self::Definition(_, b) => EntryValueRef::Definition(*b),
252 }
253 }
254
255 pub fn into_key_value(self) -> (EntryKeyRef<'a>, EntryValueRef<'a>) {
256 self.key_value()
257 }
258
259 pub fn key_value(&self) -> (EntryKeyRef<'a>, EntryValueRef<'a>) {
260 match self {
261 Self::Base(v) => (EntryKeyRef::Base, EntryValueRef::Base(*v)),
262 Self::Import(v) => (EntryKeyRef::Import, EntryValueRef::Import(v)),
263 Self::Language(v) => (EntryKeyRef::Language, EntryValueRef::Language(*v)),
264 Self::Direction(v) => (EntryKeyRef::Direction, EntryValueRef::Direction(*v)),
265 Self::Propagate(v) => (EntryKeyRef::Propagate, EntryValueRef::Propagate(*v)),
266 Self::Protected(v) => (EntryKeyRef::Protected, EntryValueRef::Protected(*v)),
267 Self::Type(v) => (EntryKeyRef::Type, EntryValueRef::Type(*v)),
268 Self::Version(v) => (EntryKeyRef::Version, EntryValueRef::Version(*v)),
269 Self::Vocab(v) => (EntryKeyRef::Vocab, EntryValueRef::Vocab(*v)),
270 Self::Definition(key, b) => {
271 (EntryKeyRef::Definition(key), EntryValueRef::Definition(*b))
272 }
273 }
274 }
275}