json-ld-core 0.12.1

A JSON-LD implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use super::{IntoSyntax, Nest};
use crate::{Container, Direction, LenientLanguageTagBuf, Nullable, Term, Type};
use contextual::WithContext;
use json_ld_syntax::{
	context::{
		definition::{Key, KeyOrTypeRef, KeyRef, TypeContainer},
		term_definition::Index,
	},
	Entry, KeywordType,
};
use locspan::{BorrowStripped, Meta, StrippedEq, StrippedPartialEq};
use locspan_derive::{StrippedEq, StrippedPartialEq};
use rdf_types::{IriVocabulary, Vocabulary};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::hash::Hash;

/// Term binding.
pub enum Binding<T, B, L, M> {
	/// Normal term definition.
	Normal(Key, NormalTermDefinition<T, B, L, M>),

	/// `@type` term definition.
	Type(TypeTermDefinition),
}

/// Term binding reference.
pub enum BindingRef<'a, T, B, L, M> {
	/// Normal term definition.
	Normal(&'a Key, &'a NormalTermDefinition<T, B, L, M>),

	/// `@type` term definition.
	Type(&'a TypeTermDefinition),
}

impl<'a, T, B, L, M> BindingRef<'a, T, B, L, M> {
	/// Returns a reference to the bound term.
	pub fn term(&self) -> KeyOrTypeRef<'a> {
		match self {
			Self::Normal(key, _) => KeyOrTypeRef::Key(KeyRef::from(*key)),
			Self::Type(_) => KeyOrTypeRef::Type,
		}
	}

	/// Returns a reference to the bound term definition.
	pub fn definition(&self) -> TermDefinitionRef<'a, T, B, L, M> {
		match self {
			Self::Normal(_, d) => TermDefinitionRef::Normal(d),
			Self::Type(d) => TermDefinitionRef::Type(d),
		}
	}
}

/// Context term definitions.
#[derive(Clone)]
pub struct Definitions<T, B, L, M> {
	normal: HashMap<Key, NormalTermDefinition<T, B, L, M>>,
	type_: Option<TypeTermDefinition>,
}

impl<T, B, L, M> Default for Definitions<T, B, L, M> {
	fn default() -> Self {
		Self {
			normal: HashMap::new(),
			type_: None,
		}
	}
}

impl<T, B, L, M> Definitions<T, B, L, M> {
	#[allow(clippy::type_complexity)]
	pub fn into_parts(
		self,
	) -> (
		HashMap<Key, NormalTermDefinition<T, B, L, M>>,
		Option<TypeTermDefinition>,
	) {
		(self.normal, self.type_)
	}

	/// Returns the number of defined terms.
	pub fn len(&self) -> usize {
		if self.type_.is_some() {
			self.normal.len() + 1
		} else {
			self.normal.len()
		}
	}

	/// Checks if no terms are defined.
	pub fn is_empty(&self) -> bool {
		self.type_.is_none() && self.normal.is_empty()
	}

	/// Returns a reference to the definition of the given `term`, if any.
	pub fn get<Q: ?Sized>(&self, term: &Q) -> Option<TermDefinitionRef<T, B, L, M>>
	where
		Q: Hash + Eq,
		Key: Borrow<Q>,
		KeywordType: Borrow<Q>,
	{
		if KeywordType.borrow() == term {
			self.type_.as_ref().map(TermDefinitionRef::Type)
		} else {
			self.normal.get(term).map(TermDefinitionRef::Normal)
		}
	}

	/// Returns a reference to the normal definition of the given `term`, if any.
	pub fn get_normal<Q: ?Sized>(&self, term: &Q) -> Option<&NormalTermDefinition<T, B, L, M>>
	where
		Q: Hash + Eq,
		Key: Borrow<Q>,
	{
		self.normal.get(term)
	}

	/// Returns a reference to the `@type` definition, if any.
	pub fn get_type(&self) -> Option<&TypeTermDefinition> {
		self.type_.as_ref()
	}

	pub fn contains_term<Q: ?Sized>(&self, term: &Q) -> bool
	where
		Q: Hash + Eq,
		Key: Borrow<Q>,
		KeywordType: Borrow<Q>,
	{
		if KeywordType.borrow() == term {
			self.type_.is_some()
		} else {
			self.normal.contains_key(term)
		}
	}

	/// Inserts the given `binding`.
	pub fn insert(&mut self, binding: Binding<T, B, L, M>) -> Option<TermDefinition<T, B, L, M>> {
		match binding {
			Binding::Normal(key, definition) => self
				.insert_normal(key, definition)
				.map(TermDefinition::Normal),
			Binding::Type(definition) => self.insert_type(definition).map(TermDefinition::Type),
		}
	}

