sdml_core/model/constraints/formal/
sequences.rs

1use crate::model::{
2    constraints::{QuantifiedSentence, Variable},
3    HasBody, HasSourceSpan, Span,
4};
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9// ------------------------------------------------------------------------------------------------
10// Public Types ❱ Constraints ❱  Sequence Comprehensions
11// ------------------------------------------------------------------------------------------------
12
13/// Corresponds to the grammar rule `sequence_comprehension`.
14#[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: Vec<Variable>,
20    body: QuantifiedSentence,
21}
22
23// ------------------------------------------------------------------------------------------------
24// Implementations ❱ Constraints ❱  SequenceBuilder
25// ------------------------------------------------------------------------------------------------
26
27impl HasBody for SequenceBuilder {
28    type Body = QuantifiedSentence;
29
30    fn body(&self) -> &Self::Body {
31        &self.body
32    }
33
34    fn body_mut(&mut self) -> &mut Self::Body {
35        &mut self.body
36    }
37
38    fn set_body(&mut self, body: Self::Body) {
39        self.body = body;
40    }
41}
42
43impl HasSourceSpan for SequenceBuilder {
44    fn with_source_span(self, span: Span) -> Self {
45        let mut self_mut = self;
46        self_mut.span = Some(span);
47        self_mut
48    }
49
50    fn source_span(&self) -> Option<&Span> {
51        self.span.as_ref()
52    }
53
54    fn set_source_span(&mut self, span: Span) {
55        self.span = Some(span);
56    }
57
58    fn unset_source_span(&mut self) {
59        self.span = None;
60    }
61}
62
63impl SequenceBuilder {
64    // --------------------------------------------------------------------------------------------
65    // Constructors
66    // --------------------------------------------------------------------------------------------
67
68    pub fn new<V, S>(variables: V, body: S) -> Self
69    where
70        V: IntoIterator<Item = Variable>,
71        S: Into<QuantifiedSentence>,
72    {
73        Self {
74            span: Default::default(),
75            variables: Vec::from_iter(variables),
76            body: body.into(),
77        }
78    }
79
80    // --------------------------------------------------------------------------------------------
81    // Fields
82    // --------------------------------------------------------------------------------------------
83
84    pub fn has_variables(&self) -> bool {
85        !self.variables.is_empty()
86    }
87
88    pub fn variables_len(&self) -> usize {
89        self.variables.len()
90    }
91
92    pub fn variables(&self) -> impl Iterator<Item = &Variable> {
93        self.variables.iter()
94    }
95
96    pub fn variables_mut(&mut self) -> impl Iterator<Item = &mut Variable> {
97        self.variables.iter_mut()
98    }
99
100    pub fn add_to_variables<I>(&mut self, value: I)
101    where
102        I: Into<Variable>,
103    {
104        self.variables.push(value.into())
105    }
106
107    pub fn extend_variables<I>(&mut self, extension: I)
108    where
109        I: IntoIterator<Item = Variable>,
110    {
111        self.variables.extend(extension)
112    }
113}