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