jsonld-core 0.23.3

JSON-LD core types and traits
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
use super::{IntoSyntax, Nest};
use crate::{Container, Direction, HashMap, LenientLangTagBuf, Nullable, Term, Type, ValidId as Id};
use contextual::WithContext;
use iri_rs::IriBuf;
use jsonld_syntax::{
    KeywordType,
    context::{
        definition::{Key, TypeContainer},
        term_definition::Index,
    },
};
use rdfx::{
    BlankIdBuf,
    vocabulary::{IriVocabulary, Vocabulary},
};
use std::{borrow::Borrow, fmt, hash::Hash, sync::Arc};

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

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

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

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

/// Term a context binding defines.
pub enum BindingTerm<'a> {
    /// An ordinary term.
    Normal(&'a Key),
    /// The `@type` entry of the context.
    Type,
}

impl<'a> BindingTerm<'a> {
    /// Returns this value as a string slice.
    #[must_use]
    pub fn as_str(&self) -> &'a str {
        match self {
            Self::Normal(key) => key.as_str(),
            Self::Type => "@type",
        }
    }
}

impl fmt::Display for BindingTerm<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl<'a, T, B> BindingRef<'a, T, B> {
    /// Returns a reference to the bound term.
    #[must_use]
    pub fn term(&self) -> BindingTerm<'a> {
        match self {
            Self::Normal(key, _) => BindingTerm::Normal(key),
            Self::Type(_) => BindingTerm::Type,
        }
    }

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

/// Term definitions of a context, split from its `@type` definition.
pub type DefinitionParts<T, B> = (HashMap<Key, NormalTermDefinition<T, B>>, Option<TypeTermDefinition>);

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

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

impl<T, B> Definitions<T, B> {
    /// Consumes this `Definitions`, returning its parts.
    #[must_use]
    pub fn into_parts(self) -> DefinitionParts<T, B> {
        (self.normal, self.type_)
    }

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

    /// Checks if no terms are defined.
    #[must_use]
    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>(&self, term: &Q) -> Option<TermDefinitionRef<'_, T, B>>
    where
        Q: ?Sized + 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>(&self, term: &Q) -> Option<&NormalTermDefinition<T, B>>
    where
        Q: ?Sized + Hash + Eq,
        Key: Borrow<Q>,
    {
        self.normal.get(term)
    }

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

    /// Checks whether the given term is defined.
    pub fn contains_term<Q>(&self, term: &Q) -> bool
    where
        Q: ?Sized + 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>) -> Option<TermDefinition<T, B>> {
        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>) -> Option<NormalTermDefinition<T, B>> {
        self.normal.insert(term, definition)
    }

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

    /// Sets the given `term` normal definition.
    pub fn set_normal(&mut self, term: Key, definition: Option<NormalTermDefinition<T, B>>) -> Option<NormalTermDefinition<T, B>> {
        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.
    #[must_use]
    pub fn iter(&self) -> Iter<'_, T, B> {
        Iter {
            type_: self.type_.as_ref(),
            normal: self.normal.iter(),
        }
    }

    /// Rewrites every IRI and identifier of this definition with the given
    /// functions.
    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>) -> Definitions<U, C>
    where
        T: Clone,
        B: Clone,
    {
        Definitions {
            normal: self.normal.into_iter().map(|(key, d)| (key, d.map_ids(&mut map_iri, &mut map_id))).collect(),
            type_: self.type_,
        }
    }
}

/// Iterator over the bindings of a context definition.
pub struct Iter<'a, T, B> {
    type_: Option<&'a TypeTermDefinition>,
    normal: hashbrown::hash_map::Iter<'a, Key, NormalTermDefinition<T, B>>,
}

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

    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> IntoIterator for &'a Definitions<T, B> {
    type Item = BindingRef<'a, T, B>;
    type IntoIter = Iter<'a, T, B>;

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

