sdml_core/model/constraints/
mod.rs

1/*!
2Provide the Rust types that implement *constraint*-related components of the SDML Grammar.
3*/
4use crate::{
5    load::ModuleLoader,
6    model::{Identifier, Span},
7    store::ModuleStore,
8};
9
10#[cfg(feature = "serde")]
11use serde::{Deserialize, Serialize};
12
13// ------------------------------------------------------------------------------------------------
14// Public Types ❱ Constraints
15// ------------------------------------------------------------------------------------------------
16
17/// Corresponds to the grammar rule `constraint`.
18#[derive(Clone, Debug)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20pub struct Constraint {
21    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
22    span: Option<Box<Span>>,
23    name: Identifier,
24    body: ConstraintBody,
25}
26
27///
28/// Corresponds to the field `body` in the grammar rule `constraint`.
29///
30/// # Semantics
31///
32/// The domain of discourse, $\mathbb{D}$, is the set of all definitions present in the current
33/// module and the set of modules transitively imported by it.
34///
35#[derive(Clone, Debug)]
36#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
37pub enum ConstraintBody {
38    /// Corresponds to the grammar rule `informal_constraint`.
39    Informal(ControlledLanguageString),
40    /// Corresponds to the grammar rule `formal_constraint`.
41    Formal(FormalConstraint),
42}
43
44// ------------------------------------------------------------------------------------------------
45// Implementations ❱ Constraints
46// ------------------------------------------------------------------------------------------------
47
48impl_has_body_for!(Constraint, ConstraintBody);
49
50impl_has_name_for!(Constraint);
51
52impl_has_source_span_for!(Constraint);
53
54impl_references_for!(Constraint => delegate body);
55
56impl Validate for Constraint {
57    fn validate(
58        &self,
59        top: &Module,
60        cache: &impl ModuleStore,
61        loader: &impl ModuleLoader,
62        check_constraints: bool,
63    ) {
64        self.body.validate(top, cache, loader, check_constraints)
65    }
66}
67
68impl Constraint {
69    // --------------------------------------------------------------------------------------------
70    // Constructors
71    // --------------------------------------------------------------------------------------------
72
73    pub fn new<B>(name: Identifier, body: B) -> Self
74    where
75        B: Into<ConstraintBody>,
76    {
77        Self {
78            span: None,
79            name,
80            body: body.into(),
81        }
82    }
83}
84
85// ------------------------------------------------------------------------------------------------
86
87impl From<ControlledLanguageString> for ConstraintBody {
88    fn from(v: ControlledLanguageString) -> Self {
89        Self::Informal(v)
90    }
91}
92
93impl From<FormalConstraint> for ConstraintBody {
94    fn from(v: FormalConstraint) -> Self {
95        Self::Formal(v)
96    }
97}
98
99impl_references_for!(ConstraintBody => variants Informal, Formal);
100
101impl_validate_for!(ConstraintBody => variants Informal, Formal);
102
103impl ConstraintBody {
104    // --------------------------------------------------------------------------------------------
105    // Variants
106    // --------------------------------------------------------------------------------------------
107
108    is_as_variant!(Informal (ControlledLanguageString) => is_informal, as_informal);
109
110    is_as_variant!(Formal (FormalConstraint) => is_formal, as_formal);
111}
112
113// ------------------------------------------------------------------------------------------------
114// Modules
115// ------------------------------------------------------------------------------------------------
116
117mod formal;
118pub use formal::{
119    AtomicSentence, BinaryBooleanSentence, BooleanSentence, ConnectiveOperator, ConstraintSentence,
120    EnvironmentDef, EnvironmentDefBody, Equation, FormalConstraint, FunctionCardinality,
121    FunctionComposition, FunctionDef, FunctionParameter, FunctionSignature, FunctionType,
122    FunctionTypeReference, FunctionTypeReferenceInner, FunctionalTerm, InequalityRelation,
123    Inequation, MappingVariable, NamedVariables, PredicateSequenceMember, PredicateValue,
124    QuantifiedSentence, QuantifiedVariable, QuantifiedVariableBinding, Quantifier, SequenceBuilder,
125    SequenceOfPredicateValues, SimpleSentence, Subject, Term, UnaryBooleanSentence, Variables,
126};
127
128mod informal;
129pub use informal::{ControlledLanguageString, ControlledLanguageTag};
130
131use super::{check::Validate, modules::Module};