shacl-structural-model 6.2.2

Typed SHACL structural values for Rust
Documentation
//! SHACL Core constraint components.

use std::fmt;

use iri_rfc3987::Iri;

use crate::{CustomConstraint, ExpressionConstraint, NodeKind, ShaclPath, SparqlConstraint};

/// The default value type used by [`Component`].
pub type DefaultValue = crate::DefaultTerm;

/// A Core, SPARQL, or AF constraint declaration applied to a shape.
///
/// Integer count and length parameters use `Count`, which defaults to the
/// caller's RDF value type. Consumers may instead supply a compiled comparison
/// type without imposing a machine-integer limit on the structural model.
/// Regex flags and patterns use independent `Flags` and `Pattern` types,
/// also defaulting to `Value`. Consumers may retain an RDF reference beside
/// a compiled representation without requiring an owned lexical string.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(
    deny_unknown_fields,
    bound(
        serialize = "Value: serde::Serialize, Count: serde::Serialize, Flags: serde::Serialize, Pattern: serde::Serialize, Named: serde::Serialize",
        deserialize = "Value: serde::Deserialize<'de>, Count: serde::Deserialize<'de>, Flags: serde::Deserialize<'de>, Pattern: serde::Deserialize<'de>, Named: serde::Deserialize<'de>"
    )
)]
pub enum Component<
    Value = DefaultValue,
    Count = Value,
    Flags = Value,
    Pattern = Value,
    Named = Iri<String>,
> {
    /// `sh:class` — the value must be an instance of the class.
    Class(Named),
    /// `sh:datatype` — the value must have the given datatype.
    Datatype(Named),
    /// `sh:nodeKind` — the value must have the given RDF node kind.
    NodeKind(NodeKind),
    /// `sh:minCount`.
    MinCount(Count),
    /// `sh:maxCount`.
    MaxCount(Count),
    /// `sh:minExclusive`.
    MinExclusive(Value),
    /// `sh:minInclusive`.
    MinInclusive(Value),
    /// `sh:maxExclusive`.
    MaxExclusive(Value),
    /// `sh:maxInclusive`.
    MaxInclusive(Value),
    /// `sh:minLength`.
    MinLength(Count),
    /// `sh:maxLength`.
    MaxLength(Count),
    /// `sh:pattern` with optional `sh:flags`.
    Pattern {
        /// Original RDF pattern value, or a caller-defined compiled representation.
        pattern: Pattern,
        /// Original RDF flags value, or a caller-defined compiled representation.
        flags: Option<Flags>,
    },
    /// `sh:languageIn`.
    LanguageIn(Vec<String>),
    /// `sh:uniqueLang`.
    UniqueLang(bool),
    /// `sh:equals`.
    Equals(ShaclPath<Named>),
    /// `sh:disjoint`.
    Disjoint(ShaclPath<Named>),
    /// `sh:lessThan`.
    LessThan(ShaclPath<Named>),
    /// `sh:lessThanOrEquals`.
    LessThanOrEquals(ShaclPath<Named>),
    /// `sh:and`.
    And(Vec<Value>),
    /// `sh:or`.
    Or(Vec<Value>),
    /// `sh:not`.
    Not(Value),
    /// `sh:xone`.
    Xone(Vec<Value>),
    /// `sh:node`.
    Node(Value),
    /// `sh:qualifiedValueShape` with optional min/max counts.
    QualifiedValueShape {
        /// The shape to count conforming values against.
        shape: Value,
        /// Minimum number of conforming values.
        min_count: Option<Count>,
        /// Maximum number of conforming values.
        max_count: Option<Count>,
        /// Whether sibling qualified shapes use disjoint value sets.
        disjoint: bool,
    },
    /// `sh:hasValue`.
    HasValue(Value),
    /// `sh:in`.
    In(Vec<Value>),
    /// `sh:closed` with `sh:ignoredProperties`.
    Closed {
        /// Whether the shape is closed.
        is_closed: bool,
        /// Properties exempt from the closure check.
        ignored_properties: Vec<Named>,
    },
    /// A `sh:sparql` constraint declaration.
    Sparql(Box<SparqlConstraint<Value>>),
    /// A `sh:expression` constraint declaration.
    Expression(Box<ExpressionConstraint<Value, Named>>),
    /// A use of a separately declared custom component.
    Custom(CustomConstraint<Value, Named>),
    /// `sh:singleLine` from the SHACL 1.2 Core 2026-10-14 Working Draft.
    ///
    /// When true, literal values must not contain U+000A through U+000D.
    /// False explicitly permits multiline values. This declaration is at risk
    /// in the draft; representing it does not implement constraint evaluation.
    SingleLine(bool),
    /// `sh:memberShape` from the SHACL 1.2 Core 2026-10-14 Working Draft.
    ///
    /// Each value must be a well-formed SHACL list whose members conform to
    /// this caller-owned shape reference. The consumer checks and evaluates it.
    MemberShape(Value),
    /// Draft `sh:minListLength`, using the independent caller-owned count type.
    ///
    /// The consumer checks the non-negative integer parameter and list structure.
    MinListLength(Count),
    /// Draft `sh:maxListLength`, using the independent caller-owned count type.
    ///
    /// The consumer checks the non-negative integer parameter and list structure.
    MaxListLength(Count),
    /// Draft `sh:uniqueMembers` — require distinct members when true.
    ///
    /// Both true and false require a well-formed SHACL list. False relaxes only
    /// the uniqueness check, not list well-formedness. Evaluation is consumer-owned.
    UniqueMembers(bool),
}

