Skip to main content

json_ld/syntax/context/definition/term/definition/
mod.rs

1use crate::{
2	JsonLdLenient,
3	syntax::{
4		JsonLdCompactIri, JsonLdCompactIriBuf, JsonLdContainerValue, JsonLdContext,
5		JsonLdDirection, JsonLdKeyword, JsonLdNullable,
6		context::{self, JsonLdContextTerm},
7	},
8};
9use langtag::{LangTag, LangTagBuf};
10use rdf_syntax::{BlankId, BlankIdBuf};
11use rdf_syntax::{Iri, IriBuf};
12
13mod id;
14mod index;
15mod nest;
16mod type_;
17
18pub use id::*;
19pub use index::*;
20pub use nest::*;
21pub use type_::*;
22
23/// Term definition.
24#[derive(PartialEq, Eq, Clone, Debug)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[cfg_attr(feature = "serde", serde(untagged))]
27pub enum JsonLdTermDefinition {
28	Simple(JsonLdSimpleTermDefinition),
29	Expanded(Box<JsonLdExpandedTermDefinition>),
30}
31
32impl JsonLdTermDefinition {
33	pub fn is_expanded(&self) -> bool {
34		matches!(self, Self::Expanded(_))
35	}
36
37	pub fn is_object(&self) -> bool {
38		self.is_expanded()
39	}
40
41	pub fn as_expanded(&self) -> JsonLdExpandedTermDefinitionRef<'_> {
42		match self {
43			Self::Simple(term) => JsonLdExpandedTermDefinitionRef {
44				id: Some(JsonLdNullable::Some(term.as_str().into())),
45				..Default::default()
46			},
47			Self::Expanded(e) => e.as_expanded_ref(),
48		}
49	}
50}
51
52#[derive(PartialEq, Eq, Clone, Debug)]
53#[cfg_attr(
54	feature = "serde",
55	derive(serde::Serialize, serde::Deserialize),
56	serde(transparent)
57)]
58pub struct JsonLdSimpleTermDefinition(pub String);
59
60impl JsonLdSimpleTermDefinition {
61	pub fn as_iri(&self) -> Option<&Iri> {
62		Iri::new(&self.0).ok()
63	}
64
65	pub fn as_compact_iri(&self) -> Option<&JsonLdCompactIri> {
66		JsonLdCompactIri::new(&self.0).ok()
67	}
68
69	pub fn as_blank_id(&self) -> Option<&BlankId> {
70		BlankId::new(&self.0).ok()
71	}
72
73	pub fn as_str(&self) -> &str {
74		&self.0
75	}
76
77	pub fn into_string(self) -> String {
78		self.0
79	}
80}
81
82impl From<IriBuf> for JsonLdSimpleTermDefinition {
83	fn from(value: IriBuf) -> Self {
84		Self(value.into_string())
85	}
86}
87
88impl From<JsonLdCompactIriBuf> for JsonLdSimpleTermDefinition {
89	fn from(value: JsonLdCompactIriBuf) -> Self {
90		Self(value.into_string())
91	}
92}
93
94impl From<BlankIdBuf> for JsonLdSimpleTermDefinition {
95	fn from(value: BlankIdBuf) -> Self {
96		Self(value.to_string())
97	}
98}
99
100/// Expanded term definition.
101#[derive(Default, PartialEq, Eq, Clone, Debug)]
102#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
103pub struct JsonLdExpandedTermDefinition {
104	#[cfg_attr(
105		feature = "serde",
106		serde(
107			rename = "@id",
108			default,
109			deserialize_with = "JsonLdNullable::optional",
110			skip_serializing_if = "Option::is_none"
111		)
112	)]
113	pub id: Option<JsonLdNullable<JsonLdTermId>>,
114
115	#[cfg_attr(
116		feature = "serde",
117		serde(
118			rename = "@type",
119			default,
120			deserialize_with = "JsonLdNullable::optional",
121			skip_serializing_if = "Option::is_none"
122		)
123	)]
124	pub type_: Option<JsonLdNullable<JsonLdTermType>>,
125
126	#[cfg_attr(
127		feature = "serde",
128		serde(
129			rename = "@context",
130			default,
131			deserialize_with = "context::JsonLdContext::optional",
132			skip_serializing_if = "Option::is_none"
133		)
134	)]
135	pub context: Option<Box<context::JsonLdContext>>,
136
137	#[cfg_attr(
138		feature = "serde",
139		serde(rename = "@reverse", default, skip_serializing_if = "Option::is_none")
140	)]
141	pub reverse: Option<JsonLdContextTerm>,
142
143	#[cfg_attr(
144		feature = "serde",
145		serde(rename = "@index", default, skip_serializing_if = "Option::is_none")
146	)]
147	pub index: Option<JsonLdIndex>,
148
149	#[cfg_attr(
150		feature = "serde",
151		serde(
152			rename = "@language",
153			default,
154			deserialize_with = "JsonLdNullable::optional",
155			skip_serializing_if = "Option::is_none"
156		)
157	)]
158	pub language: Option<JsonLdNullable<JsonLdLenient<LangTagBuf>>>,
159
160	#[cfg_attr(
161		feature = "serde",
162		serde(
163			rename = "@direction",
164			default,
165			deserialize_with = "JsonLdNullable::optional",
166			skip_serializing_if = "Option::is_none"
167		)
168	)]
169	pub direction: Option<JsonLdNullable<JsonLdDirection>>,
170
171	#[cfg_attr(
172		feature = "serde",
173		serde(
174			rename = "@container",
175			default,
176			skip_serializing_if = "Option::is_none"
177		)
178	)]
179	pub container: Option<JsonLdContainerValue>,
180
181	#[cfg_attr(
182		feature = "serde",
183		serde(rename = "@nest", default, skip_serializing_if = "Option::is_none")
184	)]
185	pub nest: Option<JsonLdNest>,
186
187	#[cfg_attr(
188		feature = "serde",
189		serde(rename = "@prefix", default, skip_serializing_if = "Option::is_none")
190	)]
191	pub prefix: Option<bool>,
192
193	#[cfg_attr(
194		feature = "serde",
195		serde(
196			rename = "@propagate",
197			default,
198			skip_serializing_if = "Option::is_none"
199		)
200	)]
201	pub propagate: Option<bool>,
202
203	#[cfg_attr(
204		feature = "serde",
205		serde(
206			rename = "@protected",
207			default,
208			skip_serializing_if = "Option::is_none"
209		)
210	)]
211	pub protected: Option<bool>,
212}
213
214impl JsonLdExpandedTermDefinition {
215	pub fn new() -> Self {
216		Self::default()
217	}
218
219	pub fn is_null(&self) -> bool {
220		matches!(&self.id, None | Some(JsonLdNullable::Null))
221			&& self.type_.is_none()
222			&& self.context.is_none()
223			&& self.reverse.is_none()
224			&& self.index.is_none()
225			&& self.language.is_none()
226			&& self.direction.is_none()
227			&& self.container.is_none()
228			&& self.nest.is_none()
229			&& self.prefix.is_none()
230			&& self.propagate.is_none()
231			&& self.protected.is_none()
232	}
233
234	pub fn is_simple_definition(&self) -> bool {
235		matches!(&self.id, Some(JsonLdNullable::Some(_)))
236			&& self.type_.is_none()
237			&& self.context.is_none()
238			&& self.reverse.is_none()
239			&& self.index.is_none()
240			&& self.language.is_none()
241			&& self.direction.is_none()
242			&& self.container.is_none()
243			&& self.nest.is_none()
244			&& self.prefix.is_none()
245			&& self.propagate.is_none()
246			&& self.protected.is_none()
247	}
248
249	pub fn simplify(self) -> JsonLdNullable<JsonLdTermDefinition> {
250		if self.is_null() {
251			JsonLdNullable::Null
252		} else if self.is_simple_definition() {
253			JsonLdNullable::Some(JsonLdTermDefinition::Simple(JsonLdSimpleTermDefinition(
254				self.id.unwrap().unwrap().into_string(),
255			)))
256		} else {
257			JsonLdNullable::Some(JsonLdTermDefinition::Expanded(Box::new(self)))
258		}
259	}
260
261	pub fn iter(&self) -> JsonLdTermDefinitionEntries<'_> {
262		JsonLdTermDefinitionEntries {
263			id: self.id.as_ref().map(JsonLdNullable::as_ref),
264			type_: self.type_.as_ref().map(JsonLdNullable::as_ref),
265			context: self.context.as_deref(),
266			reverse: self.reverse.as_ref(),
267			index: self.index.as_ref(),
268			language: self.language.as_ref().map(JsonLdNullable::as_ref),
269			direction: self.direction,
270			container: self.container.as_ref(),
271			nest: self.nest.as_ref(),
272			prefix: self.prefix,
273			propagate: self.propagate,
274			protected: self.protected,
275		}
276	}
277
278	pub fn as_expanded_ref(&self) -> JsonLdExpandedTermDefinitionRef<'_> {
279		JsonLdExpandedTermDefinitionRef {
280			id: self
281				.id
282				.as_ref()
283				.map(|i| i.as_ref().map(|id| id.as_id_ref())),
284			type_: self.type_.as_ref().map(JsonLdNullable::as_ref),
285			context: self.context.as_deref(),
286			reverse: self.reverse.as_ref(),
287			index: self.index.as_ref(),
288			language: self
289				.language
290				.as_ref()
291				.map(|n| n.as_ref().map(JsonLdLenient::as_deref)),
292			direction: self.direction,
293			container: self.container.as_ref(),
294			nest: self.nest.as_ref(),
295			prefix: self.prefix,
296			propagate: self.propagate,
297			protected: self.protected,
298		}
299	}
300}
301
302/// Expanded term definition.
303#[derive(Default, Debug)]
304pub struct JsonLdExpandedTermDefinitionRef<'a> {
305	pub id: Option<JsonLdNullable<JsonLdIdRef<'a>>>,
306	pub type_: Option<JsonLdNullable<&'a JsonLdTermType>>,
307	pub context: Option<&'a JsonLdContext>,
308	pub reverse: Option<&'a JsonLdContextTerm>,
309	pub index: Option<&'a JsonLdIndex>,
310	pub language: Option<JsonLdNullable<JsonLdLenient<&'a LangTag>>>,
311	pub direction: Option<JsonLdNullable<JsonLdDirection>>,
312	pub container: Option<&'a JsonLdContainerValue>,
313	pub nest: Option<&'a JsonLdNest>,
314	pub prefix: Option<bool>,
315	pub propagate: Option<bool>,
316	pub protected: Option<bool>,
317}
318
319impl<'a> From<JsonLdNullable<&'a JsonLdTermDefinition>> for JsonLdExpandedTermDefinitionRef<'a> {
320	fn from(value: JsonLdNullable<&'a JsonLdTermDefinition>) -> Self {
321		match value {
322			JsonLdNullable::Some(d) => d.as_expanded(),
323			JsonLdNullable::Null => Self {
324				id: Some(JsonLdNullable::Null),
325				..Default::default()
326			},
327		}
328	}
329}
330
331/// Term definition entries.
332pub struct JsonLdTermDefinitionEntries<'a> {
333	id: Option<JsonLdNullable<&'a JsonLdTermId>>,
334	type_: Option<JsonLdNullable<&'a JsonLdTermType>>,
335	context: Option<&'a context::JsonLdContext>,
336	reverse: Option<&'a JsonLdContextTerm>,
337	index: Option<&'a JsonLdIndex>,
338	language: Option<JsonLdNullable<&'a JsonLdLenient<LangTagBuf>>>,
339	direction: Option<JsonLdNullable<JsonLdDirection>>,
340	container: Option<&'a JsonLdContainerValue>,
341	nest: Option<&'a JsonLdNest>,
342	prefix: Option<bool>,
343	propagate: Option<bool>,
344	protected: Option<bool>,
345}
346
347pub enum JsonLdTermDefinitionEntryRef<'a> {
348	Id(JsonLdNullable<&'a JsonLdTermId>),
349	Type(JsonLdNullable<&'a JsonLdTermType>),
350	Context(&'a context::JsonLdContext),
351	Reverse(&'a JsonLdContextTerm),
352	Index(&'a JsonLdIndex),
353	Language(JsonLdNullable<&'a JsonLdLenient<LangTagBuf>>),
354	Direction(JsonLdNullable<JsonLdDirection>),
355	Container(&'a JsonLdContainerValue),
356	Nest(&'a JsonLdNest),
357	Prefix(bool),
358	Propagate(bool),
359	Protected(bool),
360}
361
362impl<'a> JsonLdTermDefinitionEntryRef<'a> {
363	pub fn into_key(self) -> JsonLdTermDefinitionEntryKey {
364		match self {
365			Self::Id(_) => JsonLdTermDefinitionEntryKey::Id,
366			Self::Type(_) => JsonLdTermDefinitionEntryKey::Type,
367			Self::Context(_) => JsonLdTermDefinitionEntryKey::Context,
368			Self::Reverse(_) => JsonLdTermDefinitionEntryKey::Reverse,
369			Self::Index(_) => JsonLdTermDefinitionEntryKey::Index,
370			Self::Language(_) => JsonLdTermDefinitionEntryKey::Language,
371			Self::Direction(_) => JsonLdTermDefinitionEntryKey::Direction,
372			Self::Container(_) => JsonLdTermDefinitionEntryKey::Container,
373			Self::Nest(_) => JsonLdTermDefinitionEntryKey::Nest,
374			Self::Prefix(_) => JsonLdTermDefinitionEntryKey::Prefix,
375			Self::Propagate(_) => JsonLdTermDefinitionEntryKey::Propagate,
376			Self::Protected(_) => JsonLdTermDefinitionEntryKey::Protected,
377		}
378	}
379
380	pub fn key(&self) -> JsonLdTermDefinitionEntryKey {
381		match self {
382			Self::Id(_) => JsonLdTermDefinitionEntryKey::Id,
383			Self::Type(_) => JsonLdTermDefinitionEntryKey::Type,
384			Self::Context(_) => JsonLdTermDefinitionEntryKey::Context,
385			Self::Reverse(_) => JsonLdTermDefinitionEntryKey::Reverse,
386			Self::Index(_) => JsonLdTermDefinitionEntryKey::Index,
387			Self::Language(_) => JsonLdTermDefinitionEntryKey::Language,
388			Self::Direction(_) => JsonLdTermDefinitionEntryKey::Direction,
389			Self::Container(_) => JsonLdTermDefinitionEntryKey::Container,
390			Self::Nest(_) => JsonLdTermDefinitionEntryKey::Nest,
391			Self::Prefix(_) => JsonLdTermDefinitionEntryKey::Prefix,
392			Self::Propagate(_) => JsonLdTermDefinitionEntryKey::Propagate,
393			Self::Protected(_) => JsonLdTermDefinitionEntryKey::Protected,
394		}
395	}
396
397	pub fn into_value(self) -> JsonLdTermDefinitionEntryValueRef<'a> {
398		self.value()
399	}
400
401	pub fn value(&self) -> JsonLdTermDefinitionEntryValueRef<'a> {
402		match self {
403			Self::Id(e) => JsonLdTermDefinitionEntryValueRef::Id(*e),
404			Self::Type(e) => JsonLdTermDefinitionEntryValueRef::Type(*e),
405			Self::Context(e) => JsonLdTermDefinitionEntryValueRef::Context(e),
406			Self::Reverse(e) => JsonLdTermDefinitionEntryValueRef::Reverse(e),
407			Self::Index(e) => JsonLdTermDefinitionEntryValueRef::Index(e),
408			Self::Language(e) => JsonLdTermDefinitionEntryValueRef::Language(*e),
409			Self::Direction(e) => JsonLdTermDefinitionEntryValueRef::Direction(*e),
410			Self::Container(e) => JsonLdTermDefinitionEntryValueRef::Container(e),
411			Self::Nest(e) => JsonLdTermDefinitionEntryValueRef::Nest(e),
412			Self::Prefix(e) => JsonLdTermDefinitionEntryValueRef::Prefix(*e),
413			Self::Propagate(e) => JsonLdTermDefinitionEntryValueRef::Propagate(*e),
414			Self::Protected(e) => JsonLdTermDefinitionEntryValueRef::Protected(*e),
415		}
416	}
417
418	pub fn into_key_value(
419		self,
420	) -> (
421		JsonLdTermDefinitionEntryKey,
422		JsonLdTermDefinitionEntryValueRef<'a>,
423	) {
424		self.key_value()
425	}
426
427	pub fn key_value(
428		&self,
429	) -> (
430		JsonLdTermDefinitionEntryKey,
431		JsonLdTermDefinitionEntryValueRef<'a>,
432	) {
433		match self {
434			Self::Id(e) => (
435				JsonLdTermDefinitionEntryKey::Id,
436				JsonLdTermDefinitionEntryValueRef::Id(*e),
437			),
438			Self::Type(e) => (
439				JsonLdTermDefinitionEntryKey::Type,
440				JsonLdTermDefinitionEntryValueRef::Type(*e),
441			),
442			Self::Context(e) => (
443				JsonLdTermDefinitionEntryKey::Context,
444				JsonLdTermDefinitionEntryValueRef::Context(e),
445			),
446			Self::Reverse(e) => (
447				JsonLdTermDefinitionEntryKey::Reverse,
448				JsonLdTermDefinitionEntryValueRef::Reverse(e),
449			),
450			Self::Index(e) => (
451				JsonLdTermDefinitionEntryKey::Index,
452				JsonLdTermDefinitionEntryValueRef::Index(e),
453			),
454			Self::Language(e) => (
455				JsonLdTermDefinitionEntryKey::Language,
456				JsonLdTermDefinitionEntryValueRef::Language(*e),
457			),
458			Self::Direction(e) => (
459				JsonLdTermDefinitionEntryKey::Direction,
460				JsonLdTermDefinitionEntryValueRef::Direction(*e),
461			),
462			Self::Container(e) => (
463				JsonLdTermDefinitionEntryKey::Container,
464				JsonLdTermDefinitionEntryValueRef::Container(e),
465			),
466			Self::Nest(e) => (
467				JsonLdTermDefinitionEntryKey::Nest,
468				JsonLdTermDefinitionEntryValueRef::Nest(e),
469			),
470			Self::Prefix(e) => (
471				JsonLdTermDefinitionEntryKey::Prefix,
472				JsonLdTermDefinitionEntryValueRef::Prefix(*e),
473			),
474			Self::Propagate(e) => (
475				JsonLdTermDefinitionEntryKey::Propagate,
476				JsonLdTermDefinitionEntryValueRef::Propagate(*e),
477			),
478			Self::Protected(e) => (
479				JsonLdTermDefinitionEntryKey::Protected,
480				JsonLdTermDefinitionEntryValueRef::Protected(*e),
481			),
482		}
483	}
484}
485
486pub enum JsonLdTermDefinitionEntryKey {
487	Id,
488	Type,
489	Context,
490	Reverse,
491	Index,
492	Language,
493	Direction,
494	Container,
495	Nest,
496	Prefix,
497	Propagate,
498	Protected,
499}
500
501impl JsonLdTermDefinitionEntryKey {
502	pub fn keyword(&self) -> JsonLdKeyword {
503		match self {
504			Self::Id => JsonLdKeyword::Id,
505			Self::Type => JsonLdKeyword::Type,
506			Self::Context => JsonLdKeyword::Context,
507			Self::Reverse => JsonLdKeyword::Reverse,
508			Self::Index => JsonLdKeyword::Index,
509			Self::Language => JsonLdKeyword::Language,
510			Self::Direction => JsonLdKeyword::Direction,
511			Self::Container => JsonLdKeyword::Container,
512			Self::Nest => JsonLdKeyword::Nest,
513			Self::Prefix => JsonLdKeyword::Prefix,
514			Self::Propagate => JsonLdKeyword::Propagate,
515			Self::Protected => JsonLdKeyword::Protected,
516		}
517	}
518
519	pub fn as_str(&self) -> &'static str {
520		self.keyword().into_str()
521	}
522}
523
524pub enum JsonLdTermDefinitionEntryValueRef<'a> {
525	Id(JsonLdNullable<&'a JsonLdTermId>),
526	Type(JsonLdNullable<&'a JsonLdTermType>),
527	Context(&'a context::JsonLdContext),
528	Reverse(&'a JsonLdContextTerm),
529	Index(&'a JsonLdIndex),
530	Language(JsonLdNullable<&'a JsonLdLenient<LangTagBuf>>),
531	Direction(JsonLdNullable<JsonLdDirection>),
532	Container(&'a JsonLdContainerValue),
533	Nest(&'a JsonLdNest),
534	Prefix(bool),
535	Propagate(bool),
536	Protected(bool),
537}
538
539impl<'a> Iterator for JsonLdTermDefinitionEntries<'a> {
540	type Item = JsonLdTermDefinitionEntryRef<'a>;
541
542	fn size_hint(&self) -> (usize, Option<usize>) {
543		let mut len = 0;
544
545		if self.id.is_some() {
546			len += 1
547		}
548
549		if self.type_.is_some() {
550			len += 1
551		}
552
553		if self.context.is_some() {
554			len += 1
555		}
556
557		if self.reverse.is_some() {
558			len += 1
559		}
560
561		if self.index.is_some() {
562			len += 1
563		}
564
565		if self.language.is_some() {
566			len += 1
567		}
568
569		if self.direction.is_some() {
570			len += 1
571		}
572
573		if self.container.is_some() {
574			len += 1
575		}
576
577		if self.nest.is_some() {
578			len += 1
579		}
580
581		if self.prefix.is_some() {
582			len += 1
583		}
584
585		if self.propagate.is_some() {
586			len += 1
587		}
588
589		if self.protected.is_some() {
590			len += 1
591		}
592
593		(len, Some(len))
594	}
595
596	fn next(&mut self) -> Option<Self::Item> {
597		match self.id.take() {
598			Some(value) => Some(JsonLdTermDefinitionEntryRef::Id(value)),
599			None => match self.type_.take() {
600				Some(value) => Some(JsonLdTermDefinitionEntryRef::Type(value)),
601				None => match self.context.take() {
602					Some(value) => Some(JsonLdTermDefinitionEntryRef::Context(value)),
603					None => match self.reverse.take() {
604						Some(value) => Some(JsonLdTermDefinitionEntryRef::Reverse(value)),
605						None => match self.index.take() {
606							Some(value) => Some(JsonLdTermDefinitionEntryRef::Index(value)),
607							None => match self.language.take() {
608								Some(value) => Some(JsonLdTermDefinitionEntryRef::Language(value)),
609								None => match self.direction.take() {
610									Some(value) => {
611										Some(JsonLdTermDefinitionEntryRef::Direction(value))
612									}
613									None => match self.container.take() {
614										Some(value) => {
615											Some(JsonLdTermDefinitionEntryRef::Container(value))
616										}
617										None => match self.nest.take() {
618											Some(value) => {
619												Some(JsonLdTermDefinitionEntryRef::Nest(value))
620											}
621											None => match self.prefix.take() {
622												Some(value) => Some(
623													JsonLdTermDefinitionEntryRef::Prefix(value),
624												),
625												None => match self.propagate.take() {
626													Some(value) => Some(
627														JsonLdTermDefinitionEntryRef::Propagate(
628															value,
629														),
630													),
631													None => self.protected.take().map(
632														JsonLdTermDefinitionEntryRef::Protected,
633													),
634												},
635											},
636										},
637									},
638								},
639							},
640						},
641					},
642				},
643			},
644		}
645	}
646}
647
648impl<'a> ExactSizeIterator for JsonLdTermDefinitionEntries<'a> {}