1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
use crate::model::{constraints::QuantifiedSentence, identifiers::Identifier, Span};
use std::collections::HashSet;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
// ------------------------------------------------------------------------------------------------
// Public Macros
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Public Types ❱ Formal Constraints ❱ Sequence Comprehensions
// ------------------------------------------------------------------------------------------------
/// Corresponds to the grammar rule `sequence_comprehension`.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct SequenceBuilder {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
span: Option<Span>,
variables: Variables,
body: QuantifiedSentence,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Variables {
Named(NamedVariables),
Mapping(MappingVariable),
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct NamedVariables {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
span: Option<Span>,
names: HashSet<Identifier>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct MappingVariable {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
span: Option<Span>,
domain: Identifier,
range: Identifier,
}
// ------------------------------------------------------------------------------------------------
// Implementations ❱ Formal Constraints ❱ Sequence Comprehensions
// ------------------------------------------------------------------------------------------------
impl_has_body_for!(SequenceBuilder, QuantifiedSentence);
impl_has_source_span_for!(SequenceBuilder);
impl SequenceBuilder {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub fn new<V, S>(variables: V, body: S) -> Self
where
V: Into<Variables>,
S: Into<QuantifiedSentence>,
{
Self {
span: Default::default(),
variables: variables.into(),
body: body.into(),
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
pub fn variables(&self) -> &Variables {
&self.variables
}
pub fn set_variables<V>(&mut self, variables: V)
where
V: Into<Variables>,
{
self.variables = variables.into();
}
}
// ------------------------------------------------------------------------------------------------
impl From<NamedVariables> for Variables {
fn from(value: NamedVariables) -> Self {
Self::Named(value)
}
}
impl From<MappingVariable> for Variables {
fn from(value: MappingVariable) -> Self {
Self::Mapping(value)
}
}
impl Variables {
// --------------------------------------------------------------------------------------------
// Variants
// --------------------------------------------------------------------------------------------
is_as_variant!(Named (NamedVariables) => is_named_set, as_named_set);
is_as_variant!(Mapping (MappingVariable) => is_mapping, as_mapping);
}
// ------------------------------------------------------------------------------------------------
impl_has_source_span_for!(NamedVariables);
impl FromIterator<Identifier> for NamedVariables {
fn from_iter<T: IntoIterator<Item = Identifier>>(iter: T) -> Self {
Self::new(iter.into_iter().collect())
}
}
impl AsRef<HashSet<Identifier>> for NamedVariables {
fn as_ref(&self) -> &HashSet<Identifier> {
&self.names
}
}
impl AsMut<HashSet<Identifier>> for NamedVariables {
fn as_mut(&mut self) -> &mut HashSet<Identifier> {
&mut self.names
}
}
impl NamedVariables {
pub fn new(names: HashSet<Identifier>) -> Self {
Self {
span: Default::default(),
names,
}
}
}
// ------------------------------------------------------------------------------------------------
impl_has_source_span_for!(MappingVariable);
impl MappingVariable {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub const fn new(domain: Identifier, range: Identifier) -> Self {
Self {
span: None,
domain,
range,
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
get_and_set!(pub domain, set_domain => Identifier);
get_and_set!(pub range, set_range => Identifier);
}
// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------