shacl-structural-model
Typed structural values for SHACL shapes in Rust.
shacl-structural-model represents SHACL shapes, property paths, targets,
constraint components, severities, RDF messages, SPARQL declarations, and
SHACL Advanced Features functions, expressions and rules. It is intended for parsers,
editors, validators, serializers, and storage adapters that need typed shape
values without taking a dependency on an RDF graph implementation.
The default value type is [rdf_syntax::Term]. The structural types are also
generic over the value type, so an application can use its own RDF term model
without copying or converting RDF-valued fields. Shape IDs and references
retain blank nodes; target nodes can also be literals. Messages keep language
tags and datatypes. Numeric ordering literals retain their lexical precision.
This crate models structural values only. It does not parse RDF, read or write files, access a graph store, execute validation, run SPARQL, or collect an entire shapes graph. Callers should stream shapes from their input and retain only the structural values needed by their operation.
W3C specifications
The stable baseline is the SHACL Recommendation. SPARQL and AF declarations follow that recommendation and SHACL Advanced Features. Representing these declarations does not establish validator conformance or pySHACL compatibility. SHACL-JS and complete SHACL 1.2 draft coverage are outside the current scope.
Example
use Iri;
use ;
let person: = "https://example.org/Person".parse.expect;
let name: = "https://example.org/name".parse.expect;
let name_shape = new
.with_component;
let person_shape = new
.with_target
.with_property_shape;
assert_eq!;
assert_eq!;
Value types
RDF-valued fields default to rdf_syntax::Term. Applications can instead use
oxrdf::Term, references to their existing terms, or opaque term handles.
Construction has no Clone, Debug, or Serde bounds; those traits are required
only when calling the corresponding operations. IRIs in IRI-only roles, such
as predicates and datatypes, continue to use iri_rfc3987::Iri<String>.
use Component;
let constraint: = Pattern ;
assert_eq!;
Count and length parameters preserve the caller's RDF value type, including
signed and arbitrary-size integers. Component<Value, Count>,
NodeShape<Value, Count>, PropertyShape<Value, Count>, and parameterized
declarations also accept a separate compiled count type; Count defaults to
Value. The model does not parse lexical forms, impose a u64 limit, or read
literal payloads. Order values stay as RDF terms, independently of Count.
SPARQL and Advanced Features
| Declaration | Structural types |
|---|---|
| SELECT constraints, ASK/SELECT validators, result annotations | SparqlConstraint, SparqlValidator, ResultAnnotation |
| Query literals, prefix resources and imports | SparqlQuery, PrefixDeclaration, PrefixDeclarations |
| Custom components and parameter values | CustomConstraintComponent, CustomConstraint, Parameter, ParameterValues |
| Direct SPARQL targets and reusable target types | SparqlTarget, SparqlTargetType, CustomTarget |
| ASK/SELECT functions | ShaclFunction, SparqlSelectOrAsk |
| All seven AF node-expression forms, including nested path inputs | NodeExpression, ExpressionKind, ExpressionConstraint |
| Triple and SPARQL rules, conditions, order and deactivation | ShaclRule, RuleKind, TripleRule |
Declarations retain source nodes and RDF-valued messages. Parameter values are kept in full before an engine constructs query bindings. Prefix resources and imports are references, so shared declarations need not be copied into every query. Property shapes support targets as well as node shapes. Implicit class targets and custom target-type identifiers use the caller's RDF value type, preserving blank-node identity as well as named resources.
use ;
let blank = ;
let query = new
.with_prefixes;
let shape = new
.with_component;
assert_eq!;
Scope and memory
The crate exposes individual shapes and nested structural values. It does not
provide a document-sized Schema collection, so a parser can emit shapes one
at a time and let the caller decide whether to validate, serialize, persist,
or retain them. Collections inside a single shape represent that shape's
direct structural values; they are not a replacement for streaming an entire
input document.
This is a parsed structural model, not an RDF graph archive. Paths retain
their structure but not all RDF-list cell identities. Boolean flags and Core
pattern/language strings are interpreted fields. Callers that need exact
source triples or graph context must retain those separately. path_node
can retain a path's source root for result reporting. Display helpers are
human-readable labels, not RDF or SPARQL serialization.
The parser/consumer checks RDF node kinds, cardinality, query syntax, shape
references and expression cycles. This crate does not inspect opaque Value
payloads or certify a declaration as well-formed. Serde rejects unrecognized
fields on shapes and the new declarations, rather than discarding them.
Migrating from 2.x
Target::ImplicitClass, SparqlTargetType::id and CustomTarget::target_type
now hold Value rather than Iri<String>. Pass the corresponding RDF term,
including for named resources. Target::iri() and NodeShape::target_class_iris()
cover explicit IRI-valued targets; they do not inspect opaque implicit-class
terms. This is a breaking API and serialized-shape change for callers whose
RDF value representation differs from an IRI string.
Migrating from 1.x
This is a breaking API and Serde format change:
- Shape IDs, shape references,
Target::Node, and message builders now takeValue. Construct an RDF IRI/blank-node/literal term for those fields. PropertyShape::nameanddescriptionbecomenamesanddescriptions: vectors of RDF literals. Their builders append values, retaining languages.- Shape and rule
ordervalues useOption<Value>, replacing floating-point ordering on property shapes. Node shapes now also haveorder. ShaclRule::SparqlbecomesShaclRule::new(id, RuleKind::Sparql(query)). Query text is an RDF literal inSparqlQuery, with explicit prefix references.Severitysupports custom IRIs and is no longerCopy. Its accessors borrow the value. Invalid text returnsInvalidSeverityIri.- Shape
local_name()methods are removed because IDs need not be IRIs. Inspect the caller's term type and uselocal_namefor actual IRIs.Target::iri()now returnsOptionfor Core class/predicate targets. - Exhaustive matches on
Component,Target, andSeverityneed the new variants. Migrate stored Serde values explicitly; no legacy fields are ignored.
License
Licensed under either of:
- Apache License, Version 2.0
- MIT License
at your option.