shacl_validation/constraints/
mod.rs

1use constraint_error::ConstraintError;
2use shacl_ast::compiled::component::CompiledComponent;
3use shacl_ast::compiled::shape::CompiledShape;
4use srdf::Query;
5use srdf::Rdf;
6use srdf::Sparql;
7use std::fmt::Debug;
8
9use crate::engine::Engine;
10use crate::validation_report::result::ValidationResult;
11use crate::value_nodes::ValueNodes;
12
13pub mod constraint_error;
14pub mod core;
15
16pub trait Validator<S: Rdf + Debug> {
17    fn validate(
18        &self,
19        component: &CompiledComponent<S>,
20        shape: &CompiledShape<S>,
21        store: &S,
22        engine: impl Engine<S>,
23        value_nodes: &ValueNodes<S>,
24        source_shape: Option<&CompiledShape<S>>,
25    ) -> Result<Vec<ValidationResult>, ConstraintError>;
26}
27
28pub trait NativeValidator<S: Query> {
29    fn validate_native(
30        &self,
31        component: &CompiledComponent<S>,
32        shape: &CompiledShape<S>,
33        store: &S,
34        value_nodes: &ValueNodes<S>,
35        source_shape: Option<&CompiledShape<S>>,
36    ) -> Result<Vec<ValidationResult>, ConstraintError>;
37}
38
39pub trait SparqlValidator<S: Sparql + Debug> {
40    fn validate_sparql(
41        &self,
42        component: &CompiledComponent<S>,
43        shape: &CompiledShape<S>,
44        store: &S,
45        value_nodes: &ValueNodes<S>,
46        source_shape: Option<&CompiledShape<S>>,
47    ) -> Result<Vec<ValidationResult>, ConstraintError>;
48}
49
50macro_rules! generate_deref_fn {
51    ($enum_name:ident, $($variant:ident),+) => {
52        fn deref(&self) -> &Self::Target {
53            match self {
54                $( $enum_name::$variant(inner) => inner, )+
55            }
56        }
57    };
58}
59
60pub trait NativeDeref {
61    type Target: ?Sized;
62
63    fn deref(&self) -> &Self::Target;
64}
65
66impl<S: Query + Debug + 'static> NativeDeref for CompiledComponent<S> {
67    type Target = dyn NativeValidator<S>;
68
69    generate_deref_fn!(
70        CompiledComponent,
71        Class,
72        Datatype,
73        NodeKind,
74        MinCount,
75        MaxCount,
76        MinExclusive,
77        MaxExclusive,
78        MinInclusive,
79        MaxInclusive,
80        MinLength,
81        MaxLength,
82        Pattern,
83        UniqueLang,
84        LanguageIn,
85        Equals,
86        Disjoint,
87        LessThan,
88        LessThanOrEquals,
89        Or,
90        And,
91        Not,
92        Xone,
93        Closed,
94        Node,
95        HasValue,
96        In,
97        QualifiedValueShape
98    );
99}
100
101pub trait SparqlDeref {
102    type Target: ?Sized;
103
104    fn deref(&self) -> &Self::Target;
105}
106
107impl<S: Sparql + Debug + 'static> SparqlDeref for CompiledComponent<S> {
108    type Target = dyn SparqlValidator<S>;
109
110    generate_deref_fn!(
111        CompiledComponent,
112        Class,
113        Datatype,
114        NodeKind,
115        MinCount,
116        MaxCount,
117        MinExclusive,
118        MaxExclusive,
119        MinInclusive,
120        MaxInclusive,
121        MinLength,
122        MaxLength,
123        Pattern,
124        UniqueLang,
125        LanguageIn,
126        Equals,
127        Disjoint,
128        LessThan,
129        LessThanOrEquals,
130        Or,
131        And,
132        Not,
133        Xone,
134        Closed,
135        Node,
136        HasValue,
137        In,
138        QualifiedValueShape
139    );
140}