/// Owning iterator over the bindings of a context definition.
pub struct IntoIter<T, B> {
    type_: Option<TypeTermDefinition>,
    normal: hashbrown::hash_map::IntoIter<Key, NormalTermDefinition<T, B>>,
}

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

    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> IntoIterator for Definitions<T, B> {
    type Item = Binding<T, B>;
    type IntoIter = IntoIter<T, B>;

    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, 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 {
    /// Wraps this definition so that comparisons ignore the `@protected`
    /// flag, as required when checking whether a redefinition of a protected
    /// term actually changes it.
    #[must_use]
    pub fn modulo_protected_field(&self) -> ModuloProtected<&Self> {
        ModuloProtected(self)
    }

    /// Consumes this `TypeTermDefinition`, returning its syntax definition.
    #[must_use]
    pub fn into_syntax_definition(self) -> jsonld_syntax::context::definition::Type {
        jsonld_syntax::context::definition::Type {
            container: self.container,
            protected: if self.protected { Some(true) } else { None },
        }
    }
}

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

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

impl<T, B> TermDefinition<T, B> {
    /// Borrows this `TermDefinition`.
    pub fn as_ref(&self) -> TermDefinitionRef<'_, T, B> {
        match self {
            Self::Type(t) => TermDefinitionRef::Type(t),
            Self::Normal(n) => TermDefinitionRef::Normal(n),
        }
    }

    /// Wraps this definition so that comparisons ignore the `@protected`
    /// flag, as required when checking whether a redefinition of a protected
    /// term actually changes it.
    pub fn modulo_protected_field(&self) -> ModuloProtected<TermDefinitionRef<'_, T, B>> {
        ModuloProtected(self.as_ref())
    }

    /// Returns the term this definition maps to, if any (always `None` for
    /// `@type` definitions).
    pub fn value(&self) -> Option<&Term<T, B>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.value.as_deref(),
        }
    }

    /// Checks whether the term may be used to expand compact IRIs.
    pub fn prefix(&self) -> bool {
        match self {
            Self::Type(_) => false,
            Self::Normal(d) => d.prefix,
        }
    }

    /// Checks whether the term is protected against redefinition.
    pub fn protected(&self) -> bool {
        match self {
            Self::Type(d) => d.protected,
            Self::Normal(d) => d.protected,
        }
    }

    /// Checks whether the term denotes a reverse property.
    pub fn reverse_property(&self) -> bool {
        match self {
            Self::Type(_) => false,
            Self::Normal(d) => d.reverse_property,
        }
    }

    /// Returns the base URL used to resolve relative IRIs of the term.
    pub fn base_url(&self) -> Option<&T> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.base_url.as_ref(),
        }
    }

    /// Returns the local context of the term (its `@context` entry), if any.
    pub fn context(&self) -> Option<&jsonld_syntax::context::Context> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.context.as_deref(),
        }
    }

    /// Returns the container mapping of the term.
    pub fn container(&self) -> Container {
        match self {
            Self::Type(d) => d.container.into(),
            Self::Normal(d) => d.container,
        }
    }

    /// Returns the base direction of the term's string values.
    pub fn direction(&self) -> Option<Nullable<Direction>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.direction,
        }
    }

    /// Returns the index mapping of the term.
    pub fn index(&self) -> Option<&Index> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.index.as_ref(),
        }
    }

    /// Returns the default language of the term's string values.
    pub fn language(&self) -> Option<Nullable<&LenientLangTagBuf>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.language.as_ref().map(Nullable::as_ref),
        }
    }

    /// Returns the nesting term the property is gathered under.
    pub fn nest(&self) -> Option<&Nest> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.nest.as_ref(),
        }
    }

    /// Returns the type mapping of the term.
    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)]
