Skip to main content

rdfx/interpretation/impl/
with_generator.rs

1use std::borrow::Cow;
2
3use iri_rs::{Iri, IriBuf};
4
5use crate::{
6    BlankId,
7    CowLiteral,
8    Generator,
9    Interpretation,
10    InterpretationMut,
11    LiteralRef,
12    LocalGenerator,
13    interpretation::{GenerativeInterpretation, LocalInterpretation, LocalInterpretationMut, ReverseInterpretation, ReverseLocalInterpretation},
14};
15
16/// Combines any RDF interpretation with a node id generator to make it
17/// implement `InterpretationMut`.
18///
19/// # Use cases
20///
21/// ## `()` does not implement `InterpretationMut`
22///
23/// The transparent interpretation (the unit type `()`) does not implement
24/// `InterpretationMut` because the `new_resource` method would require the
25/// creation of a fresh, unused, blank node identifier, which the transparent
26/// interpretation cannot create without a node id generator.
27///
28/// ## Resources returned by `InterpretationMut::new_resource` have no lexical representation
29///
30/// Interpreted resources are not required to have a lexical representation.
31/// This is most probably the case for new resources returned by
32/// `InterpretationMut::new_resource`. You can use `WithGenerator` to pair the
33/// interpretation with a node id generator so that `new_resource` will assign
34/// a lexical representation to new resources (a fresh blank node id for
35/// instance).
36pub struct WithGenerator<G, I = ()> {
37    interpretation: I,
38    generator: G,
39}
40
41impl<G, I> WithGenerator<G, I> {
42    pub const fn new(interpretation: I, generator: G) -> Self {
43        Self { interpretation, generator }
44    }
45
46    pub fn into_parts(self) -> (I, G) {
47        (self.interpretation, self.generator)
48    }
49
50    pub const fn inner_interpretation(&self) -> &I {
51        &self.interpretation
52    }
53
54    pub const fn inner_interpretation_mut(&mut self) -> &mut I {
55        &mut self.interpretation
56    }
57
58    pub const fn generator(&self) -> &G {
59        &self.generator
60    }
61
62    pub const fn generator_mut(&mut self) -> &mut G {
63        &mut self.generator
64    }
65
66    pub fn into_inner_interpretation(self) -> I {
67        self.interpretation
68    }
69
70    pub fn into_generator(self) -> G {
71        self.generator
72    }
73}
74
75impl<I: Interpretation, G> Interpretation for WithGenerator<G, I> {
76    type Resource = I::Resource;
77
78    fn iri(&self, iri: Iri<&str>) -> Option<Self::Resource> {
79        self.interpretation.iri(iri)
80    }
81
82    fn literal<'a>(&self, literal: impl Into<LiteralRef<'a>>) -> Option<Self::Resource> {
83        self.interpretation.literal(literal)
84    }
85}
86
87impl<I: InterpretationMut, G> InterpretationMut for WithGenerator<G, I> {
88    fn insert_iri<'a>(&mut self, iri: impl Into<Cow<'a, IriBuf>>) -> Self::Resource {
89        self.interpretation.insert_iri(iri)
90    }
91
92    fn insert_literal<'a>(&mut self, literal: impl Into<CowLiteral<'a>>) -> Self::Resource {
93        self.interpretation.insert_literal(literal)
94    }
95}
96
97impl<I: ReverseInterpretation, G> ReverseInterpretation for WithGenerator<G, I> {
98    type Iris<'a>
99        = I::Iris<'a>
100    where
101        Self: 'a;
102    type Literals<'a>
103        = I::Literals<'a>
104    where
105        Self: 'a;
106
107    fn iris_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Iris<'a> {
108        self.interpretation.iris_of(resource)
109    }
110
111    fn literals_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Literals<'a> {
112        self.interpretation.literals_of(resource)
113    }
114
115    fn triple_term_components(&self, resource: &Self::Resource) -> Option<(Self::Resource, Self::Resource, Self::Resource)> {
116        self.interpretation.triple_term_components(resource)
117    }
118
119    fn triple_term_components_view<'a>(
120        &'a self,
121        resource: &'a Self::Resource,
122    ) -> Option<(
123        std::borrow::Cow<'a, Self::Resource>,
124        std::borrow::Cow<'a, Self::Resource>,
125        std::borrow::Cow<'a, Self::Resource>,
126    )>
127    where
128        Self::Resource: Clone,
129    {
130        self.interpretation.triple_term_components_view(resource)
131    }
132}
133
134impl<I: InterpretationMut, G: Generator> GenerativeInterpretation for WithGenerator<G, I> {
135    fn new_resource(&mut self) -> Self::Resource {
136        let term = self.generator.next_term();
137        self.interpretation.insert_term(term)
138    }
139}
140
141pub struct WithLocalGenerator<G, I = ()> {
142    interpretation: I,
143    generator: G,
144}
145
146impl<G, I> WithLocalGenerator<G, I> {
147    pub const fn new(interpretation: I, generator: G) -> Self {
148        Self { interpretation, generator }
149    }
150
151    pub fn into_parts(self) -> (I, G) {
152        (self.interpretation, self.generator)
153    }
154
155    pub const fn inner_interpretation(&self) -> &I {
156        &self.interpretation
157    }
158
159    pub const fn inner_interpretation_mut(&mut self) -> &mut I {
160        &mut self.interpretation
161    }
162
163    pub const fn generator(&self) -> &G {
164        &self.generator
165    }
166
167    pub const fn generator_mut(&mut self) -> &mut G {
168        &mut self.generator
169    }
170
171    pub fn into_inner_interpretation(self) -> I {
172        self.interpretation
173    }
174
175    pub fn into_generator(self) -> G {
176        self.generator
177    }
178}
179
180impl<I: Interpretation, G> Interpretation for WithLocalGenerator<G, I> {
181    type Resource = I::Resource;
182
183    fn iri(&self, iri: Iri<&str>) -> Option<Self::Resource> {
184        self.interpretation.iri(iri)
185    }
186
187    fn literal<'a>(&self, literal: impl Into<LiteralRef<'a>>) -> Option<Self::Resource> {
188        self.interpretation.literal(literal)
189    }
190}
191
192impl<I: LocalInterpretation, G> LocalInterpretation for WithLocalGenerator<G, I> {
193    fn blank_id<'a>(&'a self, blank_id: &'a crate::BlankId) -> Option<Self::Resource> {
194        self.interpretation.blank_id(blank_id)
195    }
196}
197
198impl<I: InterpretationMut, G> InterpretationMut for WithLocalGenerator<G, I> {
199    fn insert_iri<'a>(&mut self, iri: impl Into<Cow<'a, IriBuf>>) -> Self::Resource {
200        self.interpretation.insert_iri(iri)
201    }
202
203    fn insert_literal<'a>(&mut self, literal: impl Into<CowLiteral<'a>>) -> Self::Resource {
204        self.interpretation.insert_literal(literal)
205    }
206}
207
208impl<I: LocalInterpretationMut, G> LocalInterpretationMut for WithLocalGenerator<G, I> {
209    fn insert_blank_id<'a>(&mut self, blank_id: impl Into<Cow<'a, BlankId>>) -> Self::Resource {
210        self.interpretation.insert_blank_id(blank_id)
211    }
212}
213
214impl<I: ReverseInterpretation, G> ReverseInterpretation for WithLocalGenerator<G, I> {
215    type Iris<'a>
216        = I::Iris<'a>
217    where
218        Self: 'a;
219    type Literals<'a>
220        = I::Literals<'a>
221    where
222        Self: 'a;
223
224    fn iris_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Iris<'a> {
225        self.interpretation.iris_of(resource)
226    }
227
228    fn literals_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Literals<'a> {
229        self.interpretation.literals_of(resource)
230    }
231
232    fn triple_term_components(&self, resource: &Self::Resource) -> Option<(Self::Resource, Self::Resource, Self::Resource)> {
233        self.interpretation.triple_term_components(resource)
234    }
235
236    fn triple_term_components_view<'a>(
237        &'a self,
238        resource: &'a Self::Resource,
239    ) -> Option<(
240        std::borrow::Cow<'a, Self::Resource>,
241        std::borrow::Cow<'a, Self::Resource>,
242        std::borrow::Cow<'a, Self::Resource>,
243    )>
244    where
245        Self::Resource: Clone,
246    {
247        self.interpretation.triple_term_components_view(resource)
248    }
249}
250
251impl<I: ReverseLocalInterpretation, G> ReverseLocalInterpretation for WithLocalGenerator<G, I> {
252    type BlankIds<'a>
253        = I::BlankIds<'a>
254    where
255        Self: 'a;
256
257    fn blank_ids_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::BlankIds<'a> {
258        self.interpretation.blank_ids_of(resource)
259    }
260}
261
262impl<I: LocalInterpretationMut, G: LocalGenerator> GenerativeInterpretation for WithLocalGenerator<G, I> {
263    fn new_resource(&mut self) -> Self::Resource {
264        let term = self.generator.next_local_term();
265        self.interpretation.insert_local_term(term)
266    }
267}