	/// Defines the given normal term.
	pub fn insert_normal(
		&mut self,
		term: Key,
		definition: NormalTermDefinition<T, B, L, M>,
	) -> Option<NormalTermDefinition<T, B, L, M>> {
		self.normal.insert(term, definition)
	}

	/// Inserts the given `@type` definition.
	pub fn insert_type(&mut self, definition: TypeTermDefinition) -> Option<TypeTermDefinition> {
		std::mem::replace(&mut self.type_, Some(definition))
	}

	/// Sets the given `term` normal definition.
	pub fn set_normal(
		&mut self,
		term: Key,
		definition: Option<NormalTermDefinition<T, B, L, M>>,
	) -> Option<NormalTermDefinition<T, B, L, M>> {
		match definition {
			Some(d) => self.normal.insert(term, d),
			None => self.normal.remove(&term),
		}
	}

	/// Sets the given `@type` definition.
	pub fn set_type(
		&mut self,
		definition: Option<TypeTermDefinition>,
	) -> Option<TypeTermDefinition> {
		std::mem::replace(&mut self.type_, definition)
	}

	/// Returns an iterator over the term definitions.
	pub fn iter(&self) -> Iter<T, B, L, M> {
		Iter {
			type_: self.type_.as_ref(),
			normal: self.normal.iter(),
		}
	}
}

pub struct Iter<'a, T, B, L, M> {
	type_: Option<&'a TypeTermDefinition>,
	normal: std::collections::hash_map::Iter<'a, Key, NormalTermDefinition<T, B, L, M>>,
}

impl<'a, T, B, L, M> Iterator for Iter<'a, T, B, L, M> {
	type Item = BindingRef<'a, T, B, L, M>;

	fn next(&mut self) -> Option<Self::Item> {
		self.type_
			.take()
			.map(BindingRef::Type)
			.or_else(|| self.normal.next().map(|(k, d)| BindingRef::Normal(k, d)))
	}
}

impl<'a, T, B, L, M> IntoIterator for &'a Definitions<T, B, L, M> {
	type Item = BindingRef<'a, T, B, L, M>;
	type IntoIter = Iter<'a, T, B, L, M>;

	fn into_iter(self) -> Self::IntoIter {
		self.iter()
	}
}

pub struct IntoIter<T, B, L, M> {
	type_: Option<TypeTermDefinition>,
	normal: std::collections::hash_map::IntoIter<Key, NormalTermDefinition<T, B, L, M>>,
}

impl<T, B, L, M> Iterator for IntoIter<T, B, L, M> {
	type Item = Binding<T, B, L, M>;

	fn next(&mut self) -> Option<Self::Item> {
		self.type_
			.take()
			.map(Binding::Type)
			.or_else(|| self.normal.next().map(|(k, d)| Binding::Normal(k, d)))
	}
}

impl<T, B, L, M> IntoIterator for Definitions<T, B, L, M> {
	type Item = Binding<T, B, L, M>;
	type IntoIter = IntoIter<T, B, L, M>;

	fn into_iter(self) -> Self::IntoIter {
		IntoIter {
			type_: self.type_,
			normal: self.normal.into_iter(),
		}
	}
}

/// `@type` term definition.
///
/// Such definition compared to a [`NormalTermDefinition`] can only contain
/// a `@container` and `@protected` value.
#[derive(PartialEq, Eq, StrippedPartialEq, StrippedEq, Clone)]
pub struct TypeTermDefinition {
	/// Type container.
	pub container: TypeContainer,

	/// Protection flag.
	pub protected: bool,
}

impl Default for TypeTermDefinition {
	fn default() -> Self {
		Self {
			container: TypeContainer::Set,
			protected: false,
		}
	}
}

impl TypeTermDefinition {
	pub fn modulo_protected_field(&self) -> ModuloProtected<&Self> {
		ModuloProtected(self)
	}

	pub fn into_syntax_definition<M: Clone>(
		&self,
		meta: M,
	) -> Meta<json_ld_syntax::context::definition::Type<M>, M> {
		let def = json_ld_syntax::context::definition::Type {
			container: Entry::new(meta.clone(), Meta(self.container, meta.clone())),
			protected: if self.protected {
				Some(Entry::new(meta.clone(), Meta(true, meta.clone())))
			} else {
				None
			},
		};

		Meta(def, meta)
	}
}

