sdml_core/model/constraints/formal/
environments.rs1use crate::model::constraints::{
2 BooleanSentence, ConstraintSentence, FunctionDef, PredicateValue, QuantifiedSentence,
3 SequenceOfPredicateValues, SimpleSentence,
4};
5use crate::model::identifiers::Identifier;
6use crate::model::values::SimpleValue;
7use crate::model::{HasBody, HasName, HasSourceSpan, Span};
8
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11
12#[derive(Clone, Debug)]
17#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
18pub struct EnvironmentDef {
19 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
20 span: Option<Span>,
21 name: Identifier,
22 body: EnvironmentDefBody,
23}
24
25#[derive(Clone, Debug)]
26#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
27pub enum EnvironmentDefBody {
28 Function(FunctionDef),
29 Value(PredicateValue),
30 Sentence(ConstraintSentence),
31}
32
33impl HasName for EnvironmentDef {
38 fn name(&self) -> &Identifier {
39 &self.name
40 }
41
42 fn set_name(&mut self, name: Identifier) {
43 self.name = name;
44 }
45}
46
47impl HasBody for EnvironmentDef {
48 type Body = EnvironmentDefBody;
49
50 fn body(&self) -> &Self::Body {
51 &self.body
52 }
53
54 fn body_mut(&mut self) -> &mut Self::Body {
55 &mut self.body
56 }
57
58 fn set_body(&mut self, body: Self::Body) {
59 self.body = body;
60 }
61}
62
63impl HasSourceSpan for EnvironmentDef {
64 fn with_source_span(self, span: Span) -> Self {
65 let mut self_mut = self;
66 self_mut.span = Some(span);
67 self_mut
68 }
69
70 fn source_span(&self) -> Option<&Span> {
71 self.span.as_ref()
72 }
73
74 fn set_source_span(&mut self, span: Span) {
75 self.span = Some(span);
76 }
77
78 fn unset_source_span(&mut self) {
79 self.span = None;
80 }
81}
82
83impl EnvironmentDef {
84 pub const fn new(name: Identifier, body: EnvironmentDefBody) -> Self {
89 Self {
90 span: None,
91 name,
92 body,
93 }
94 }
95
96 pub fn new_value<V>(name: Identifier, body: V) -> Self
97 where
98 V: Into<PredicateValue>,
99 {
100 Self {
101 span: None,
102 name,
103 body: EnvironmentDefBody::Value(body.into()),
104 }
105 }
106
107 pub fn new_function<V>(name: Identifier, body: V) -> Self
108 where
109 V: Into<FunctionDef>,
110 {
111 Self {
112 span: None,
113 name,
114 body: EnvironmentDefBody::Function(body.into()),
115 }
116 }
117
118 pub fn new_sentence<V>(name: Identifier, body: V) -> Self
119 where
120 V: Into<ConstraintSentence>,
121 {
122 Self {
123 span: None,
124 name,
125 body: EnvironmentDefBody::Sentence(body.into()),
126 }
127 }
128}
129
130impl From<&FunctionDef> for EnvironmentDefBody {
135 fn from(v: &FunctionDef) -> Self {
136 Self::Function(v.clone())
137 }
138}
139
140impl From<FunctionDef> for EnvironmentDefBody {
141 fn from(v: FunctionDef) -> Self {
142 Self::Function(v)
143 }
144}
145
146impl From<&PredicateValue> for EnvironmentDefBody {
147 fn from(v: &PredicateValue) -> Self {
148 Self::Value(v.clone())
149 }
150}
151
152impl From<PredicateValue> for EnvironmentDefBody {
153 fn from(v: PredicateValue) -> Self {
154 Self::Value(v)
155 }
156}
157
158impl From<&SimpleValue> for EnvironmentDefBody {
159 fn from(v: &SimpleValue) -> Self {
160 Self::Value(v.clone().into())
161 }
162}
163
164impl From<SimpleValue> for EnvironmentDefBody {
165 fn from(v: SimpleValue) -> Self {
166 Self::Value(v.into())
167 }
168}
169
170impl From<&SequenceOfPredicateValues> for EnvironmentDefBody {
171 fn from(v: &SequenceOfPredicateValues) -> Self {
172 Self::Value(v.clone().into())
173 }
174}
175
176impl From<SequenceOfPredicateValues> for EnvironmentDefBody {
177 fn from(v: SequenceOfPredicateValues) -> Self {
178 Self::Value(v.into())
179 }
180}
181
182impl From<&ConstraintSentence> for EnvironmentDefBody {
183 fn from(v: &ConstraintSentence) -> Self {
184 Self::Sentence(v.clone())
185 }
186}
187
188impl From<ConstraintSentence> for EnvironmentDefBody {
189 fn from(v: ConstraintSentence) -> Self {
190 Self::Sentence(v)
191 }
192}
193
194impl From<&SimpleSentence> for EnvironmentDefBody {
195 fn from(v: &SimpleSentence) -> Self {
196 Self::Sentence(v.clone().into())
197 }
198}
199
200impl From<SimpleSentence> for EnvironmentDefBody {
201 fn from(v: SimpleSentence) -> Self {
202 Self::Sentence(v.into())
203 }
204}
205
206impl From<&BooleanSentence> for EnvironmentDefBody {
207 fn from(v: &BooleanSentence) -> Self {
208 Self::Sentence(v.clone().into())
209 }
210}
211
212impl From<BooleanSentence> for EnvironmentDefBody {
213 fn from(v: BooleanSentence) -> Self {
214 Self::Sentence(v.into())
215 }
216}
217
218impl From<&QuantifiedSentence> for EnvironmentDefBody {
219 fn from(v: &QuantifiedSentence) -> Self {
220 Self::Sentence(v.clone().into())
221 }
222}
223
224impl From<QuantifiedSentence> for EnvironmentDefBody {
225 fn from(v: QuantifiedSentence) -> Self {
226 Self::Sentence(v.into())
227 }
228}
229
230impl EnvironmentDefBody {
231 pub const fn is_value(&self) -> bool {
236 match self {
237 Self::Value(_) => true,
238 _ => false,
239 }
240 }
241
242 pub const fn as_value(&self) -> Option<&PredicateValue> {
243 match self {
244 Self::Value(v) => Some(v),
245 _ => None,
246 }
247 }
248
249 pub const fn is_function(&self) -> bool {
250 match self {
251 Self::Function(_) => true,
252 _ => false,
253 }
254 }
255
256 pub const fn as_function(&self) -> Option<&FunctionDef> {
257 match self {
258 Self::Function(v) => Some(v),
259 _ => None,
260 }
261 }
262
263 pub const fn is_sentence(&self) -> bool {
264 match self {
265 Self::Sentence(_) => true,
266 _ => false,
267 }
268 }
269
270 pub const fn as_sentence(&self) -> Option<&ConstraintSentence> {
271 match self {
272 Self::Sentence(v) => Some(v),
273 _ => None,
274 }
275 }
276}