Skip to main content

json_ld/syntax/context/definition/
reference.rs

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