/// Term definition.
#[derive(PartialEq, Eq, StrippedPartialEq, StrippedEq, Clone)]
#[locspan(stripped(T, B), fixed(T, B))]
pub enum TermDefinition<T, B, C, M> {
	/// `@type` term definition.
	Type(TypeTermDefinition),

	/// Normal term definition.
	Normal(NormalTermDefinition<T, B, C, M>),
}

impl<T, B, C, M> TermDefinition<T, B, C, M> {
	pub fn as_ref(&self) -> TermDefinitionRef<T, B, C, M> {
		match self {
			Self::Type(t) => TermDefinitionRef::Type(t),
			Self::Normal(n) => TermDefinitionRef::Normal(n),
		}
	}

	pub fn modulo_protected_field(&self) -> ModuloProtected<TermDefinitionRef<T, B, C, M>> {
		ModuloProtected(self.as_ref())
	}

	pub fn value(&self) -> Option<&Term<T, B>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.value.as_ref(),
		}
	}

	pub fn prefix(&self) -> bool {
		match self {
			Self::Type(_) => false,
			Self::Normal(d) => d.prefix,
		}
	}

	pub fn protected(&self) -> bool {
		match self {
			Self::Type(d) => d.protected,
			Self::Normal(d) => d.protected,
		}
	}

	pub fn reverse_property(&self) -> bool {
		match self {
			Self::Type(_) => false,
			Self::Normal(d) => d.reverse_property,
		}
	}

	pub fn base_url(&self) -> Option<&T> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.base_url.as_ref(),
		}
	}

	pub fn context(&self) -> Option<&Entry<C, M>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.context.as_ref(),
		}
	}

	pub fn container(&self) -> Container {
		match self {
			Self::Type(d) => d.container.into(),
			Self::Normal(d) => d.container,
		}
	}

	pub fn direction(&self) -> Option<Nullable<Direction>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.direction,
		}
	}

	pub fn index(&self) -> Option<&Entry<Index, M>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.index.as_ref(),
		}
	}

	pub fn language(&self) -> Option<&Nullable<LenientLanguageTagBuf>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.language.as_ref(),
		}
	}

	pub fn nest(&self) -> Option<&Entry<Nest, M>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.nest.as_ref(),
		}
	}

	pub fn typ(&self) -> Option<&Type<T>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.typ.as_ref(),
		}
	}
}

/// Term definition reference.
#[derive(PartialEq, Eq, StrippedPartialEq, StrippedEq)]
#[locspan(stripped(T, B), fixed(T, B))]
pub enum TermDefinitionRef<'a, T, B, C, M> {
	/// `@type` definition.
	Type(&'a TypeTermDefinition),

	/// Normal definition.
	Normal(&'a NormalTermDefinition<T, B, C, M>),
}

impl<'a, T, B, C, M> TermDefinitionRef<'a, T, B, C, M> {
	pub fn modulo_protected_field(&self) -> ModuloProtected<Self> {
		ModuloProtected(*self)
	}

	pub fn value(&self) -> Option<&'a Term<T, B>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.value.as_ref(),
		}
	}

	pub fn prefix(&self) -> bool {
		match self {
			Self::Type(_) => false,
			Self::Normal(d) => d.prefix,
		}
	}

	pub fn protected(&self) -> bool {
		match self {
			Self::Type(d) => d.protected,
			Self::Normal(d) => d.protected,
		}
	}

	pub fn reverse_property(&self) -> bool {
		match self {
			Self::Type(_) => false,
			Self::Normal(d) => d.reverse_property,
		}
	}

	pub fn base_url(&self) -> Option<&'a T> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.base_url.as_ref(),
		}
	}

	pub fn context(&self) -> Option<&'a Entry<C, M>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.context.as_ref(),
		}
	}

	pub fn container(&self) -> Container {
		match self {
			Self::Type(d) => d.container.into(),
			Self::Normal(d) => d.container,
		}
	}

	pub fn direction(&self) -> Option<Nullable<Direction>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.direction,
		}
	}

	pub fn index(&self) -> Option<&'a Entry<Index, M>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.index.as_ref(),
		}
	}

	pub fn language(&self) -> Option<&'a Nullable<LenientLanguageTagBuf>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.language.as_ref(),
		}
	}

	pub fn nest(&self) -> Option<&'a Entry<Nest, M>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.nest.as_ref(),
		}
	}

	pub fn typ(&self) -> Option<&'a Type<T>> {
		match self {
			Self::Type(_) => None,
			Self::Normal(d) => d.typ.as_ref(),
		}
	}
}

