rdf_types/vocabulary/
iri.rs

1use iref::{Iri, IriBuf};
2
3use super::{EmbedIntoVocabulary, EmbeddedIntoVocabulary};
4
5/// IRI vocabulary.
6pub trait IriVocabulary {
7	type Iri;
8
9	/// Returns the IRI associated to the given IRI id.
10	fn iri<'i>(&'i self, id: &'i Self::Iri) -> Option<&'i Iri>;
11
12	/// Returns a copy of the IRI associated to the given IRI id.
13	fn owned_iri(&self, id: Self::Iri) -> Result<IriBuf, Self::Iri> {
14		self.iri(&id).map(Iri::to_owned).ok_or(id)
15	}
16
17	/// Returns the id of the given IRI, if any.
18	fn get(&self, iri: &Iri) -> Option<Self::Iri>;
19}
20
21impl<V: IriVocabulary> IriVocabulary for &V {
22	type Iri = V::Iri;
23
24	fn iri<'i>(&'i self, id: &'i Self::Iri) -> Option<&'i Iri> {
25		V::iri(*self, id)
26	}
27
28	fn owned_iri(&self, id: Self::Iri) -> Result<IriBuf, Self::Iri> {
29		V::owned_iri(*self, id)
30	}
31
32	fn get(&self, iri: &Iri) -> Option<Self::Iri> {
33		V::get(*self, iri)
34	}
35}
36
37impl<V: IriVocabulary> IriVocabulary for &mut V {
38	type Iri = V::Iri;
39
40	fn iri<'i>(&'i self, id: &'i Self::Iri) -> Option<&'i Iri> {
41		V::iri(*self, id)
42	}
43
44	fn owned_iri(&self, id: Self::Iri) -> Result<IriBuf, Self::Iri> {
45		V::owned_iri(*self, id)
46	}
47
48	fn get(&self, iri: &Iri) -> Option<Self::Iri> {
49		V::get(*self, iri)
50	}
51}
52
53/// Mutable IRI vocabulary.
54pub trait IriVocabularyMut: IriVocabulary {
55	/// Inserts an IRI to the vocabulary and returns its id.
56	///
57	/// If the IRI was already present in the vocabulary, no new id is created
58	/// and the current one is returned.
59	fn insert(&mut self, iri: &Iri) -> Self::Iri;
60
61	fn insert_owned(&mut self, iri: IriBuf) -> Self::Iri {
62		self.insert(iri.as_iri())
63	}
64}
65
66impl<V: IriVocabularyMut> IriVocabularyMut for &mut V {
67	fn insert(&mut self, iri: &Iri) -> Self::Iri {
68		V::insert(*self, iri)
69	}
70
71	fn insert_owned(&mut self, iri: IriBuf) -> Self::Iri {
72		V::insert_owned(*self, iri)
73	}
74}
75
76impl<V: IriVocabularyMut> EmbedIntoVocabulary<V> for &Iri {
77	type Embedded = V::Iri;
78
79	fn embed_into_vocabulary(self, vocabulary: &mut V) -> Self::Embedded {
80		vocabulary.insert(self)
81	}
82}
83
84impl<V: IriVocabularyMut> EmbedIntoVocabulary<V> for IriBuf {
85	type Embedded = V::Iri;
86
87	fn embed_into_vocabulary(self, vocabulary: &mut V) -> Self::Embedded {
88		vocabulary.insert_owned(self)
89	}
90}
91
92impl<V: IriVocabularyMut> EmbeddedIntoVocabulary<V> for &Iri {
93	type Embedded = V::Iri;
94
95	fn embedded_into_vocabulary(&self, vocabulary: &mut V) -> Self::Embedded {
96		vocabulary.insert(self)
97	}
98}
99
100impl<V: IriVocabularyMut> EmbeddedIntoVocabulary<V> for IriBuf {
101	type Embedded = V::Iri;
102
103	fn embedded_into_vocabulary(&self, vocabulary: &mut V) -> Self::Embedded {
104		vocabulary.insert(self.as_iri())
105	}
106}