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
use crate::{
	literal,
	vocabulary::{IriVocabularyMut, LiteralVocabulary, LiteralVocabularyMut},
	Interpretation, Literal,
};

/// Literal value interpretation.
pub trait LiteralInterpretation<L>: Interpretation {
	/// Returns the interpretation of the given literal value, if any.
	fn literal_interpretation(&self, literal: &L) -> Option<Self::Resource>;

	fn lexical_literal_interpretation<V: LiteralVocabulary<Literal = L>>(
		&self,
		vocabulary: &V,
		literal: &Literal<V::Iri>,
	) -> Option<Self::Resource> {
		vocabulary
			.get_literal(literal)
			.and_then(|l| self.literal_interpretation(&l))
	}
}

impl<'t, L, T: LiteralInterpretation<L>> LiteralInterpretation<L> for &'t T {
	fn literal_interpretation(&self, literal: &L) -> Option<Self::Resource> {
		T::literal_interpretation(*self, literal)
	}

	fn lexical_literal_interpretation<V: LiteralVocabulary<Literal = L>>(
		&self,
		vocabulary: &V,
		literal: &Literal<V::Iri>,
	) -> Option<Self::Resource> {
		T::lexical_literal_interpretation(*self, vocabulary, literal)
	}
}

impl<'t, L, T: LiteralInterpretation<L>> LiteralInterpretation<L> for &'t mut T {
	fn literal_interpretation(&self, literal: &L) -> Option<Self::Resource> {
		T::literal_interpretation(*self, literal)
	}

	fn lexical_literal_interpretation<V: LiteralVocabulary<Literal = L>>(
		&self,
		vocabulary: &V,
		literal: &Literal<V::Iri>,
	) -> Option<Self::Resource> {
		T::lexical_literal_interpretation(*self, vocabulary, literal)
	}
}

/// Mutable literal value interpretation.
pub trait LiteralInterpretationMut<L = Literal>: Interpretation {
	/// Interprets the given literal value.
	fn interpret_literal(&mut self, literal: L) -> Self::Resource;

	fn interpret_lexical_literal<V: LiteralVocabularyMut<Literal = L>>(
		&mut self,
		vocabulary: &mut V,
		literal: &Literal<V::Iri>,
	) -> Self::Resource {
		self.interpret_literal(vocabulary.insert_literal(literal))
	}

	fn interpret_owned_lexical_literal<V: LiteralVocabularyMut<Literal = L>>(
		&mut self,
		vocabulary: &mut V,
		literal: Literal<V::Iri>,
	) -> Self::Resource {
		self.interpret_literal(vocabulary.insert_owned_literal(literal))
	}

	fn interpret_full_lexical_literal(
		&mut self,
		vocabulary: &mut (impl IriVocabularyMut + LiteralVocabularyMut<Literal = L>),
		literal: Literal,
	) -> Self::Resource {
		let (value, type_) = literal.into_parts();
		let type_ = match type_ {
			literal::LiteralType::Any(ty) => literal::LiteralType::Any(vocabulary.insert_owned(ty)),
			literal::LiteralType::LangString(tag) => literal::LiteralType::LangString(tag),
		};

		self.interpret_literal(vocabulary.insert_owned_literal(Literal::new(value, type_)))
	}
}

pub trait ReverseLiteralInterpretation: Interpretation {
	type Literal;

	type Literals<'a>: Clone + Iterator<Item = &'a Self::Literal>
	where
		Self: 'a;

	fn literals_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Literals<'a>;
}

impl<'t, T: ReverseLiteralInterpretation> ReverseLiteralInterpretation for &'t T {
	type Literal = T::Literal;
	type Literals<'a> = T::Literals<'a> where Self: 'a;

	fn literals_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Literals<'a> {
		T::literals_of(*self, id)
	}
}

impl<'t, T: ReverseLiteralInterpretation> ReverseLiteralInterpretation for &'t mut T {
	type Literal = T::Literal;
	type Literals<'a> = T::Literals<'a> where Self: 'a;

	fn literals_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Literals<'a> {
		T::literals_of(*self, id)
	}
}