linked_data_schema/
linked_data_schema_field_visitor.rs

1mod uuid;
2
3use prefixmap::IriRef;
4use shacl_ast::component::Component;
5
6pub trait LinkedDataSchemaFieldVisitor {
7  fn field_components() -> Vec<Component> {
8    vec![]
9  }
10
11  fn type_iri_ref() -> Option<IriRef>;
12}
13
14macro_rules! field_visitor_impl {
15  ($for_type:ty, $uri_datatype:literal) => {
16    impl LinkedDataSchemaFieldVisitor for $for_type {
17      fn field_components() -> Vec<Component> {
18        use std::str::FromStr;
19
20        vec![
21          Component::MinCount(1),
22          Component::MaxCount(1),
23          Component::Datatype(IriRef::from_str($uri_datatype).unwrap()),
24        ]
25      }
26
27      fn type_iri_ref() -> Option<IriRef> {
28        use std::str::FromStr;
29
30        IriRef::from_str($uri_datatype).ok()
31      }
32    }
33  };
34}
35
36field_visitor_impl!(String, "http://www.w3.org/2001/XMLSchema#string");
37field_visitor_impl!(u8, "http://www.w3.org/2001/XMLSchema#unsignedByte");
38field_visitor_impl!(i8, "http://www.w3.org/2001/XMLSchema#byte");
39field_visitor_impl!(u16, "http://www.w3.org/2001/XMLSchema#unsignedShort");
40field_visitor_impl!(i16, "http://www.w3.org/2001/XMLSchema#short");
41field_visitor_impl!(u32, "http://www.w3.org/2001/XMLSchema#unsignedInt");
42field_visitor_impl!(i32, "http://www.w3.org/2001/XMLSchema#int");
43field_visitor_impl!(u64, "http://www.w3.org/2001/XMLSchema#unsignedLong");
44field_visitor_impl!(i64, "http://www.w3.org/2001/XMLSchema#long");
45field_visitor_impl!(usize, "http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
46field_visitor_impl!(isize, "http://www.w3.org/2001/XMLSchema#integer");
47field_visitor_impl!(f32, "http://www.w3.org/2001/XMLSchema#float");
48field_visitor_impl!(f64, "http://www.w3.org/2001/XMLSchema#double");
49
50impl<S: LinkedDataSchemaFieldVisitor> LinkedDataSchemaFieldVisitor for Option<S> {
51  fn field_components() -> Vec<Component> {
52    if let Some(datatype) = S::type_iri_ref() {
53      [
54        S::field_components(),
55        vec![Component::MaxCount(1), Component::Datatype(datatype)],
56      ]
57      .concat()
58    } else {
59      vec![]
60    }
61  }
62
63  fn type_iri_ref() -> Option<IriRef> {
64    None
65  }
66}
67
68impl<S: LinkedDataSchemaFieldVisitor> LinkedDataSchemaFieldVisitor for Vec<S> {
69  fn field_components() -> Vec<Component> {
70    if let Some(datatype) = S::type_iri_ref() {
71      [S::field_components(), vec![Component::Datatype(datatype)]].concat()
72    } else {
73      vec![]
74    }
75  }
76
77  fn type_iri_ref() -> Option<IriRef> {
78    None
79  }
80}