impl<'a, T, B, C, M> Clone for TermDefinitionRef<'a, T, B, C, M> {
	fn clone(&self) -> Self {
		match self {
			Self::Type(d) => Self::Type(d),
			Self::Normal(d) => Self::Normal(*d),
		}
	}
}

impl<'a, T, B, C, M> Copy for TermDefinitionRef<'a, T, B, C, M> {}

// A term definition.
#[derive(PartialEq, Eq, StrippedPartialEq, StrippedEq, Clone)]
#[locspan(stripped(T, B), fixed(T, B))]
pub struct NormalTermDefinition<T, B, C, M> {
	// IRI mapping.
	#[locspan(stripped)]
	pub value: Option<Term<T, B>>,

	// Prefix flag.
	#[locspan(stripped)]
	pub prefix: bool,

	// Protected flag.
	#[locspan(stripped)]
	pub protected: bool,

	// Reverse property flag.
	#[locspan(stripped)]
	pub reverse_property: bool,

	// Optional base URL.
	#[locspan(stripped)]
	pub base_url: Option<T>,

	// Optional context.
	pub context: Option<Entry<C, M>>,

	// Container mapping.
	#[locspan(stripped)]
	pub container: Container,

	// Optional direction mapping.
	#[locspan(stripped)]
	pub direction: Option<Nullable<Direction>>,

	// Optional index mapping.
	#[locspan(unwrap_deref2_stripped)]
	pub index: Option<Entry<Index, M>>,

	// Optional language mapping.
	#[locspan(stripped)]
	pub language: Option<Nullable<LenientLanguageTagBuf>>,

	// Optional nest value.
	#[locspan(unwrap_deref2_stripped)]
	pub nest: Option<Entry<Nest, M>>,

	// Optional type mapping.
	#[locspan(stripped)]
	pub typ: Option<Type<T>>,
}

impl<T, B, C, M> NormalTermDefinition<T, B, C, M> {
	pub fn modulo_protected_field(&self) -> ModuloProtected<&Self> {
		ModuloProtected(self)
	}

	pub fn base_url(&self) -> Option<&T> {
		self.base_url.as_ref()
	}

	pub fn into_syntax_definition(
		self,
		vocabulary: &impl Vocabulary<Iri = T, BlankId = B>,
		meta: M,
	) -> Meta<Nullable<json_ld_syntax::context::TermDefinition<M>>, M>
	where
		C: IntoSyntax<T, B, M>,
		M: Clone,
	{
		use json_ld_syntax::context::term_definition::{Id, Type as SyntaxType, TypeKeyword};

		fn term_into_id<T, B>(
			vocabulary: &impl Vocabulary<Iri = T, BlankId = B>,
			term: Term<T, B>,
		) -> Nullable<Id> {
			match term {
				Term::Null => Nullable::Null,
				Term::Keyword(k) => Nullable::Some(Id::Keyword(k)),
				Term::Id(r) => Nullable::Some(Id::Term(r.with(vocabulary).to_string())),
			}
		}

		fn term_into_key<T, B>(
			vocabulary: &impl Vocabulary<Iri = T, BlankId = B>,
			term: Term<T, B>,
		) -> Key {
			match term {
				Term::Null => panic!("invalid key"),
				Term::Keyword(k) => k.to_string().into(),
				Term::Id(r) => r.with(vocabulary).to_string().into(),
			}
		}

		fn type_into_syntax<T>(
			vocabulary: &impl IriVocabulary<Iri = T>,
			ty: Type<T>,
		) -> SyntaxType {
			match ty {
				Type::Id => SyntaxType::Keyword(TypeKeyword::Id),
				Type::Json => SyntaxType::Keyword(TypeKeyword::Json),
				Type::None => SyntaxType::Keyword(TypeKeyword::None),
				Type::Vocab => SyntaxType::Keyword(TypeKeyword::Vocab),
				Type::Iri(t) => SyntaxType::Term(vocabulary.iri(&t).unwrap().to_string()),
			}
		}

		let (id, reverse) = if self.reverse_property {
			(
				None,
				self.value.map(|t| {
					Entry::new(
						meta.clone(),
						Meta(term_into_key(vocabulary, t), meta.clone()),
					)
				}),
			)
		} else {
			(
				self.value.map(|t| {
					Entry::new(
						meta.clone(),
						Meta(term_into_id(vocabulary, t), meta.clone()),
					)
				}),
				None,
			)
		};

		let container = self.container.into_syntax(meta.clone());

		let expanded = json_ld_syntax::context::term_definition::Expanded {
			id,
			type_: self.typ.map(|t| {
				Entry::new(
					meta.clone(),
					Meta(
						Nullable::Some(type_into_syntax(vocabulary, t)),
						meta.clone(),
					),
				)
			}),
			context: self.context.map(|e| {
				Entry::new(
					e.key_metadata.clone(),
					Meta(
						Box::new(e.value.0.into_syntax(vocabulary, meta.clone())),
						e.value.1,
					),
				)
			}),
			reverse,
			index: self.index.clone(),
			language: self
				.language
				.map(|l| Entry::new(meta.clone(), Meta(l, meta.clone()))),
			direction: self
				.direction
				.map(|d| Entry::new(meta.clone(), Meta(d, meta.clone()))),
			container: container
				.map(|Meta(c, m)| Entry::new(meta.clone(), Meta(Nullable::Some(c), m))),
			nest: self.nest.clone(),
			prefix: if self.prefix {
				Some(Entry::new(meta.clone(), Meta(true, meta.clone())))
			} else {
				None
			},
			propagate: None,
			protected: if self.protected {
				Some(Entry::new(meta.clone(), Meta(true, meta.clone())))
			} else {
				None
			},
		};

		Meta(expanded.simplify(), meta)
	}
}

