Skip to main content

rdf_syntax/interpretation/impl/
with_generator.rs

1use std::borrow::Cow;
2
3use iref::Iri;
4use rdf_types::{Domain, GenDomain, StaticDomain};
5
6use crate::{
7	BlankId, CowLiteral, Generator, GroundInterpretation, GroundInterpretationMut, LiteralRef,
8	Term,
9	interpretation::{
10		Interpretation, InterpretationMut, ReverseGroundInterpretation, ReverseInterpretation,
11	},
12};
13
14/// Combines any RDF interpretation with a node id generator.
15///
16/// - Provides a [`GenDomain`] implementation, using the given
17///   generator to create fresh node identifiers.
18/// - Assigns a node identifier to each new resource created by
19///   [`GenDomain::new_resource`], based on the generator.
20///
21/// Note that in the case of the unit (`()`) interpretation, using
22/// [`GenDomain`] will give you a similar result but with other
23/// advantages (such as a [`ConstGenDomain`] implementation).
24///
25/// [`GenDomain`]: rdf_types::GenDomain
26/// [`ConstGenDomain`]: rdf_types::ConstGenDomain
27pub struct WithGenerator<G, I = StaticDomain<Term>> {
28	interpretation: I,
29	generator: G,
30}
31
32impl<G, I> WithGenerator<G, I> {
33	/// Combines the given interpretation with the given generator.
34	pub fn new(interpretation: I, generator: G) -> Self {
35		Self {
36			interpretation,
37			generator,
38		}
39	}
40
41	/// Discards this wrapper, returning the inner interpretation and
42	/// generator.
43	pub fn into_parts(self) -> (I, G) {
44		(self.interpretation, self.generator)
45	}
46
47	/// Returns a reference to the inner interpretation.
48	pub fn inner_interpretation(&self) -> &I {
49		&self.interpretation
50	}
51
52	/// Returns a mutable reference to the inner interpretation.
53	pub fn inner_interpretation_mut(&mut self) -> &mut I {
54		&mut self.interpretation
55	}
56
57	/// Returns a reference to the inner generator.
58	pub fn generator(&self) -> &G {
59		&self.generator
60	}
61
62	/// Returns a mutable reference to the inner generator.
63	pub fn generator_mut(&mut self) -> &mut G {
64		&mut self.generator
65	}
66
67	/// Discards this wrapper and the generator, returning the inner
68	/// interpretation.
69	pub fn into_inner_interpretation(self) -> I {
70		self.interpretation
71	}
72
73	/// Discards this wrapper and the interpretation, returning the inner
74	/// generator.
75	pub fn into_generator(self) -> G {
76		self.generator
77	}
78}
79
80impl<I: Domain, G> Domain for WithGenerator<G, I> {
81	type Resource = I::Resource;
82}
83
84impl<I: GroundInterpretation, G> GroundInterpretation for WithGenerator<G, I> {
85	fn iri(&self, iri: &Iri) -> Option<Self::Resource> {
86		self.interpretation.iri(iri)
87	}
88
89	fn literal<'a>(&self, literal: impl Into<LiteralRef<'a>>) -> Option<Self::Resource> {
90		self.interpretation.literal(literal)
91	}
92}
93
94impl<I: Interpretation, G> Interpretation for WithGenerator<G, I> {
95	fn blank_id<'a>(&'a self, blank_id: &'a crate::BlankId) -> Option<Self::Resource> {
96		self.interpretation.blank_id(blank_id)
97	}
98}
99
100impl<I: GroundInterpretationMut, G> GroundInterpretationMut for WithGenerator<G, I> {
101	fn insert_iri<'a>(&mut self, iri: impl Into<Cow<'a, Iri>>) -> Self::Resource {
102		self.interpretation.insert_iri(iri)
103	}
104
105	fn insert_literal<'a>(&mut self, literal: impl Into<CowLiteral<'a>>) -> Self::Resource {
106		self.interpretation.insert_literal(literal)
107	}
108}
109
110impl<I: InterpretationMut, G> InterpretationMut for WithGenerator<G, I> {
111	fn insert_blank_id<'a>(&mut self, blank_id: impl Into<Cow<'a, BlankId>>) -> Self::Resource {
112		self.interpretation.insert_blank_id(blank_id)
113	}
114}
115
116impl<I: ReverseGroundInterpretation, G> ReverseGroundInterpretation for WithGenerator<G, I> {
117	type Iris<'a>
118		= I::Iris<'a>
119	where
120		Self: 'a;
121	type Literals<'a>
122		= I::Literals<'a>
123	where
124		Self: 'a;
125
126	fn iris_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Iris<'a> {
127		self.interpretation.iris_of(resource)
128	}
129
130	fn literals_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Literals<'a> {
131		self.interpretation.literals_of(resource)
132	}
133}
134
135impl<I: ReverseInterpretation, G> ReverseInterpretation for WithGenerator<G, I> {
136	type BlankIds<'a>
137		= I::BlankIds<'a>
138	where
139		Self: 'a;
140
141	fn blank_ids_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::BlankIds<'a> {
142		self.interpretation.blank_ids_of(resource)
143	}
144}
145
146impl<I: InterpretationMut, G: Generator> GenDomain for WithGenerator<G, I> {
147	fn new_resource(&mut self) -> Self::Resource {
148		let id = self.generator.next_id();
149		self.interpretation.insert_id(id)
150	}
151}