rdf_types/interpretation/
iri.rs

1use iref::{Iri, IriBuf};
2
3use crate::vocabulary::{IriVocabulary, IriVocabularyMut};
4
5use super::Interpretation;
6
7/// IRI Interpretation.
8pub trait IriInterpretation<I: ?Sized>: Interpretation {
9	/// Returns the interpretation of the given IRI, if any.
10	fn iri_interpretation(&self, iri: &I) -> Option<Self::Resource>;
11
12	fn lexical_iri_interpretation(
13		&self,
14		vocabulary: &impl IriVocabulary<Iri = I>,
15		iri: &Iri,
16	) -> Option<Self::Resource>
17	where
18		I: Sized,
19	{
20		vocabulary
21			.get(iri)
22			.and_then(|iri| self.iri_interpretation(&iri))
23	}
24}
25
26impl<I, T: IriInterpretation<I>> IriInterpretation<I> for &T {
27	fn iri_interpretation(&self, iri: &I) -> Option<Self::Resource> {
28		T::iri_interpretation(*self, iri)
29	}
30}
31
32impl<I, T: IriInterpretation<I>> IriInterpretation<I> for &mut T {
33	fn iri_interpretation(&self, iri: &I) -> Option<Self::Resource> {
34		T::iri_interpretation(*self, iri)
35	}
36}
37
38/// Mutable IRI interpretation.
39pub trait IriInterpretationMut<I = IriBuf>: Interpretation {
40	/// Interprets the given IRI.
41	fn interpret_iri(&mut self, iri: I) -> Self::Resource;
42
43	fn interpret_lexical_iri(
44		&mut self,
45		vocabulary: &mut impl IriVocabularyMut<Iri = I>,
46		iri: &Iri,
47	) -> Self::Resource {
48		self.interpret_iri(vocabulary.insert(iri))
49	}
50
51	fn interpret_owned_lexical_iri(
52		&mut self,
53		vocabulary: &mut impl IriVocabularyMut<Iri = I>,
54		iri: IriBuf,
55	) -> Self::Resource {
56		self.interpret_iri(vocabulary.insert_owned(iri))
57	}
58}
59
60impl<I, T: IriInterpretationMut<I>> IriInterpretationMut<I> for &mut T {
61	fn interpret_iri(&mut self, iri: I) -> Self::Resource {
62		T::interpret_iri(*self, iri)
63	}
64
65	fn interpret_lexical_iri(
66		&mut self,
67		vocabulary: &mut impl IriVocabularyMut<Iri = I>,
68		iri: &Iri,
69	) -> Self::Resource {
70		T::interpret_lexical_iri(*self, vocabulary, iri)
71	}
72
73	fn interpret_owned_lexical_iri(
74		&mut self,
75		vocabulary: &mut impl IriVocabularyMut<Iri = I>,
76		iri: IriBuf,
77	) -> Self::Resource {
78		T::interpret_owned_lexical_iri(*self, vocabulary, iri)
79	}
80}
81
82pub trait ReverseIriInterpretation: Interpretation {
83	type Iri;
84	type Iris<'a>: Clone + Iterator<Item = &'a Self::Iri>
85	where
86		Self: 'a;
87
88	fn iris_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Iris<'a>;
89}
90
91impl<T: ReverseIriInterpretation> ReverseIriInterpretation for &T {
92	type Iri = T::Iri;
93	type Iris<'a>
94		= T::Iris<'a>
95	where
96		Self: 'a;
97
98	fn iris_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Iris<'a> {
99		T::iris_of(*self, id)
100	}
101}
102
103impl<T: ReverseIriInterpretation> ReverseIriInterpretation for &mut T {
104	type Iri = T::Iri;
105	type Iris<'a>
106		= T::Iris<'a>
107	where
108		Self: 'a;
109
110	fn iris_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Iris<'a> {
111		T::iris_of(*self, id)
112	}
113}
114
115pub trait ReverseIriInterpretationMut: ReverseIriInterpretation {
116	fn assign_iri(&mut self, id: &Self::Resource, iri: Self::Iri) -> bool;
117}
118
119impl<T: ReverseIriInterpretationMut> ReverseIriInterpretationMut for &mut T {
120	fn assign_iri(&mut self, id: &Self::Resource, iri: Self::Iri) -> bool {
121		T::assign_iri(*self, id, iri)
122	}
123}