rdf_types/interpretation/
mod.rs

1//! Resource interpretations.
2use crate::{Id, Literal, Quad, Term};
3
4mod r#impl;
5pub use r#impl::*;
6
7mod iri;
8pub use iri::*;
9
10mod blank_id;
11pub use blank_id::*;
12
13mod literal;
14pub use literal::*;
15
16mod id;
17pub use id::*;
18
19mod term;
20pub use term::*;
21
22pub mod fallible;
23pub use fallible::FallibleInterpretation;
24
25/// RDF resource interpretation.
26pub trait Interpretation {
27	/// Resource identifier type.
28	type Resource;
29}
30
31impl<I: Interpretation> Interpretation for &I {
32	type Resource = I::Resource;
33}
34
35impl<I: Interpretation> Interpretation for &mut I {
36	type Resource = I::Resource;
37}
38
39pub trait TraversableInterpretation: Interpretation {
40	/// Interpreted resource iterator.
41	type Resources<'a>: Iterator<Item = &'a Self::Resource>
42	where
43		Self: 'a;
44
45	/// Returns an iterator over the interpreted resources.
46	fn resources(&self) -> Self::Resources<'_>;
47}
48
49impl<I: TraversableInterpretation> TraversableInterpretation for &I {
50	type Resources<'a>
51		= I::Resources<'a>
52	where
53		Self: 'a;
54
55	fn resources(&self) -> Self::Resources<'_> {
56		I::resources(*self)
57	}
58}
59
60impl<I: TraversableInterpretation> TraversableInterpretation for &mut I {
61	type Resources<'a>
62		= I::Resources<'a>
63	where
64		Self: 'a;
65
66	fn resources(&self) -> Self::Resources<'_> {
67		I::resources(*self)
68	}
69}
70
71/// Mutable RDF resource interpretation.
72pub trait InterpretationMut<V>: Interpretation {
73	/// Creates a new resource.
74	fn new_resource(&mut self, vocabulary: &mut V) -> Self::Resource;
75}
76
77impl<V, T: InterpretationMut<V>> InterpretationMut<V> for &mut T {
78	fn new_resource(&mut self, vocabulary: &mut V) -> Self::Resource {
79		T::new_resource(*self, vocabulary)
80	}
81}
82
83pub type UninterpretedIdRef<'a, I> =
84	Id<&'a <I as ReverseIriInterpretation>::Iri, &'a <I as ReverseBlankIdInterpretation>::BlankId>;
85
86pub type UninterpretedTermRef<'a, I> =
87	Term<UninterpretedIdRef<'a, I>, &'a <I as ReverseLiteralInterpretation>::Literal>;
88
89pub type UninterpretedQuadRef<'a, I> = Quad<
90	UninterpretedIdRef<'a, I>,
91	&'a <I as ReverseIriInterpretation>::Iri,
92	UninterpretedTermRef<'a, I>,
93	UninterpretedIdRef<'a, I>,
94>;
95
96pub type UninterpretedGrdfQuadRef<'a, I> = Quad<
97	UninterpretedTermRef<'a, I>,
98	UninterpretedTermRef<'a, I>,
99	UninterpretedTermRef<'a, I>,
100	UninterpretedTermRef<'a, I>,
101>;
102
103/// RDF interpretation function.
104pub trait Interpret<I: Interpretation> {
105	/// Interpreted form.
106	type Interpreted;
107
108	/// Interpret the given resource.
109	fn interpret(self, interpretation: &mut I) -> Self::Interpreted;
110}
111
112impl<I: Interpretation, T: Interpret<I>> Interpret<I> for Option<T> {
113	type Interpreted = Option<T::Interpreted>;
114
115	fn interpret(self, interpretation: &mut I) -> Self::Interpreted {
116		self.map(|t| t.interpret(interpretation))
117	}
118}
119
120impl<I, B, T: IdInterpretationMut<I, B>> Interpret<T> for Id<I, B> {
121	type Interpreted = T::Resource;
122
123	fn interpret(self, interpretation: &mut T) -> Self::Interpreted {
124		interpretation.interpret_id(self)
125	}
126}
127
128impl<T, I: LiteralInterpretationMut<Self>> Interpret<I> for Literal<T> {
129	type Interpreted = I::Resource;
130
131	fn interpret(self, interpretation: &mut I) -> Self::Interpreted {
132		interpretation.interpret_literal(self)
133	}
134}