shacl_ast/compiled/
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
use compiled_shacl_error::CompiledShaclError;
use prefixmap::IriRef;
use shape::CompiledShape;
use srdf::lang::Lang;
use srdf::literal::Literal;
use srdf::Object;
use srdf::RDFNode;
use srdf::SRDFBasic;

use crate::value::Value;
use crate::Schema;

pub mod compiled_shacl_error;
pub mod component;
pub mod node_shape;
pub mod property_shape;
pub mod schema;
pub mod severity;
pub mod shape;
pub mod target;

fn convert_iri_ref<S: SRDFBasic>(iri_ref: IriRef) -> Result<S::IRI, CompiledShaclError> {
    let iri_s = iri_ref
        .get_iri()
        .map_err(|_| CompiledShaclError::IriRefConversion)?;
    Ok(S::iri_s2iri(&iri_s))
}

fn convert_lang<S: SRDFBasic>(lang: Lang) -> Result<S::Literal, CompiledShaclError> {
    let object = RDFNode::literal(Literal::str(&lang.value()));
    let term = S::object_as_term(&object);
    S::term_as_literal(&term).ok_or(CompiledShaclError::LiteralConversion)
}

fn compile_shape<S: SRDFBasic>(
    shape: Object,
    schema: &Schema,
) -> Result<CompiledShape<S>, CompiledShaclError> {
    let shape = schema
        .get_shape(&shape)
        .ok_or(CompiledShaclError::ShapeNotFound)?;
    CompiledShape::compile(shape.to_owned(), schema)
}

fn compile_shapes<S: SRDFBasic>(
    shapes: Vec<Object>,
    schema: &Schema,
) -> Result<Vec<CompiledShape<S>>, CompiledShaclError> {
    let compiled_shapes = shapes
        .into_iter()
        .map(|shape| compile_shape::<S>(shape, schema))
        .collect::<Result<Vec<_>, _>>()?;
    Ok(compiled_shapes)
}

fn convert_value<S: SRDFBasic>(value: Value) -> Result<S::Term, CompiledShaclError> {
    let ans = match value {
        Value::Iri(iri_ref) => {
            let iri_ref = convert_iri_ref::<S>(iri_ref)?;
            S::iri_as_term(iri_ref)
        }
        Value::Literal(literal) => S::object_as_term(&RDFNode::literal(literal)),
    };
    Ok(ans)
}