impl<T, B, C, M> Default for NormalTermDefinition<T, B, C, M> {
	fn default() -> NormalTermDefinition<T, B, C, M> {
		NormalTermDefinition {
			value: None,
			prefix: false,
			protected: false,
			reverse_property: false,
			base_url: None,
			typ: None,
			language: None,
			direction: None,
			context: None,
			nest: None,
			index: None,
			container: Container::new(),
		}
	}
}

/// Wrapper to consider a term definition without the `@protected` flag.
pub struct ModuloProtected<T>(T);

impl<'a, 'b, T: PartialEq, B: PartialEq, C: StrippedPartialEq<E>, M, E, N>
	StrippedPartialEq<ModuloProtected<&'b NormalTermDefinition<T, B, E, N>>>
	for ModuloProtected<&'a NormalTermDefinition<T, B, C, M>>
{
	fn stripped_eq(&self, other: &ModuloProtected<&'b NormalTermDefinition<T, B, E, N>>) -> bool {
		// NOTE we ignore the `protected` flag.
		self.0.prefix == other.0.prefix
			&& self.0.reverse_property == other.0.reverse_property
			&& self.0.language == other.0.language
			&& self.0.direction == other.0.direction
			&& self.0.nest.stripped() == other.0.nest.stripped()
			&& self.0.index.stripped() == other.0.index.stripped()
			&& self.0.container == other.0.container
			&& self.0.base_url == other.0.base_url
			&& self.0.value == other.0.value
			&& self.0.typ == other.0.typ
			&& self.0.context.stripped() == other.0.context.stripped()
	}
}

impl<'a, T: Eq, B: Eq, C: StrippedEq, M> StrippedEq
	for ModuloProtected<&'a NormalTermDefinition<T, B, C, M>>
{
}

impl<'a, 'b> StrippedPartialEq<ModuloProtected<&'b TypeTermDefinition>>
	for ModuloProtected<&'a TypeTermDefinition>
{
	fn stripped_eq(&self, other: &ModuloProtected<&'b TypeTermDefinition>) -> bool {
		// NOTE we ignore the `protected` flag.
		self.0.container == other.0.container
	}
}

impl<'a> StrippedEq for ModuloProtected<&'a TypeTermDefinition> {}

impl<'a, 'b, T: PartialEq, B: PartialEq, C: StrippedPartialEq, M>
	StrippedPartialEq<ModuloProtected<TermDefinitionRef<'b, T, B, C, M>>>
	for ModuloProtected<TermDefinitionRef<'a, T, B, C, M>>
{
	fn stripped_eq(&self, other: &ModuloProtected<TermDefinitionRef<'b, T, B, C, M>>) -> bool {
		// NOTE we ignore the `protected` flag.
		self.0.prefix() == other.0.prefix()
			&& self.0.reverse_property() == other.0.reverse_property()
			&& self.0.language() == other.0.language()
			&& self.0.direction() == other.0.direction()
			&& self.0.nest().stripped() == other.0.nest().stripped()
			&& self.0.index().stripped() == other.0.index().stripped()
			&& self.0.container() == other.0.container()
			&& self.0.base_url() == other.0.base_url()
			&& self.0.value() == other.0.value()
			&& self.0.typ() == other.0.typ()
			&& self.0.context().stripped() == other.0.context().stripped()
	}
}

impl<'a, T: Eq, B: Eq, C: StrippedEq, M> StrippedEq
	for ModuloProtected<TermDefinitionRef<'a, T, B, C, M>>
{
}