pub enum TermDefinitionRef<'a, T = IriBuf, B = BlankIdBuf> {
    /// `@type` definition.
    Type(&'a TypeTermDefinition),

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

impl<'a, T, B> TermDefinitionRef<'a, T, B> {
    /// Wraps this definition so that comparisons ignore the `@protected`
    /// flag, as required when checking whether a redefinition of a protected
    /// term actually changes it.
    #[must_use]
    pub fn modulo_protected_field(&self) -> ModuloProtected<Self> {
        ModuloProtected(*self)
    }

    /// Returns the term this definition maps to, if any (always `None` for
    /// `@type` definitions).
    #[must_use]
    pub fn value(&self) -> Option<&'a Term<T, B>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.value.as_deref(),
        }
    }

    /// Cheap-clonable handle to the IRI mapping, if present and the
    /// definition is a normal term definition.
    #[must_use]
    pub fn value_arc(&self) -> Option<&'a Arc<Term<T, B>>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.value.as_ref(),
        }
    }

    /// Checks whether the term may be used to expand compact IRIs.
    #[must_use]
    pub fn prefix(&self) -> bool {
        match self {
            Self::Type(_) => false,
            Self::Normal(d) => d.prefix,
        }
    }

    /// Checks whether the term is protected against redefinition.
    #[must_use]
    pub fn protected(&self) -> bool {
        match self {
            Self::Type(d) => d.protected,
            Self::Normal(d) => d.protected,
        }
    }

    /// Checks whether the term denotes a reverse property.
    #[must_use]
    pub fn reverse_property(&self) -> bool {
        match self {
            Self::Type(_) => false,
            Self::Normal(d) => d.reverse_property,
        }
    }

    /// Returns the base URL used to resolve relative IRIs of the term.
    #[must_use]
    pub fn base_url(&self) -> Option<&'a T> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.base_url.as_ref(),
        }
    }

    /// Returns the local context of the term (its `@context` entry), if any.
    #[must_use]
    pub fn context(&self) -> Option<&'a jsonld_syntax::context::Context> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.context.as_deref(),
        }
    }

    /// Returns the container mapping of the term.
    #[must_use]
    pub fn container(&self) -> Container {
        match self {
            Self::Type(d) => d.container.into(),
            Self::Normal(d) => d.container,
        }
    }

    /// Returns the base direction of the term's string values.
    #[must_use]
    pub fn direction(&self) -> Option<Nullable<Direction>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.direction,
        }
    }

    /// Returns the index mapping of the term.
    #[must_use]
    pub fn index(&self) -> Option<&'a Index> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.index.as_ref(),
        }
    }

    /// Returns the default language of the term's string values.
    pub fn language(&self) -> Option<Nullable<&'a LenientLangTagBuf>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.language.as_ref().map(Nullable::as_ref),
        }
    }

    /// Returns the nesting term the property is gathered under.
    #[must_use]
    pub fn nest(&self) -> Option<&'a Nest> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.nest.as_ref(),
        }
    }

    /// Returns the type mapping of the term.
    #[must_use]
    pub fn typ(&self) -> Option<&'a Type<T>> {
        match self {
            Self::Type(_) => None,
            Self::Normal(d) => d.typ.as_ref(),
        }
    }
}

impl<T, B> Clone for TermDefinitionRef<'_, T, B> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T, B> Copy for TermDefinitionRef<'_, T, B> {}

#[derive(PartialEq, Eq, Clone)]
/// Definition of an ordinary term, as processed from a context.
pub struct NormalTermDefinition<T = IriBuf, B = BlankIdBuf> {
    /// Term the definition maps to.
    ///
    /// Wrapped in `Arc` so the per-element-per-property hot path in
    /// `expand_iri_simple` returns a refcount bump instead of deep-cloning the
    /// (potentially `IriBuf`-backed) term. Prefer the `value()` /
    /// `value_arc()` accessors over direct field access.
    pub value: Option<Arc<Term<T, B>>>,

    /// Whether the term may expand compact IRIs.
    pub prefix: bool,

    /// Whether the term is protected against redefinition.
    pub protected: bool,

    /// Whether the term denotes a reverse property.
    pub reverse_property: bool,

    /// Base URL used to resolve relative IRIs of the term.
    pub base_url: Option<T>,

    /// Context local to the term.
    pub context: Option<Box<jsonld_syntax::context::Context>>,

    /// Container mapping of the term.
    pub container: Container,

    /// Base direction of the term's string values.
    pub direction: Option<Nullable<Direction>>,

    /// Index mapping of the term.
    pub index: Option<Index>,

    /// Default language of the term's string values.
    pub language: Option<Nullable<LenientLangTagBuf>>,

    /// Nesting term the property is gathered under.
    pub nest: Option<Nest>,

    /// Type mapping of the term.
    pub typ: Option<Type<T>>,
}

impl<T, B> NormalTermDefinition<T, B> {
    /// Wraps this definition so that comparisons ignore the `@protected`
    /// flag, as required when checking whether a redefinition of a protected
    /// term actually changes it.
    pub fn modulo_protected_field(&self) -> ModuloProtected<&Self> {
        ModuloProtected(self)
    }

    /// Returns the base URL used to resolve relative IRIs of the term.
    pub fn base_url(&self) -> Option<&T> {
        self.base_url.as_ref()
    }

    /// IRI mapping as a `Term` reference, transparent to the `Arc` wrapper.
    pub fn value(&self) -> Option<&Term<T, B>> {
        self.value.as_deref()
    }

