protospec_build/asg/
field.rs

1use super::*;
2
3#[derive(Debug, Clone)]
4pub struct TypeArgument {
5    pub name: String,
6    pub type_: Type,
7    pub default_value: Option<Expression>,
8    pub can_resolve_auto: bool,
9}
10
11#[derive(Debug)]
12pub struct TypeTransform {
13    pub transform: Arc<Transform>,
14    pub condition: Option<Expression>,
15    pub arguments: Vec<Expression>,
16}
17
18#[derive(Debug)]
19pub struct Field {
20    pub name: String,
21    pub arguments: RefCell<Vec<TypeArgument>>,
22    pub span: Span,
23    pub type_: RefCell<Type>,
24    pub condition: RefCell<Option<Expression>>,
25    pub transforms: RefCell<Vec<TypeTransform>>,
26    pub toplevel: bool,
27    pub is_auto: Cell<bool>,
28    pub is_maybe_cyclical: Cell<bool>,
29    pub is_pad: Cell<bool>,
30}
31
32impl Field {
33    pub(super) fn get_indirect_contained_fields(&self, target: &mut IndexSet<String>) {
34        let type_ = self.type_.borrow();
35        match &*type_ {
36            Type::Array(interior) => {
37                if target.insert(interior.element.name.clone()) {
38                    interior.element.get_indirect_contained_fields(target);
39                }
40            }
41            Type::Container(interior) => {
42                for (_, field) in &interior.items {
43                    if target.insert(field.name.clone()) {
44                        field.get_indirect_contained_fields(target);
45                    }
46                }
47            }
48            Type::Ref(call) => {
49                if target.insert(call.target.name.clone()) {
50                    call.target.get_indirect_contained_fields(target);
51                }
52            }
53            _ => (),
54        }
55    }
56}
57
58impl fmt::Display for Field {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "{}: {}", self.name, self.type_.borrow())?;
61        if let Some(condition) = &*self.condition.borrow() {
62            write!(f, " {{ {} }}", condition)?;
63        }
64        for transform in self.transforms.borrow().iter() {
65            write!(f, " -> {}", transform.transform.name)?;
66            if let Some(condition) = transform.condition.as_ref() {
67                write!(f, " {{ {} }}", condition)?;
68            }
69        }
70        Ok(())
71    }
72}
73
74impl AsgExpression for Field {
75    fn get_type(&self) -> Option<Type> {
76        Some(self.type_.borrow().clone())
77    }
78}
79
80impl PartialEq for Field {
81    fn eq(&self, other: &Field) -> bool {
82        self.name == other.name && self.type_.borrow().assignable_from(&other.type_.borrow())
83    }
84}