shex_ast/ast/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
pub mod annotation;
pub mod bnode;
pub mod exclusion;
pub mod iri_ref_or_wildcard;
pub mod lang_or_wildcard;
pub mod node_constraint;
pub mod node_kind;
pub mod object_value;
// pub mod ref_;
pub mod iri_or_str;
pub mod schema;
pub mod schema_json_error;
pub mod sem_act;
pub mod serde_string_or_struct;
pub mod shape;
pub mod shape_decl;
pub mod shape_expr;
pub mod shape_expr_label;
pub mod simple_repr_schema;
pub mod start_action;
pub mod string_or_iri_stem;
pub mod string_or_literal_stem;
pub mod string_or_wildcard;
pub mod triple_expr;
pub mod triple_expr_label;
pub mod value_set_value;
pub mod xs_facet;

use crate::ast::serde_string_or_struct::*;
pub use crate::exclusion::*;
pub use annotation::*;
pub use bnode::*;
pub use iri_or_str::*;
pub use iri_ref_or_wildcard::*;
pub use lang_or_wildcard::*;
pub use node_constraint::*;
pub use node_kind::*;
pub use object_value::*;
pub use simple_repr_schema::*;
// pub use ref_::*;
pub use schema::*;
pub use schema_json_error::*;
pub use sem_act::*;
pub use shape::*;
pub use shape_decl::*;
pub use shape_expr::*;
pub use shape_expr_label::*;
pub use start_action::*;
pub use string_or_iri_stem::*;
pub use string_or_literal_stem::*;
pub use string_or_wildcard::*;
pub use triple_expr::*;
pub use triple_expr_label::*;
pub use value_set_value::*;
pub use xs_facet::*;

const BOOLEAN_STR: &str = "http://www.w3.org/2001/XMLSchema#boolean";
const INTEGER_STR: &str = "http://www.w3.org/2001/XMLSchema#integer";
const DOUBLE_STR: &str = "http://www.w3.org/2001/XMLSchema#double";
const DECIMAL_STR: &str = "http://www.w3.org/2001/XMLSchema#decimal";

#[derive(Debug, Clone)]
pub struct FromStrRefError;

#[cfg(test)]
mod tests {

    use std::str::FromStr;

    use iri_s::IriS;
    use prefixmap::IriRef;

    use super::*;

    #[test]
    fn test_shape_expr_triple_constraint() {
        let str = r#"{
            "type": "Shape",
            "expression": {
              "type": "TripleConstraint",
              "predicate": "http://a.example/p1"
            }
          }"#;
        let se = serde_json::from_str::<ShapeExpr>(str).unwrap();
        let expected = ShapeExpr::Shape(Shape::default().with_expression(
            TripleExpr::TripleConstraint {
                id: None,
                negated: None,
                inverse: None,
                predicate: IriS::new_unchecked("http://a.example/p1").into(),
                value_expr: None,
                min: None,
                max: None,
                sem_acts: None,
                annotations: None,
            },
        ));
        assert_eq!(se, expected);
    }

    #[test]
    fn test_shape_expr_ref() {
        let str = r#"{
            "type": "Shape",
            "expression": {
              "type": "TripleConstraint",
              "predicate": "http://a.example/p1",
              "valueExpr": "http://all.example/S5"
            }
          }"#;
        let se = serde_json::from_str::<ShapeExpr>(str).unwrap();
        let expected = ShapeExpr::Shape(Shape::default().with_expression(
            TripleExpr::TripleConstraint {
                id: None,
                negated: None,
                inverse: None,
                predicate: IriS::new_unchecked("http://a.example/p1").into(),
                value_expr: Some(Box::new(ShapeExpr::Ref(ShapeExprLabel::IriRef {
                    value: IriRef::iri(IriS::new_unchecked("http://all.example/S5")),
                }))),
                min: None,
                max: None,
                sem_acts: None,
                annotations: None,
            },
        ));
        assert_eq!(se, expected);
    }

    #[test]
    fn test_triple_constraint1() {
        let str = r#"{
 "type": "TripleConstraint",
 "predicate": "http://a.example/p1",
 "valueExpr": "http://all.example/S5"
}"#;
        let te = serde_json::from_str::<TripleExpr>(str).unwrap();
        let p1 = IriS::from_str("http://a.example/p1").unwrap();
        let s5 = IriS::from_str("http://all.example/S5").unwrap();
        let expected = TripleExpr::TripleConstraint {
            id: None,
            negated: None,
            inverse: None,
            predicate: p1.into(),
            value_expr: Some(Box::new(ShapeExpr::Ref(ShapeExprLabel::IriRef {
                value: IriRef::iri(s5),
            }))),
            max: None,
            min: None,
            sem_acts: None,
            annotations: None,
        };
        assert_eq!(te, expected);
    }

    #[test]
    fn test_json() {
        let str = r#"{
            "type": "NodeConstraint",
            "values": [
                {
                    "value": "0",
                    "type": "http://www.w3.org/2001/XMLSchema#integer"
                }
             ]
          }"#;

        let shape_expr = serde_json::from_str::<ShapeExpr>(str);
        if let Ok(v) = &shape_expr {
            let _serialized = serde_json::to_string(v).unwrap();
        }
        assert!(shape_expr.is_ok())
    }

    #[test]
    fn test_triple() {
        let str = r#"{
            "type": "Shape",
            "expression": "http://all.example/S2e"
          }"#;

        let shape_expr = serde_json::from_str::<ShapeExpr>(str);
        if let Ok(v) = &shape_expr {
            serde_json::to_string(v).unwrap();
        }
        assert!(shape_expr.is_ok())
    }
}