Skip to main content

linked_data_next/datatypes/
ser.rs

1use rdf_types::{
2	Id, Interpretation, Term, Vocabulary,
3	vocabulary::{IriVocabularyMut, LiteralVocabularyMut},
4};
5
6use crate::{
7	CowRdfTerm, LinkedDataPredicateObjects, LinkedDataResource, LinkedDataSubject,
8	PredicateObjectsVisitor, RdfLiteral, RdfLiteralRef, ResourceInterpretation,
9};
10
11macro_rules! datatype {
12	($ty:ty, $variant:ident) => {
13		impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation>
14			LinkedDataResource<I, V> for $ty
15		{
16			fn interpretation(
17				&self,
18				_vocabulary: &mut V,
19				_interpretation: &mut I,
20			) -> ResourceInterpretation<'_, I, V> {
21				ResourceInterpretation::Uninterpreted(Some(CowRdfTerm::Owned(Term::Literal(
22					RdfLiteral::Xsd(xsd_types::Value::$variant(self.clone().into())),
23				))))
24			}
25		}
26
27		impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation>
28			LinkedDataSubject<I, V> for $ty
29		{
30			fn visit_subject<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
31			where
32				S: crate::SubjectVisitor<I, V>,
33			{
34				visitor.end()
35			}
36		}
37
38		impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation>
39			LinkedDataPredicateObjects<I, V> for $ty
40		{
41			fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
42			where
43				S: PredicateObjectsVisitor<I, V>,
44			{
45				visitor.object(self)?;
46				visitor.end()
47			}
48		}
49	};
50}
51
52macro_rules! unsized_datatype {
53	($($ty:ty : $variant:ident),*) => {
54		$(
55			impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation> LinkedDataResource<I, V> for $ty {
56				fn interpretation(
57					&self,
58					_vocabulary: &mut V,
59					_interpretation: &mut I,
60				) -> ResourceInterpretation<'_, I, V> {
61					ResourceInterpretation::Uninterpreted(Some(CowRdfTerm::Borrowed(Term::Literal(RdfLiteralRef::Xsd(
62						xsd_types::ValueRef::$variant(self)
63					)))))
64				}
65			}
66
67			impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation> LinkedDataSubject<I, V> for $ty {
68				fn visit_subject<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
69				where
70					S: crate::SubjectVisitor<I, V>
71				{
72					visitor.end()
73				}
74			}
75
76			impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation> LinkedDataPredicateObjects<I, V> for $ty {
77				fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
78				where
79					S: PredicateObjectsVisitor<I, V>,
80				{
81					visitor.object(self)?;
82					visitor.end()
83				}
84			}
85		)*
86	};
87}
88
89datatype!(bool, Boolean);
90datatype!(u8, UnsignedByte);
91datatype!(u16, UnsignedShort);
92datatype!(u32, UnsignedInt);
93datatype!(u64, UnsignedLong);
94datatype!(i8, Byte);
95datatype!(i16, Short);
96datatype!(i32, Int);
97datatype!(i64, Long);
98datatype!(f32, Float);
99datatype!(f64, Double);
100datatype!(String, String);
101datatype!(xsd_types::DateTime, DateTime);
102
103unsized_datatype! {
104	str: String
105}
106
107impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation>
108	LinkedDataResource<I, V> for xsd_types::AnyUriBuf
109{
110	fn interpretation(
111		&self,
112		_vocabulary: &mut V,
113		_interpretation: &mut I,
114	) -> ResourceInterpretation<'_, I, V> {
115		ResourceInterpretation::Uninterpreted(Some(CowRdfTerm::Owned(Term::Literal(
116			RdfLiteral::Xsd(xsd_types::Value::AnyUri(self.clone())),
117		))))
118	}
119
120	fn reference_interpretation(
121		&self,
122		vocabulary: &mut V,
123		_interpretation: &mut I,
124	) -> ResourceInterpretation<'_, I, V> {
125		ResourceInterpretation::Uninterpreted(Some(CowRdfTerm::Owned(Term::Id(Id::Iri(
126			vocabulary.insert(self.as_iri()),
127		)))))
128	}
129}
130
131impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation>
132	LinkedDataSubject<I, V> for xsd_types::AnyUriBuf
133{
134	fn visit_subject<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
135	where
136		S: crate::SubjectVisitor<I, V>,
137	{
138		visitor.end()
139	}
140}
141
142impl<V: Vocabulary + IriVocabularyMut + LiteralVocabularyMut, I: Interpretation>
143	LinkedDataPredicateObjects<I, V> for xsd_types::AnyUriBuf
144{
145	fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
146	where
147		S: PredicateObjectsVisitor<I, V>,
148	{
149		visitor.object(self)?;
150		visitor.end()
151	}
152}