1use std::fmt;
4
5use iri_rfc3987::Iri;
6
7use crate::{
8 CustomConstraint, ExpressionConstraint, LanguageTag, NodeKind, ShaclPath, SparqlConstraint,
9};
10
11pub type DefaultValue = crate::DefaultTerm;
13
14#[derive(
23 Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
24)]
25#[serde(
26 deny_unknown_fields,
27 bound(
28 serialize = "Value: serde::Serialize, Count: serde::Serialize, Flags: serde::Serialize, Pattern: serde::Serialize, Named: serde::Serialize",
29 deserialize = "Value: serde::Deserialize<'de>, Count: serde::Deserialize<'de>, Flags: serde::Deserialize<'de>, Pattern: serde::Deserialize<'de>, Named: serde::Deserialize<'de>"
30 )
31)]
32pub enum Component<
33 Value = DefaultValue,
34 Count = Value,
35 Flags = Value,
36 Pattern = Value,
37 Named = Iri<String>,
38> {
39 Class(Named),
41 Datatype(Named),
43 NodeKind(NodeKind),
45 MinCount(Count),
47 MaxCount(Count),
49 MinExclusive(Value),
51 MinInclusive(Value),
53 MaxExclusive(Value),
55 MaxInclusive(Value),
57 MinLength(Count),
59 MaxLength(Count),
61 Pattern {
63 pattern: Pattern,
65 flags: Option<Flags>,
67 },
68 LanguageIn(Vec<LanguageTag>),
70 UniqueLang(bool),
72 Equals(ShaclPath<Named>),
74 Disjoint(ShaclPath<Named>),
76 LessThan(ShaclPath<Named>),
78 LessThanOrEquals(ShaclPath<Named>),
80 And(Vec<Value>),
82 Or(Vec<Value>),
84 Not(Value),
86 Xone(Vec<Value>),
88 Node(Value),
90 QualifiedValueShape {
92 shape: Value,
94 min_count: Option<Count>,
96 max_count: Option<Count>,
98 disjoint: bool,
100 },
101 HasValue(Value),
103 In(Vec<Value>),
105 Closed {
107 is_closed: bool,
109 ignored_properties: Vec<Named>,
111 },
112 Sparql(Box<SparqlConstraint<Value>>),
114 Expression(Box<ExpressionConstraint<Value, Named>>),
116 Custom(CustomConstraint<Value, Named>),
118 SingleLine(bool),
124 MemberShape(Value),
129 MinListLength(Count),
133 MaxListLength(Count),
137 UniqueMembers(bool),
142}
143
144impl<Value, Count, Flags, Pattern, Named> Component<Value, Count, Flags, Pattern, Named> {
145 #[must_use]
147 pub const fn kind_name(&self) -> &'static str {
148 match self {
149 Self::Class(_) => "class",
150 Self::Datatype(_) => "datatype",
151 Self::NodeKind(_) => "nodeKind",
152 Self::MinCount(_) => "minCount",
153 Self::MaxCount(_) => "maxCount",
154 Self::MinExclusive(_) => "minExclusive",
155 Self::MinInclusive(_) => "minInclusive",
156 Self::MaxExclusive(_) => "maxExclusive",
157 Self::MaxInclusive(_) => "maxInclusive",
158 Self::MinLength(_) => "minLength",
159 Self::MaxLength(_) => "maxLength",
160 Self::Pattern { .. } => "pattern",
161 Self::LanguageIn(_) => "languageIn",
162 Self::UniqueLang(_) => "uniqueLang",
163 Self::Equals(_) => "equals",
164 Self::Disjoint(_) => "disjoint",
165 Self::LessThan(_) => "lessThan",
166 Self::LessThanOrEquals(_) => "lessThanOrEquals",
167 Self::And(_) => "and",
168 Self::Or(_) => "or",
169 Self::Not(_) => "not",
170 Self::Xone(_) => "xone",
171 Self::Node(_) => "node",
172 Self::QualifiedValueShape { .. } => "qualifiedValueShape",
173 Self::HasValue(_) => "hasValue",
174 Self::In(_) => "in",
175 Self::Closed { .. } => "closed",
176 Self::Sparql(_) => "sparql",
177 Self::Expression(_) => "expression",
178 Self::Custom(_) => "custom",
179 Self::SingleLine(_) => "singleLine",
180 Self::MemberShape(_) => "memberShape",
181 Self::MinListLength(_) => "minListLength",
182 Self::MaxListLength(_) => "maxListLength",
183 Self::UniqueMembers(_) => "uniqueMembers",
184 }
185 }
186}
187
188impl<
189 Value: fmt::Debug,
190 Count: fmt::Debug,
191 Flags: fmt::Debug,
192 Pattern: fmt::Debug,
193 Named: AsRef<str>,
194> Component<Value, Count, Flags, Pattern, Named>
195{
196 #[must_use]
198 pub fn display_value(&self) -> String {
199 match self {
200 Self::Class(iri) | Self::Datatype(iri) => crate::local_name(iri),
201 Self::NodeKind(kind) => format!("{kind:?}"),
202 Self::MinCount(count)
203 | Self::MaxCount(count)
204 | Self::MinLength(count)
205 | Self::MaxLength(count)
206 | Self::MinListLength(count)
207 | Self::MaxListLength(count) => format!("{count:?}"),
208 Self::MinExclusive(value)
209 | Self::MinInclusive(value)
210 | Self::MaxExclusive(value)
211 | Self::MaxInclusive(value)
212 | Self::HasValue(value)
213 | Self::Not(value)
214 | Self::Node(value)
215 | Self::MemberShape(value) => format!("{value:?}"),
216 Self::Pattern { pattern, flags } => match flags {
217 Some(flags) => format!("/{pattern:?}/{flags:?}"),
218 None => format!("/{pattern:?}/"),
219 },
220 Self::LanguageIn(languages) => languages
221 .iter()
222 .map(LanguageTag::as_str)
223 .collect::<Vec<_>>()
224 .join(", "),
225 Self::UniqueLang(value) | Self::SingleLine(value) | Self::UniqueMembers(value) => {
226 value.to_string()
227 }
228 Self::Equals(path)
229 | Self::Disjoint(path)
230 | Self::LessThan(path)
231 | Self::LessThanOrEquals(path) => path.to_string(),
232 Self::And(iris) | Self::Or(iris) | Self::Xone(iris) => iris
233 .iter()
234 .map(|value| format!("{value:?}"))
235 .collect::<Vec<_>>()
236 .join(", "),
237 Self::QualifiedValueShape {
238 shape,
239 min_count,
240 max_count,
241 ..
242 } => {
243 let mut parts = vec![format!("{shape:?}")];
244 if let Some(min) = min_count {
245 parts.push(format!("min={min:?}"));
246 }
247 if let Some(max) = max_count {
248 parts.push(format!("max={max:?}"));
249 }
250 parts.join(", ")
251 }
252 Self::In(values) => values
253 .iter()
254 .map(|value| format!("{value:?}"))
255 .collect::<Vec<_>>()
256 .join(", "),
257 Self::Closed {
258 is_closed,
259 ignored_properties,
260 } => {
261 if *is_closed {
262 if ignored_properties.is_empty() {
263 "true".to_owned()
264 } else {
265 let ignored = ignored_properties
266 .iter()
267 .map(crate::local_name)
268 .collect::<Vec<_>>();
269 format!("true (ignore: {})", ignored.join(", "))
270 }
271 } else {
272 "false".to_owned()
273 }
274 }
275 Self::Sparql(constraint) => format!("{:?}", constraint.select.text),
276 Self::Expression(constraint) => format!("{:?}", constraint.expression.node),
277 Self::Custom(constraint) => crate::local_name(&constraint.component),
278 }
279 }
280}