linked_data_schema/
linked_data_schema_field_visitor.rs

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