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
use super::*;

mod display;

mod iters;

/// `class Name(Super): Trait {}`
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ConstraintDeclaration {
    /// The document of the class
    pub annotations: AnnotationNode,
    /// The parameter arguments of the class.
    pub generics: Vec<IdentifierNode>,
    /// All fields declared in this block, exclude extensions.
    pub terms: Vec<ConstraintTerm>,
}

impl Debug for ConstraintDeclaration {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let w = &mut f.debug_struct("Constraints");
        if !self.annotations.is_empty() {
            w.field("annotations", &self.annotations);
        }
        if !self.generics.is_empty() {
            w.field("generics", &self.generics);
        }
        if !self.terms.is_empty() {
            w.field("terms", &self.terms);
        }
        w.finish()
    }
}

/// Valid terms in the class statements
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ConstraintTerm {
    /// `@expand {}`
    Macro(ProceduralNode),
    /// `field: Type = default`
    Field(FieldDeclaration),
    /// `method()`
    Method(MethodDeclaration),
    /// `domain { }`
    Domain(DomainDeclaration),
}
impl Default for ConstraintDeclaration {
    fn default() -> Self {
        Self { annotations: Default::default(), generics: vec![], terms: vec![] }
    }
}

impl ConstraintDeclaration {
    /// Create a new [`ConstraintDeclaration`] with the given capacity.
    pub fn is_empty(&self) -> bool {
        self.generics.is_empty() && self.terms.is_empty() && self.annotations.is_empty()
    }
}