rdf_types/interpretation/
id.rs

1use iref::Iri;
2
3use crate::{
4	vocabulary::{BlankIdVocabulary, BlankIdVocabularyMut, IriVocabulary, IriVocabularyMut},
5	BlankId, Id,
6};
7
8use super::{
9	BlankIdInterpretation, BlankIdInterpretationMut, IriInterpretation, IriInterpretationMut,
10	ReverseBlankIdInterpretation, ReverseBlankIdInterpretationMut, ReverseIriInterpretation,
11	ReverseIriInterpretationMut,
12};
13
14/// Node identifier interpretation.
15pub trait IdInterpretation<I, B>: IriInterpretation<I> + BlankIdInterpretation<B> {
16	/// Returns the interpretation of the given node identifier, if any.
17	fn id_interpretation(&self, id: &Id<I, B>) -> Option<Self::Resource> {
18		match id {
19			Id::Iri(i) => self.iri_interpretation(i),
20			Id::Blank(b) => self.blank_id_interpretation(b),
21		}
22	}
23
24	fn lexical_id_interpretation(
25		&self,
26		vocabulary: &(impl IriVocabulary<Iri = I> + BlankIdVocabulary<BlankId = B>),
27		id: Id<&Iri, &BlankId>,
28	) -> Option<Self::Resource> {
29		match id {
30			Id::Iri(i) => self.lexical_iri_interpretation(vocabulary, i),
31			Id::Blank(b) => self.lexical_blank_id_interpretation(vocabulary, b),
32		}
33	}
34}
35
36impl<I, B, T: IriInterpretation<I> + BlankIdInterpretation<B>> IdInterpretation<I, B> for T {}
37
38/// Node identifier interpretation.
39pub trait IdInterpretationMut<I, B>: IriInterpretationMut<I> + BlankIdInterpretationMut<B> {
40	/// Interprets the given identifier.
41	fn interpret_id(&mut self, id: Id<I, B>) -> Self::Resource {
42		match id {
43			Id::Iri(i) => self.interpret_iri(i),
44			Id::Blank(b) => self.interpret_blank_id(b),
45		}
46	}
47
48	fn interpret_lexical_id(
49		&mut self,
50		vocabulary: &mut (impl IriVocabularyMut<Iri = I> + BlankIdVocabularyMut<BlankId = B>),
51		id: Id<&Iri, &BlankId>,
52	) -> Self::Resource {
53		match id {
54			Id::Iri(i) => self.interpret_lexical_iri(vocabulary, i),
55			Id::Blank(b) => self.interpret_lexical_blank_id(vocabulary, b),
56		}
57	}
58
59	fn interpret_owned_lexical_id(
60		&mut self,
61		vocabulary: &mut (impl IriVocabularyMut<Iri = I> + BlankIdVocabularyMut<BlankId = B>),
62		id: Id,
63	) -> Self::Resource {
64		match id {
65			Id::Iri(i) => self.interpret_owned_lexical_iri(vocabulary, i),
66			Id::Blank(b) => self.interpret_owned_lexical_blank_id(vocabulary, b),
67		}
68	}
69}
70
71impl<I, B, T: IriInterpretationMut<I> + BlankIdInterpretationMut<B>> IdInterpretationMut<I, B>
72	for T
73{
74}
75
76/// Reverse node identifier interpretation.
77///
78/// Used to retrieve the node identifiers of a given resource.
79pub trait ReverseIdInterpretation: ReverseIriInterpretation + ReverseBlankIdInterpretation {
80	fn ids_of<'a>(&'a self, id: &'a Self::Resource) -> IdsOf<'a, Self> {
81		IdsOf {
82			iris: self.iris_of(id),
83			blanks: self.blank_ids_of(id),
84		}
85	}
86}
87
88impl<I: ?Sized + ReverseIriInterpretation + ReverseBlankIdInterpretation> ReverseIdInterpretation
89	for I
90{
91}
92
93pub struct IdsOf<'a, I: 'a + ?Sized + ReverseIdInterpretation> {
94	iris: I::Iris<'a>,
95	blanks: I::BlankIds<'a>,
96}
97
98impl<'a, I: 'a + ?Sized + ReverseIdInterpretation> Clone for IdsOf<'a, I> {
99	fn clone(&self) -> Self {
100		Self {
101			iris: self.iris.clone(),
102			blanks: self.blanks.clone(),
103		}
104	}
105}
106
107impl<'a, I: 'a + ?Sized + ReverseIdInterpretation> Copy for IdsOf<'a, I>
108where
109	I::Iris<'a>: Copy,
110	I::BlankIds<'a>: Copy,
111{
112}
113
114impl<'a, I: 'a + ?Sized + ReverseIdInterpretation> Iterator for IdsOf<'a, I> {
115	type Item = Id<&'a I::Iri, &'a I::BlankId>;
116
117	fn next(&mut self) -> Option<Self::Item> {
118		self.iris
119			.next()
120			.map(Id::Iri)
121			.or_else(|| self.blanks.next().map(Id::Blank))
122	}
123}
124
125pub trait ReverseIdInterpretationMut:
126	ReverseIriInterpretationMut + ReverseBlankIdInterpretationMut
127{
128	fn assign_id(&mut self, r: &Self::Resource, id: Id<Self::Iri, Self::BlankId>) -> bool {
129		match id {
130			Id::Iri(i) => self.assign_iri(r, i),
131			Id::Blank(b) => self.assign_blank_id(r, b),
132		}
133	}
134}
135
136impl<I: ?Sized + ReverseIriInterpretationMut + ReverseBlankIdInterpretationMut>
137	ReverseIdInterpretationMut for I
138{
139}