shacl-structural-model 2.0.0

Typed SHACL structural values for Rust
Documentation
//! SHACL node shapes.

use iri_rfc3987::Iri;

use crate::{Component, Severity, ShaclRule, Target};

/// A SHACL node shape.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(
    deny_unknown_fields,
    bound(
        serialize = "Value: serde::Serialize, Count: serde::Serialize",
        deserialize = "Value: serde::Deserialize<'de>, Count: serde::Deserialize<'de>"
    )
)]
pub struct NodeShape<Value = crate::DefaultTerm, Count = Value> {
    /// The IRI or blank node identifying this shape.
    pub id: Value,
    /// Target declarations.
    pub targets: Vec<Target<Value>>,
    /// Constraint components applied to the focus node.
    pub components: Vec<Component<Value, Count>>,
    /// IRI or blank-node references to attached property shapes.
    pub property_shapes: Vec<Value>,
    /// Whether this shape is deactivated.
    pub deactivated: bool,
    /// The severity for results from this shape.
    pub severity: Severity,
    /// RDF `sh:message` literals, including any language tags.
    pub messages: Vec<Value>,
    /// SHACL-AF rules attached to this shape.
    pub rules: Vec<ShaclRule<Value>>,
    /// The original numeric `sh:order` literal for AF rule execution.
    pub order: Option<Value>,
}

impl<Value, Count> NodeShape<Value, Count> {
    /// Create an empty node shape with the given IRI or blank-node term.
    #[must_use]
    pub fn new(id: Value) -> Self {
        Self {
            id,
            targets: Vec::new(),
            components: Vec::new(),
            property_shapes: Vec::new(),
            deactivated: false,
            severity: Severity::default(),
            messages: Vec::new(),
            rules: Vec::new(),
            order: None,
        }
    }

    /// Add a target declaration.
    #[must_use]
    pub fn with_target(mut self, target: Target<Value>) -> Self {
        self.targets.push(target);
        self
    }

    /// Add a constraint component.
    #[must_use]
    pub fn with_component(mut self, component: Component<Value, Count>) -> Self {
        self.components.push(component);
        self
    }

    /// Add a property-shape reference.
    #[must_use]
    pub fn with_property_shape(mut self, shape: Value) -> Self {
        self.property_shapes.push(shape);
        self
    }

    /// Set whether this shape is deactivated.
    #[must_use]
    pub fn with_deactivated(mut self, deactivated: bool) -> Self {
        self.deactivated = deactivated;
        self
    }

    /// Set the severity level.
    #[must_use]
    pub fn with_severity(mut self, severity: Severity) -> Self {
        self.severity = severity;
        self
    }

    /// Add a custom validation message.
    #[must_use]
    pub fn with_message(mut self, message: Value) -> Self {
        self.messages.push(message);
        self
    }

    /// Set the original numeric execution-order literal.
    #[must_use]
    pub fn with_order(mut self, order: Value) -> Self {
        self.order = Some(order);
        self
    }

    /// Attach a rule declaration.
    #[must_use]
    pub fn with_rule(mut self, rule: ShaclRule<Value>) -> Self {
        self.rules.push(rule);
        self
    }

    /// Iterate over class and implicit-class target IRIs.
    pub fn target_class_iris(&self) -> impl Iterator<Item = &Iri<String>> + '_ {
        self.targets.iter().filter_map(|target| match target {
            Target::Class(iri) | Target::ImplicitClass(iri) => Some(iri),
            _ => None,
        })
    }

    /// Return the number of attached property-shape references.
    #[must_use]
    pub fn property_count(&self) -> usize {
        self.property_shapes.len()
    }
}