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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
use crate::error::Error;
use crate::model::check::Validate;
use crate::model::constraints::ConstraintSentence;
use crate::model::identifiers::{Identifier, IdentifierReference};
use crate::model::members::{CardinalityRange, MappingType, Ordering, Uniqueness};
use crate::model::modules::Module;
use crate::model::Span;
use crate::syntax::KW_WILDCARD;
use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
// ------------------------------------------------------------------------------------------------
// Public Macros
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Public Types ❱ Formal Constraints ❱ Environments ❱ Functions
// ------------------------------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionDef {
span: Option<Span>,
signature: FunctionSignature,
body: ConstraintSentence,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionSignature {
span: Option<Span>,
parameters: Vec<FunctionParameter>,
target_type: FunctionType,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionParameter {
span: Option<Span>,
name: Identifier,
target_type: FunctionType,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionType {
span: Option<Span>,
target_cardinality: FunctionCardinality,
target_type: FunctionTypeReference,
}
/// Corresponds to the grammar rule `cardinality`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionCardinality {
span: Option<Span>,
ordering: Option<Ordering>,
uniqueness: Option<Uniqueness>,
range: Option<CardinalityRange>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct FunctionTypeReference {
optional: bool,
inner: FunctionTypeReferenceInner,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum FunctionTypeReferenceInner {
Wildcard,
Reference(IdentifierReference),
// builtin_simple_type is converted into a reference
MappingType(MappingType),
}
// ------------------------------------------------------------------------------------------------
// Implementations ❱ Formal Constraints ❱ Environments ❱ Functions
// ------------------------------------------------------------------------------------------------
impl_has_body_for!(FunctionDef, ConstraintSentence);
impl_has_source_span_for!(FunctionDef);
impl FunctionDef {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub const fn new(signature: FunctionSignature, body: ConstraintSentence) -> Self {
Self {
span: None,
signature,
body,
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
get_and_set!(pub signature, set_signature => FunctionSignature);
}
// ------------------------------------------------------------------------------------------------
impl_has_source_span_for!(FunctionSignature);
impl FunctionSignature {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub fn new(parameters: Vec<FunctionParameter>, target_type: FunctionType) -> Self {
Self {
span: Default::default(),
parameters,
target_type,
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
get_and_set_vec!(
pub
has has_parameters,
parameters_len,
parameters,
parameters_mut,
add_to_parameters,
extend_parameters
=> parameters, FunctionParameter
);
get_and_set!(pub target_type, set_target_type => FunctionType);
}
// ------------------------------------------------------------------------------------------------
impl_has_name_for!(FunctionParameter);
impl_has_source_span_for!(FunctionParameter);
impl FunctionParameter {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub const fn new(name: Identifier, target_type: FunctionType) -> Self {
Self {
span: None,
name,
target_type,
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
get_and_set!(pub target_type, set_target_type => FunctionType);
}
// ------------------------------------------------------------------------------------------------
impl_has_source_span_for!(FunctionType);
impl FunctionType {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub fn new(
target_cardinality: FunctionCardinality,
target_type: FunctionTypeReference,
) -> Self {
Self {
span: Default::default(),
target_cardinality,
target_type,
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
pub fn with_wildcard_cardinality(self) -> Self {
Self {
target_cardinality: FunctionCardinality::new_wildcard(),
..self
}
}
pub fn with_target_cardinality(self, target_cardinality: FunctionCardinality) -> Self {
Self {
target_cardinality,
..self
}
}
get_and_set!(pub target_cardinality, set_target_cardinality => FunctionCardinality);
pub fn with_target_type(self, target_type: FunctionTypeReference) -> Self {
Self {
target_type,
..self
}
}
get_and_set!(pub target_type, set_target_type => FunctionTypeReference);
}
// ------------------------------------------------------------------------------------------------
impl Display for FunctionCardinality {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{{}{}{}}}",
self.ordering.map(|c| format!("{} ", c)).unwrap_or_default(),
self.uniqueness
.map(|c| format!("{} ", c))
.unwrap_or_default(),
if let Some(range) = &self.range {
range.to_string()
} else {
KW_WILDCARD.to_string()
}
)
}
}
impl_has_source_span_for!(FunctionCardinality);
impl Validate for FunctionCardinality {
fn is_complete(&self, top: &Module) -> Result<bool, Error> {
if let Some(range) = &self.range {
range.is_complete(top)
} else {
Ok(true)
}
}
fn is_valid(&self, check_constraints: bool, top: &Module) -> Result<bool, Error> {
if let Some(range) = &self.range {
range.is_valid(check_constraints, top)
} else {
Ok(true)
}
}
}
impl FunctionCardinality {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub const fn new(
ordering: Option<Ordering>,
uniqueness: Option<Uniqueness>,
range: Option<CardinalityRange>,
) -> Self {
Self {
span: None,
ordering,
uniqueness,
range,
}
}
pub const fn new_range(min: u32, max: u32) -> Self {
Self {
span: None,
ordering: None,
uniqueness: None,
range: Some(CardinalityRange::new_range(min, max)),
}
}
pub const fn new_unbounded(min: u32) -> Self {
Self {
span: None,
ordering: None,
uniqueness: None,
range: Some(CardinalityRange::new_unbounded(min)),
}
}
pub const fn new_single(min_and_max: u32) -> Self {
Self {
span: None,
ordering: None,
uniqueness: None,
range: Some(CardinalityRange::new_single(min_and_max)),
}
}
pub const fn new_wildcard() -> Self {
Self {
span: None,
ordering: None,
uniqueness: None,
range: None,
}
}
#[inline(always)]
pub const fn one() -> Self {
Self::new_single(1)
}
#[inline(always)]
pub const fn zero_or_one() -> Self {
Self::new_range(0, 1)
}
#[inline(always)]
pub const fn one_or_more() -> Self {
Self::new_unbounded(1)
}
#[inline(always)]
pub const fn zero_or_more() -> Self {
Self::new_unbounded(0)
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
pub const fn with_ordering(self, ordering: Ordering) -> Self {
Self {
ordering: Some(ordering),
..self
}
}
get_and_set!(pub ordering, set_ordering, unset_ordering => optional copy has_ordering, Ordering);
#[inline(always)]
pub fn is_ordered(&self) -> Option<bool> {
self.ordering().map(|o| o == Ordering::Ordered)
}
// --------------------------------------------------------------------------------------------
#[inline(always)]
pub const fn with_uniqueness(self, uniqueness: Uniqueness) -> Self {
Self {
uniqueness: Some(uniqueness),
..self
}
}
get_and_set!(pub uniqueness, set_uniqueness, unset_uniqueness => optional copy has_uniqueness, Uniqueness);
#[inline(always)]
pub fn is_unique(&self) -> Option<bool> {
self.uniqueness().map(|u| u == Uniqueness::Unique)
}
// --------------------------------------------------------------------------------------------
get_and_set!(pub range, set_range, unset_range => optional has_range, CardinalityRange);
pub fn is_wildcard(&self) -> bool {
self.range.is_none()
}
}
// ------------------------------------------------------------------------------------------------
impl From<FunctionTypeReferenceInner> for FunctionTypeReference {
fn from(inner: FunctionTypeReferenceInner) -> Self {
Self {
optional: false,
inner,
}
}
}
impl AsRef<FunctionTypeReferenceInner> for FunctionTypeReference {
fn as_ref(&self) -> &FunctionTypeReferenceInner {
&self.inner
}
}
impl FunctionTypeReference {
// --------------------------------------------------------------------------------------------
// Constructors
// --------------------------------------------------------------------------------------------
pub fn optional(inner: FunctionTypeReferenceInner) -> Self {
Self {
optional: true,
inner,
}
}
// --------------------------------------------------------------------------------------------
// Fields
// --------------------------------------------------------------------------------------------
pub fn is_optional(&self) -> bool {
self.optional
}
pub fn inner(&self) -> &FunctionTypeReferenceInner {
&self.inner
}
}
// ------------------------------------------------------------------------------------------------
impl From<IdentifierReference> for FunctionTypeReferenceInner {
fn from(value: IdentifierReference) -> Self {
Self::Reference(value)
}
}
impl From<MappingType> for FunctionTypeReferenceInner {
fn from(value: MappingType) -> Self {
Self::MappingType(value)
}
}
impl FunctionTypeReferenceInner {
// --------------------------------------------------------------------------------------------
// Variants
// --------------------------------------------------------------------------------------------
is_as_variant!(Reference (IdentifierReference) => is_type_reference, as_type_reference);
is_as_variant!(MappingType (MappingType) => is_mapping_type, as_mapping_type);
is_variant!(Wildcard => is_wildcard);
}
// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------