    /// Cheap-clonable handle to the IRI mapping.
    pub fn value_arc(&self) -> Option<&Arc<Term<T, B>>> {
        self.value.as_ref()
    }

    /// Consumes this `NormalTermDefinition`, returning its syntax definition.
    ///
    /// # Errors
    ///
    /// Returns an error when a term in the context cannot be represented in syntax form, which happens if the vocabulary has no IRI for an identifier it holds.
    pub fn into_syntax_definition(
        self,
        vocabulary: &impl Vocabulary<Iri = T, BlankId = B>,
    ) -> Result<Nullable<jsonld_syntax::context::TermDefinition>, super::InvalidContextError>
    where
        T: Clone,
        B: Clone,
    {
        use jsonld_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>) -> Result<Key, super::InvalidContextKey> {
            match term {
                Term::Null => Err(super::InvalidContextKey),
                Term::Keyword(k) => Ok(k.to_string().into()),
                Term::Id(r) => Ok(r.with(vocabulary).to_string().into()),
            }
        }

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

        fn unwrap_term<T: Clone, B: Clone>(arc: Arc<Term<T, B>>) -> Term<T, B> {
            Arc::try_unwrap(arc).unwrap_or_else(|a| (*a).clone())
        }

        let (id, reverse) = if self.reverse_property {
            let reverse = self.value.map(|t| term_into_key(vocabulary, unwrap_term(t))).transpose()?;
            (None, reverse)
        } else {
            (self.value.map(|t| term_into_id(vocabulary, unwrap_term(t))), None)
        };

        let container = self.container.into_syntax();

        let context = match self.context {
            Some(e) => Some(Box::new(e.into_syntax(vocabulary)?)),
            None => None,
        };

        Ok(jsonld_syntax::context::term_definition::Expanded {
            id,
            type_: self.typ.map(|t| type_into_syntax(vocabulary, t).map(Nullable::Some)).transpose()?,
            context,
            reverse,
            index: self.index.clone(),
            language: self.language,
            direction: self.direction,
            container: container.map(Nullable::Some),
            nest: self.nest.clone(),
            prefix: if self.prefix { Some(true) } else { None },
            propagate: None,
            protected: if self.protected { Some(true) } else { None },
        }
        .simplify())
    }

    fn map_ids<U, C>(self, mut map_iri: impl FnMut(T) -> U, map_id: impl FnOnce(Id<T, B>) -> Id<U, C>) -> NormalTermDefinition<U, C>
    where
        T: Clone,
        B: Clone,
    {
        NormalTermDefinition {
            value: self.value.map(|t| {
                let term = Arc::try_unwrap(t).unwrap_or_else(|a| (*a).clone());
                Arc::new(term.map_id(map_id))
            }),
            prefix: self.prefix,
            protected: self.protected,
            reverse_property: self.reverse_property,
            base_url: self.base_url.map(&mut map_iri),
            context: self.context,
            container: self.container,
            direction: self.direction,
            index: self.index,
            language: self.language,
            nest: self.nest,
            typ: self.typ.map(|t| t.map(map_iri)),
        }
    }
}

impl<T, B> Default for NormalTermDefinition<T, B> {
    fn default() -> NormalTermDefinition<T, B> {
        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<'b, T: PartialEq, B: PartialEq> PartialEq<ModuloProtected<&'b NormalTermDefinition<T, B>>> for ModuloProtected<&NormalTermDefinition<T, B>> {
    fn eq(&self, other: &ModuloProtected<&'b NormalTermDefinition<T, B>>) -> 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 == other.0.nest
            && self.0.index == other.0.index
            && 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 == other.0.context
    }
}

impl<T: Eq, B: Eq> Eq for ModuloProtected<&NormalTermDefinition<T, B>> {}

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

impl Eq for ModuloProtected<&TypeTermDefinition> {}

impl<'b, T: PartialEq, B: PartialEq> PartialEq<ModuloProtected<TermDefinitionRef<'b, T, B>>> for ModuloProtected<TermDefinitionRef<'_, T, B>> {
    fn eq(&self, other: &ModuloProtected<TermDefinitionRef<'b, T, B>>) -> 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() == other.0.nest()
            && self.0.index() == other.0.index()
            && 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() == other.0.context()
    }
}

impl<T: Eq, B: Eq> Eq for ModuloProtected<TermDefinitionRef<'_, T, B>> {}