rdf_types/interpretation/
literal.rs

1use crate::{
2	literal,
3	vocabulary::{IriVocabularyMut, LiteralVocabulary, LiteralVocabularyMut},
4	Interpretation, Literal, LiteralRef,
5};
6
7/// Literal value interpretation.
8pub trait LiteralInterpretation<L>: Interpretation {
9	/// Returns the interpretation of the given literal value, if any.
10	fn literal_interpretation(&self, literal: &L) -> Option<Self::Resource>;
11
12	fn lexical_literal_interpretation<V: LiteralVocabulary<Literal = L>>(
13		&self,
14		vocabulary: &V,
15		literal: LiteralRef<V::Iri>,
16	) -> Option<Self::Resource> {
17		vocabulary
18			.get_literal(literal)
19			.and_then(|l| self.literal_interpretation(&l))
20	}
21}
22
23impl<L, T: LiteralInterpretation<L>> LiteralInterpretation<L> for &T {
24	fn literal_interpretation(&self, literal: &L) -> Option<Self::Resource> {
25		T::literal_interpretation(*self, literal)
26	}
27
28	fn lexical_literal_interpretation<V: LiteralVocabulary<Literal = L>>(
29		&self,
30		vocabulary: &V,
31		literal: LiteralRef<V::Iri>,
32	) -> Option<Self::Resource> {
33		T::lexical_literal_interpretation(*self, vocabulary, literal)
34	}
35}
36
37impl<L, T: LiteralInterpretation<L>> LiteralInterpretation<L> for &mut T {
38	fn literal_interpretation(&self, literal: &L) -> Option<Self::Resource> {
39		T::literal_interpretation(*self, literal)
40	}
41
42	fn lexical_literal_interpretation<V: LiteralVocabulary<Literal = L>>(
43		&self,
44		vocabulary: &V,
45		literal: LiteralRef<V::Iri>,
46	) -> Option<Self::Resource> {
47		T::lexical_literal_interpretation(*self, vocabulary, literal)
48	}
49}
50
51/// Mutable literal value interpretation.
52pub trait LiteralInterpretationMut<L = Literal>: Interpretation {
53	/// Interprets the given literal value.
54	fn interpret_literal(&mut self, literal: L) -> Self::Resource;
55
56	fn interpret_lexical_literal<V: LiteralVocabularyMut<Literal = L>>(
57		&mut self,
58		vocabulary: &mut V,
59		literal: LiteralRef<V::Iri>,
60	) -> Self::Resource {
61		self.interpret_literal(vocabulary.insert_literal(literal))
62	}
63
64	fn interpret_owned_lexical_literal<V: LiteralVocabularyMut<Literal = L>>(
65		&mut self,
66		vocabulary: &mut V,
67		literal: Literal<V::Iri>,
68	) -> Self::Resource {
69		self.interpret_literal(vocabulary.insert_owned_literal(literal))
70	}
71
72	fn interpret_full_lexical_literal(
73		&mut self,
74		vocabulary: &mut (impl IriVocabularyMut + LiteralVocabularyMut<Literal = L>),
75		literal: Literal,
76	) -> Self::Resource {
77		let (value, type_) = literal.into_parts();
78		let type_ = match type_ {
79			literal::LiteralType::Any(ty) => literal::LiteralType::Any(vocabulary.insert_owned(ty)),
80			literal::LiteralType::LangString(tag) => literal::LiteralType::LangString(tag),
81		};
82
83		self.interpret_literal(vocabulary.insert_owned_literal(Literal::new(value, type_)))
84	}
85}
86
87pub trait ReverseLiteralInterpretation: Interpretation {
88	type Literal;
89
90	type Literals<'a>: Clone + Iterator<Item = &'a Self::Literal>
91	where
92		Self: 'a;
93
94	fn literals_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Literals<'a>;
95}
96
97impl<T: ReverseLiteralInterpretation> ReverseLiteralInterpretation for &T {
98	type Literal = T::Literal;
99	type Literals<'a>
100		= T::Literals<'a>
101	where
102		Self: 'a;
103
104	fn literals_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Literals<'a> {
105		T::literals_of(*self, id)
106	}
107}
108
109impl<T: ReverseLiteralInterpretation> ReverseLiteralInterpretation for &mut T {
110	type Literal = T::Literal;
111	type Literals<'a>
112		= T::Literals<'a>
113	where
114		Self: 'a;
115
116	fn literals_of<'a>(&'a self, id: &'a Self::Resource) -> Self::Literals<'a> {
117		T::literals_of(*self, id)
118	}
119}