1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! Resource interpretations.
use crate::{Id, Literal, Quad, Term};

mod r#impl;
pub use r#impl::*;

mod iri;
pub use iri::*;

mod blank_id;
pub use blank_id::*;

mod literal;
pub use literal::*;

mod id;
pub use id::*;

mod term;
pub use term::*;

pub mod fallible;
pub use fallible::FallibleInterpretation;

/// RDF resource interpretation.
pub trait Interpretation {
	/// Resource identifier type.
	type Resource;
}

impl<'a, I: Interpretation> Interpretation for &'a I {
	type Resource = I::Resource;
}

impl<'a, I: Interpretation> Interpretation for &'a mut I {
	type Resource = I::Resource;
}

pub trait TraversableInterpretation: Interpretation {
	/// Interpreted resource iterator.
	type Resources<'a>: Iterator<Item = &'a Self::Resource>
	where
		Self: 'a;

	/// Returns an iterator over the interpreted resources.
	fn resources(&self) -> Self::Resources<'_>;
}

impl<'i, I: TraversableInterpretation> TraversableInterpretation for &'i I {
	type Resources<'a> = I::Resources<'a> where Self: 'a;

	fn resources(&self) -> Self::Resources<'_> {
		I::resources(*self)
	}
}

impl<'i, I: TraversableInterpretation> TraversableInterpretation for &'i mut I {
	type Resources<'a> = I::Resources<'a> where Self: 'a;

	fn resources(&self) -> Self::Resources<'_> {
		I::resources(*self)
	}
}

/// Mutable RDF resource interpretation.
pub trait InterpretationMut<V>: Interpretation {
	/// Creates a new resource.
	fn new_resource(&mut self, vocabulary: &mut V) -> Self::Resource;
}

impl<'t, V, T: InterpretationMut<V>> InterpretationMut<V> for &'t mut T {
	fn new_resource(&mut self, vocabulary: &mut V) -> Self::Resource {
		T::new_resource(*self, vocabulary)
	}
}

pub type UninterpretedIdRef<'a, I> =
	Id<&'a <I as ReverseIriInterpretation>::Iri, &'a <I as ReverseBlankIdInterpretation>::BlankId>;

pub type UninterpretedTermRef<'a, I> =
	Term<UninterpretedIdRef<'a, I>, &'a <I as ReverseLiteralInterpretation>::Literal>;

pub type UninterpretedQuadRef<'a, I> = Quad<
	UninterpretedIdRef<'a, I>,
	&'a <I as ReverseIriInterpretation>::Iri,
	UninterpretedTermRef<'a, I>,
	UninterpretedIdRef<'a, I>,
>;

pub type UninterpretedGrdfQuadRef<'a, I> = Quad<
	UninterpretedTermRef<'a, I>,
	UninterpretedTermRef<'a, I>,
	UninterpretedTermRef<'a, I>,
	UninterpretedTermRef<'a, I>,
>;

/// RDF interpretation function.
pub trait Interpret<I: Interpretation> {
	/// Interpreted form.
	type Interpreted;

	/// Interpret the given resource.
	fn interpret(self, interpretation: &mut I) -> Self::Interpreted;
}

impl<I: Interpretation, T: Interpret<I>> Interpret<I> for Option<T> {
	type Interpreted = Option<T::Interpreted>;

	fn interpret(self, interpretation: &mut I) -> Self::Interpreted {
		self.map(|t| t.interpret(interpretation))
	}
}

impl<I, B, T: IdInterpretationMut<I, B>> Interpret<T> for Id<I, B> {
	type Interpreted = T::Resource;

	fn interpret(self, interpretation: &mut T) -> Self::Interpreted {
		interpretation.interpret_id(self)
	}
}

impl<T, I: LiteralInterpretationMut<Self>> Interpret<I> for Literal<T> {
	type Interpreted = I::Resource;

	fn interpret(self, interpretation: &mut I) -> Self::Interpreted {
		interpretation.interpret_literal(self)
	}
}