shex_ast/ast/
shape.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
use iri_s::IriS;
use prefixmap::PrefixMap;
use serde_derive::{Deserialize, Serialize};

use crate::{Annotation, SemAct, ShapeExprLabel, TripleExpr, TripleExprWrapper};
use prefixmap::{Deref, DerefError, IriRef};

#[derive(Deserialize, Serialize, Debug, Default, PartialEq, Clone)]
pub struct Shape {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub closed: Option<bool>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extra: Option<Vec<IriRef>>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expression: Option<TripleExprWrapper>,

    #[serde(default, rename = "semActs", skip_serializing_if = "Option::is_none")]
    pub sem_acts: Option<Vec<SemAct>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<Vec<Annotation>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub extends: Option<Vec<ShapeExprLabel>>,
}

impl Shape {
    pub fn new(
        closed: Option<bool>,
        extra: Option<Vec<IriRef>>,
        expression: Option<TripleExpr>,
    ) -> Self {
        Shape {
            closed,
            extra,
            expression: expression.map(|e| e.into()),
            sem_acts: None,
            annotations: None,
            extends: None,
        }
    }

    pub fn with_expression(mut self, expression: TripleExpr) -> Self {
        self.expression = Some(expression.into());
        self
    }

    pub fn with_sem_acts(mut self, sem_acts: Option<Vec<SemAct>>) -> Self {
        self.sem_acts = sem_acts;
        self
    }

    pub fn annotations(&self) -> Option<impl Iterator<Item = &Annotation>> {
        self.annotations.as_ref().map(|anns| anns.iter())
    }

    pub fn has_annotations(&self) -> bool {
        self.annotations.is_some()
    }

    pub fn with_annotations(mut self, annotations: Option<Vec<Annotation>>) -> Self {
        self.annotations = annotations;
        self
    }

    pub fn add_annotation(&mut self, annotation: Annotation) {
        if let Some(annotations) = &mut self.annotations {
            annotations.push(annotation)
        } else {
            self.annotations = Some(vec![annotation])
        }
    }

    pub fn with_extends(mut self, extends: Option<Vec<ShapeExprLabel>>) -> Self {
        self.extends = extends;
        self
    }

    pub fn add_extend(&mut self, extend: ShapeExprLabel) {
        match &mut self.extends {
            None => self.extends = Some(vec![extend]),
            Some(ref mut es) => es.push(extend),
        }
    }

    pub fn is_closed(&self) -> bool {
        self.closed.unwrap_or(false)
    }

    pub fn triple_expr(&self) -> Option<TripleExpr> {
        self.expression.as_ref().map(|tew| tew.te.clone())
    }
}

impl Deref for Shape {
    fn deref(
        &self,
        base: &Option<IriS>,
        prefixmap: &Option<PrefixMap>,
    ) -> Result<Self, DerefError> {
        let new_extra = match &self.extra {
            None => None,
            Some(es) => {
                let mut ves = Vec::new();
                for e in es {
                    ves.push(e.deref(base, prefixmap)?);
                }
                Some(ves)
            }
        };
        let new_expr = match &self.expression {
            None => None,
            Some(expr) => {
                let ed = expr.deref(base, prefixmap)?;
                Some(ed)
            }
        };
        let new_anns = match &self.annotations {
            None => None,
            Some(anns) => {
                let mut new_as = Vec::new();
                for a in anns {
                    new_as.push(a.deref(base, prefixmap)?);
                }
                Some(new_as)
            }
        };
        let new_sem_acts = match &self.sem_acts {
            None => None,
            Some(sem_acts) => {
                let mut new_sas = Vec::new();
                for sa in sem_acts {
                    new_sas.push(sa.deref(base, prefixmap)?);
                }
                Some(new_sas)
            }
        };
        let new_extends = match &self.extends {
            None => None,
            Some(extends) => {
                let mut new_extends = Vec::new();
                for e in extends {
                    new_extends.push(e.deref(base, prefixmap)?);
                }
                Some(new_extends)
            }
        };

        let shape = Shape {
            closed: self.closed,
            extra: new_extra,
            expression: new_expr,
            sem_acts: new_sem_acts,
            annotations: new_anns,
            extends: new_extends,
        };
        Ok(shape)
    }
}