linked_data_next/datatypes/
de.rs1use rdf_types::{
2 Interpretation, RDF_LANG_STRING, Vocabulary,
3 interpretation::{ReverseIriInterpretation, ReverseLiteralInterpretation},
4 vocabulary::LiteralVocabulary,
5};
6
7use crate::{
8 Context, FromLinkedDataError, LinkedDataDeserializePredicateObjects,
9 LinkedDataDeserializeSubject,
10};
11
12macro_rules! deserialize_datatype {
13 ($ty:ty, $iri:ident, $($extra_type:expr)*) => {
14 impl<V: Vocabulary, I: Interpretation> LinkedDataDeserializeSubject<I, V> for $ty
15 where
16 V: LiteralVocabulary,
17 I: ReverseIriInterpretation<Iri = V::Iri> + ReverseLiteralInterpretation<Literal = V::Literal>
18 {
19 fn deserialize_subject_in<D>(
20 vocabulary: &V,
21 interpretation: &I,
22 _dataset: &D,
23 _graph: Option<&I::Resource>,
24 resource: &I::Resource,
25 context: Context<I>
26 ) -> Result<Self, FromLinkedDataError>
27 where
28 D: rdf_types::dataset::PatternMatchingDataset<Resource = I::Resource>
29 {
30 let mut literal_ty = None;
31 for l in interpretation.literals_of(resource) {
32 let l = vocabulary.literal(l).unwrap();
33 match l.type_ {
34 rdf_types::LiteralTypeRef::Any(ty_iri) => {
35 let ty_iri = vocabulary.iri(ty_iri).unwrap();
36 if ty_iri == xsd_types::$iri $(|| ty_iri == $extra_type)* {
37 return match l.value.parse() {
38 Ok(value) => Ok(value),
39 Err(_) => Err(FromLinkedDataError::InvalidLiteral(
40 context.into_iris(
41 vocabulary,
42 interpretation
43 )
44 ))
45 }
46 }
47
48 literal_ty = Some(ty_iri)
49 }
50 rdf_types::LiteralTypeRef::LangString(_) => {
51 literal_ty = Some(RDF_LANG_STRING)
52 }
53 }
54 }
55
56 match literal_ty {
57 Some(ty) => {
58 Err(FromLinkedDataError::LiteralTypeMismatch {
59 context: context.into_iris(vocabulary, interpretation),
60 expected: Some(xsd_types::$iri.to_owned()),
61 found: ty.to_owned()
62 })
63 }
64 None => {
65 Err(FromLinkedDataError::ExpectedLiteral(
66 context.into_iris(vocabulary, interpretation)
67 ))
68 }
69 }
70 }
71 }
72
73 impl<V: Vocabulary, I: Interpretation> LinkedDataDeserializePredicateObjects<I, V> for $ty
74 where
75 V: LiteralVocabulary,
76 I: ReverseIriInterpretation<Iri = V::Iri> + ReverseLiteralInterpretation<Literal = V::Literal>
77 {
78 fn deserialize_objects_in<'a, D>(
79 vocabulary: &V,
80 interpretation: &I,
81 dataset: &D,
82 graph: Option<&I::Resource>,
83 objects: impl IntoIterator<Item = &'a <I as Interpretation>::Resource>,
84 context: Context<I>
85 ) -> Result<Self, FromLinkedDataError>
86 where
87 <I as Interpretation>::Resource: 'a,
88 D: rdf_types::dataset::PatternMatchingDataset<Resource = I::Resource>
89 {
90 let mut error = None;
91
92 for o in objects {
93 match Self::deserialize_subject_in(vocabulary, interpretation, dataset, graph, o, context) {
94 Ok(value) => return Ok(value),
95 Err(e) => error = Some(e)
96 }
97 }
98
99 Err(error.unwrap_or_else(|| {
100 FromLinkedDataError::MissingRequiredValue(
101 context.into_iris(vocabulary, interpretation)
102 )
103 }))
104 }
105 }
106 };
107}
108
109deserialize_datatype!(bool, XSD_BOOLEAN,);
110deserialize_datatype!(u8, XSD_UNSIGNED_BYTE, xsd_types::XSD_INTEGER);
111deserialize_datatype!(u16, XSD_UNSIGNED_SHORT, xsd_types::XSD_INTEGER);
112deserialize_datatype!(u32, XSD_UNSIGNED_INT, xsd_types::XSD_INTEGER);
113deserialize_datatype!(u64, XSD_UNSIGNED_LONG, xsd_types::XSD_INTEGER);
114deserialize_datatype!(i8, XSD_BYTE, xsd_types::XSD_INTEGER);
115deserialize_datatype!(i16, XSD_SHORT, xsd_types::XSD_INTEGER);
116deserialize_datatype!(i32, XSD_INT, xsd_types::XSD_INTEGER);
117deserialize_datatype!(i64, XSD_LONG, xsd_types::XSD_INTEGER);
118deserialize_datatype!(f32, XSD_FLOAT, xsd_types::XSD_DECIMAL);
119deserialize_datatype!(f64, XSD_DOUBLE, xsd_types::XSD_DECIMAL);
120deserialize_datatype!(String, XSD_STRING,);
121deserialize_datatype!(xsd_types::DateTime, XSD_DATE_TIME,);