shacl_ast/compiled/
shape.rs

1use iri_s::IriS;
2use srdf::Rdf;
3
4use crate::shape::Shape;
5use crate::Schema;
6
7use super::compiled_shacl_error::CompiledShaclError;
8use super::component::CompiledComponent;
9use super::node_shape::CompiledNodeShape;
10use super::property_shape::CompiledPropertyShape;
11use super::target::CompiledTarget;
12
13#[derive(Debug)]
14pub enum CompiledShape<S: Rdf> {
15    NodeShape(CompiledNodeShape<S>),
16    PropertyShape(CompiledPropertyShape<S>),
17}
18
19impl<S: Rdf> CompiledShape<S> {
20    pub fn is_deactivated(&self) -> &bool {
21        match self {
22            CompiledShape::NodeShape(ns) => ns.is_deactivated(),
23            CompiledShape::PropertyShape(ps) => ps.is_deactivated(),
24        }
25    }
26
27    pub fn id(&self) -> &S::Term {
28        match self {
29            CompiledShape::NodeShape(ns) => ns.id(),
30            CompiledShape::PropertyShape(ps) => ps.id(),
31        }
32    }
33
34    pub fn targets(&self) -> &Vec<CompiledTarget<S>> {
35        match self {
36            CompiledShape::NodeShape(ns) => ns.targets(),
37            CompiledShape::PropertyShape(ps) => ps.targets(),
38        }
39    }
40
41    pub fn components(&self) -> &Vec<CompiledComponent<S>> {
42        match self {
43            CompiledShape::NodeShape(ns) => ns.components(),
44            CompiledShape::PropertyShape(ps) => ps.components(),
45        }
46    }
47
48    pub fn property_shapes(&self) -> &Vec<CompiledShape<S>> {
49        match self {
50            CompiledShape::NodeShape(ns) => ns.property_shapes(),
51            CompiledShape::PropertyShape(ps) => ps.property_shapes(),
52        }
53    }
54
55    pub fn path(&self) -> Option<S::Term> {
56        match self {
57            CompiledShape::NodeShape(_) => None,
58            CompiledShape::PropertyShape(_ps) => todo!(),
59        }
60    }
61
62    pub fn path_str(&self) -> Option<String> {
63        match self {
64            CompiledShape::NodeShape(_) => None,
65            CompiledShape::PropertyShape(ps) => Some(ps.path().to_string()),
66        }
67    }
68
69    pub fn severity(&self) -> S::Term {
70        let iri_s: IriS = match self {
71            CompiledShape::NodeShape(ns) => ns.severity().into(),
72            CompiledShape::PropertyShape(ps) => ps.severity().into(),
73        };
74        let iri: S::IRI = iri_s.into(); // TODO: this can be avoided
75        iri.into()
76    }
77}
78
79impl<S: Rdf> CompiledShape<S> {
80    pub fn compile(shape: Shape, schema: &Schema) -> Result<Self, CompiledShaclError> {
81        let shape = match shape {
82            Shape::NodeShape(node_shape) => {
83                let node_shape = CompiledNodeShape::compile(node_shape, schema)?;
84                CompiledShape::NodeShape(node_shape)
85            }
86            Shape::PropertyShape(property_shape) => {
87                let property_shape = CompiledPropertyShape::compile(property_shape, schema)?;
88                CompiledShape::PropertyShape(property_shape)
89            }
90        };
91
92        Ok(shape)
93    }
94}