sdml_core/model/constraints/formal/
sequences.rs1use crate::model::{
2 constraints::QuantifiedSentence, identifiers::Identifier, HasBody, HasSourceSpan, Span,
3};
4use std::collections::BTreeSet;
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug)]
15#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
16pub struct SequenceBuilder {
17 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
18 span: Option<Span>,
19 variables: Variables,
20 body: QuantifiedSentence,
21}
22
23#[derive(Clone, Debug)]
24#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
25pub enum Variables {
26 Named(NamedVariables),
27 Mapping(MappingVariable),
28}
29
30#[derive(Clone, Debug)]
31#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
32pub struct NamedVariables {
33 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
34 span: Option<Span>,
35 names: BTreeSet<Identifier>,
36}
37
38#[derive(Clone, Debug)]
39#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
40pub struct MappingVariable {
41 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
42 span: Option<Span>,
43 domain: Identifier,
44 range: Identifier,
45}
46
47impl HasBody for SequenceBuilder {
52 type Body = QuantifiedSentence;
53
54 fn body(&self) -> &Self::Body {
55 &self.body
56 }
57
58 fn body_mut(&mut self) -> &mut Self::Body {
59 &mut self.body
60 }
61
62 fn set_body(&mut self, body: Self::Body) {
63 self.body = body;
64 }
65}
66
67impl HasSourceSpan for SequenceBuilder {
68 fn with_source_span(self, span: Span) -> Self {
69 let mut self_mut = self;
70 self_mut.span = Some(span);
71 self_mut
72 }
73
74 fn source_span(&self) -> Option<&Span> {
75 self.span.as_ref()
76 }
77
78 fn set_source_span(&mut self, span: Span) {
79 self.span = Some(span);
80 }
81
82 fn unset_source_span(&mut self) {
83 self.span = None;
84 }
85}
86
87impl SequenceBuilder {
88 pub fn new<V, S>(variables: V, body: S) -> Self
93 where
94 V: Into<Variables>,
95 S: Into<QuantifiedSentence>,
96 {
97 Self {
98 span: Default::default(),
99 variables: variables.into(),
100 body: body.into(),
101 }
102 }
103
104 pub fn variables(&self) -> &Variables {
109 &self.variables
110 }
111
112 pub fn set_variables<V>(&mut self, variables: V)
113 where
114 V: Into<Variables>,
115 {
116 self.variables = variables.into();
117 }
118}
119
120impl From<&NamedVariables> for Variables {
125 fn from(value: &NamedVariables) -> Self {
126 Self::Named(value.clone())
127 }
128}
129
130impl From<NamedVariables> for Variables {
131 fn from(value: NamedVariables) -> Self {
132 Self::Named(value)
133 }
134}
135
136impl From<&MappingVariable> for Variables {
137 fn from(value: &MappingVariable) -> Self {
138 Self::Mapping(value.clone())
139 }
140}
141
142impl From<MappingVariable> for Variables {
143 fn from(value: MappingVariable) -> Self {
144 Self::Mapping(value)
145 }
146}
147
148impl Variables {
149 pub const fn is_named_set(&self) -> bool {
154 matches!(self, Self::Named(_))
155 }
156
157 pub const fn as_named_set(&self) -> Option<&NamedVariables> {
158 match self {
159 Self::Named(v) => Some(v),
160 _ => None,
161 }
162 }
163
164 pub const fn is_mapping(&self) -> bool {
165 matches!(self, Self::Mapping(_))
166 }
167
168 pub const fn as_mapping(&self) -> Option<&MappingVariable> {
169 match self {
170 Self::Mapping(v) => Some(v),
171 _ => None,
172 }
173 }
174}
175
176impl HasSourceSpan for NamedVariables {
181 fn with_source_span(self, span: Span) -> Self {
182 let mut self_mut = self;
183 self_mut.span = Some(span);
184 self_mut
185 }
186
187 fn source_span(&self) -> Option<&Span> {
188 self.span.as_ref()
189 }
190
191 fn set_source_span(&mut self, span: Span) {
192 self.span = Some(span);
193 }
194
195 fn unset_source_span(&mut self) {
196 self.span = None;
197 }
198}
199
200impl FromIterator<Identifier> for NamedVariables {
201 fn from_iter<T: IntoIterator<Item = Identifier>>(iter: T) -> Self {
202 Self::new(iter.into_iter().collect())
203 }
204}
205
206impl AsRef<BTreeSet<Identifier>> for NamedVariables {
207 fn as_ref(&self) -> &BTreeSet<Identifier> {
208 &self.names
209 }
210}
211
212impl AsMut<BTreeSet<Identifier>> for NamedVariables {
213 fn as_mut(&mut self) -> &mut BTreeSet<Identifier> {
214 &mut self.names
215 }
216}
217
218impl NamedVariables {
219 pub fn new(names: BTreeSet<Identifier>) -> Self {
224 Self {
225 span: Default::default(),
226 names,
227 }
228 }
229
230 pub fn names(&self) -> impl Iterator<Item = &Identifier> {
235 self.names.iter()
236 }
237}
238
239impl HasSourceSpan for MappingVariable {
244 fn with_source_span(self, span: Span) -> Self {
245 let mut self_mut = self;
246 self_mut.span = Some(span);
247 self_mut
248 }
249
250 fn source_span(&self) -> Option<&Span> {
251 self.span.as_ref()
252 }
253
254 fn set_source_span(&mut self, span: Span) {
255 self.span = Some(span);
256 }
257
258 fn unset_source_span(&mut self) {
259 self.span = None;
260 }
261}
262
263impl MappingVariable {
264 pub const fn new(domain: Identifier, range: Identifier) -> Self {
269 Self {
270 span: None,
271 domain,
272 range,
273 }
274 }
275
276 pub const fn domain(&self) -> &Identifier {
281 &self.domain
282 }
283
284 pub fn set_domain(&mut self, domain: Identifier) {
285 self.domain = domain;
286 }
287
288 pub const fn range(&self) -> &Identifier {
291 &self.range
292 }
293
294 pub fn set_range(&mut self, range: Identifier) {
295 self.range = range;
296 }
297}