impl<Value, Count, Flags, Pattern, Named> Component<Value, Count, Flags, Pattern, Named> {
    /// Return the SHACL constraint kind name.
    #[must_use]
    pub const fn kind_name(&self) -> &'static str {
        match self {
            Self::Class(_) => "class",
            Self::Datatype(_) => "datatype",
            Self::NodeKind(_) => "nodeKind",
            Self::MinCount(_) => "minCount",
            Self::MaxCount(_) => "maxCount",
            Self::MinExclusive(_) => "minExclusive",
            Self::MinInclusive(_) => "minInclusive",
            Self::MaxExclusive(_) => "maxExclusive",
            Self::MaxInclusive(_) => "maxInclusive",
            Self::MinLength(_) => "minLength",
            Self::MaxLength(_) => "maxLength",
            Self::Pattern { .. } => "pattern",
            Self::LanguageIn(_) => "languageIn",
            Self::UniqueLang(_) => "uniqueLang",
            Self::Equals(_) => "equals",
            Self::Disjoint(_) => "disjoint",
            Self::LessThan(_) => "lessThan",
            Self::LessThanOrEquals(_) => "lessThanOrEquals",
            Self::And(_) => "and",
            Self::Or(_) => "or",
            Self::Not(_) => "not",
            Self::Xone(_) => "xone",
            Self::Node(_) => "node",
            Self::QualifiedValueShape { .. } => "qualifiedValueShape",
            Self::HasValue(_) => "hasValue",
            Self::In(_) => "in",
            Self::Closed { .. } => "closed",
            Self::Sparql(_) => "sparql",
            Self::Expression(_) => "expression",
            Self::Custom(_) => "custom",
            Self::SingleLine(_) => "singleLine",
            Self::MemberShape(_) => "memberShape",
            Self::MinListLength(_) => "minListLength",
            Self::MaxListLength(_) => "maxListLength",
            Self::UniqueMembers(_) => "uniqueMembers",
        }
    }
}

impl<
    Value: fmt::Debug,
    Count: fmt::Debug,
    Flags: fmt::Debug,
    Pattern: fmt::Debug,
    Named: AsRef<str>,
> Component<Value, Count, Flags, Pattern, Named>
{
    /// Return a human-readable display value for this component.
    #[must_use]
    pub fn display_value(&self) -> String {
        match self {
            Self::Class(iri) | Self::Datatype(iri) => crate::local_name(iri),
            Self::NodeKind(kind) => format!("{kind:?}"),
            Self::MinCount(count)
            | Self::MaxCount(count)
            | Self::MinLength(count)
            | Self::MaxLength(count)
            | Self::MinListLength(count)
            | Self::MaxListLength(count) => format!("{count:?}"),
            Self::MinExclusive(value)
            | Self::MinInclusive(value)
            | Self::MaxExclusive(value)
            | Self::MaxInclusive(value)
            | Self::HasValue(value)
            | Self::Not(value)
            | Self::Node(value)
            | Self::MemberShape(value) => format!("{value:?}"),
            Self::Pattern { pattern, flags } => match flags {
                Some(flags) => format!("/{pattern:?}/{flags:?}"),
                None => format!("/{pattern:?}/"),
            },
            Self::LanguageIn(languages) => languages.join(", "),
            Self::UniqueLang(value) | Self::SingleLine(value) | Self::UniqueMembers(value) => {
                value.to_string()
            }
            Self::Equals(path)
            | Self::Disjoint(path)
            | Self::LessThan(path)
            | Self::LessThanOrEquals(path) => path.to_string(),
            Self::And(iris) | Self::Or(iris) | Self::Xone(iris) => iris
                .iter()
                .map(|value| format!("{value:?}"))
                .collect::<Vec<_>>()
                .join(", "),
            Self::QualifiedValueShape {
                shape,
                min_count,
                max_count,
                ..
            } => {
                let mut parts = vec![format!("{shape:?}")];
                if let Some(min) = min_count {
                    parts.push(format!("min={min:?}"));
                }
                if let Some(max) = max_count {
                    parts.push(format!("max={max:?}"));
                }
                parts.join(", ")
            }
            Self::In(values) => values
                .iter()
                .map(|value| format!("{value:?}"))
                .collect::<Vec<_>>()
                .join(", "),
            Self::Closed {
                is_closed,
                ignored_properties,
            } => {
                if *is_closed {
                    if ignored_properties.is_empty() {
                        "true".to_owned()
                    } else {
                        let ignored = ignored_properties
                            .iter()
                            .map(crate::local_name)
                            .collect::<Vec<_>>();
                        format!("true (ignore: {})", ignored.join(", "))
                    }
                } else {
                    "false".to_owned()
                }
            }
            Self::Sparql(constraint) => format!("{:?}", constraint.select.text),
            Self::Expression(constraint) => format!("{:?}", constraint.expression.node),
            Self::Custom(constraint) => crate::local_name(&constraint.component),
        }
    }
}