Skip to main content

shacl_structural_model/
target.rs

1//! SHACL target declarations.
2
3use iri_rfc3987::Iri;
4
5use crate::{Parameter, ParameterValues, SparqlQuery, local_name};
6
7/// A target declaration on a node or property shape.
8#[derive(
9    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
10)]
11#[serde(deny_unknown_fields)]
12pub enum Target<Value = crate::DefaultTerm, Named = Iri<String>> {
13    /// `sh:targetNode` — the given node is a focus node.
14    Node(Value),
15    /// `sh:targetClass` — instances of the class are focus nodes.
16    Class(Named),
17    /// `sh:targetSubjectsOf` — subjects of the predicate are focus nodes.
18    SubjectsOf(Named),
19    /// `sh:targetObjectsOf` — objects of the predicate are focus nodes.
20    ObjectsOf(Named),
21    /// An implicit class target, retaining an IRI or blank-node class term.
22    ImplicitClass(Value),
23    /// A direct SHACL-AF SPARQL target.
24    Sparql(Box<SparqlTarget<Value>>),
25    /// An application of a separately declared target type.
26    Custom(CustomTarget<Value, Named>),
27}
28
29impl<Value, Named> Target<Value, Named> {
30    /// Return the SHACL target kind name.
31    #[must_use]
32    pub const fn kind_name(&self) -> &'static str {
33        match self {
34            Self::Node(_) => "targetNode",
35            Self::Class(_) => "targetClass",
36            Self::SubjectsOf(_) => "targetSubjectsOf",
37            Self::ObjectsOf(_) => "targetObjectsOf",
38            Self::ImplicitClass(_) => "implicitClass",
39            Self::Sparql(_) => "SPARQLTarget",
40            Self::Custom(_) => "target",
41        }
42    }
43
44    /// Return an IRI-valued Core class or predicate target, when applicable.
45    ///
46    /// Node and implicit-class targets use the caller's term representation,
47    /// even for IRI nodes; this helper does not inspect those opaque values.
48    #[must_use]
49    pub fn iri(&self) -> Option<&Named> {
50        match self {
51            Self::Class(iri) | Self::SubjectsOf(iri) | Self::ObjectsOf(iri) => Some(iri),
52            Self::Node(_) | Self::ImplicitClass(_) | Self::Sparql(_) | Self::Custom(_) => None,
53        }
54    }
55}
56
57impl<Value: std::fmt::Debug, Named: AsRef<str>> Target<Value, Named> {
58    /// Return a human-readable label, not an RDF serialization.
59    #[must_use]
60    pub fn display(&self) -> String {
61        match self {
62            Self::Class(iri) | Self::SubjectsOf(iri) | Self::ObjectsOf(iri) => local_name(iri),
63            Self::Node(value) | Self::ImplicitClass(value) => format!("{value:?}"),
64            Self::Sparql(target) => format!("{:?}", target.id),
65            Self::Custom(target) => format!("{:?}", target.target_type),
66        }
67    }
68}
69
70/// A direct `sh:SPARQLTarget` declaration.
71#[derive(
72    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
73)]
74#[serde(deny_unknown_fields)]
75pub struct SparqlTarget<Value = crate::DefaultTerm> {
76    /// The target's IRI or blank node.
77    pub id: Value,
78    /// The SELECT query identifying focus nodes.
79    pub select: SparqlQuery<Value>,
80    /// Optional ASK query testing target membership.
81    pub ask: Option<SparqlQuery<Value>>,
82}
83
84impl<Value> SparqlTarget<Value> {
85    /// Create a target without an explicit ASK query.
86    #[must_use]
87    pub fn new(id: Value, select: SparqlQuery<Value>) -> Self {
88        Self {
89            id,
90            select,
91            ask: None,
92        }
93    }
94}
95
96/// A reusable, parameterized `sh:SPARQLTargetType` declaration.
97#[derive(
98    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
99)]
100#[serde(
101    deny_unknown_fields,
102    bound(
103        serialize = "Value: serde::Serialize, Count: serde::Serialize, Flags: serde::Serialize, Pattern: serde::Serialize, Named: serde::Serialize",
104        deserialize = "Value: serde::Deserialize<'de>, Count: serde::Deserialize<'de>, Flags: serde::Deserialize<'de>, Pattern: serde::Deserialize<'de>, Named: serde::Deserialize<'de>"
105    )
106)]
107pub struct SparqlTargetType<
108    Value = crate::DefaultTerm,
109    Count = Value,
110    Flags = Value,
111    Pattern = Value,
112    Named = Iri<String>,
113> {
114    /// The target type's IRI or blank-node term.
115    pub id: Value,
116    /// Parameter declarations in caller order.
117    pub parameters: Vec<Parameter<Value, Count, Flags, Pattern, Named>>,
118    /// The SELECT query identifying focus nodes.
119    pub select: SparqlQuery<Value>,
120    /// Optional ASK query testing target membership.
121    pub ask: Option<SparqlQuery<Value>>,
122    /// RDF `sh:labelTemplate` literals.
123    pub label_templates: Vec<Value>,
124}
125
126impl<Value, Count, Flags, Pattern, Named> SparqlTargetType<Value, Count, Flags, Pattern, Named> {
127    /// Create a target type with no parameters, ASK query, or label templates.
128    #[must_use]
129    pub fn new(id: Value, select: SparqlQuery<Value>) -> Self {
130        Self {
131            id,
132            select,
133            ask: None,
134            parameters: Vec::new(),
135            label_templates: Vec::new(),
136        }
137    }
138}
139
140/// One use of a custom target type, with its supplied RDF parameter values.
141#[derive(
142    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
143)]
144#[serde(deny_unknown_fields)]
145pub struct CustomTarget<Value = crate::DefaultTerm, Named = Iri<String>> {
146    /// The target instance's IRI or blank node.
147    pub id: Value,
148    /// An IRI or blank-node reference to a separately supplied target type.
149    pub target_type: Value,
150    /// Supplied values; consumers check target parameter cardinality.
151    pub parameters: Vec<ParameterValues<Value, Named>>,
152}