php_parser_rs/parser/ast/
properties.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use serde::Serialize;
4
5use crate::lexer::token::Span;
6use crate::node::Node;
7use crate::parser::ast::attributes::AttributeGroup;
8use crate::parser::ast::data_type::Type;
9use crate::parser::ast::modifiers::PropertyModifierGroup;
10use crate::parser::ast::variables::SimpleVariable;
11use crate::parser::ast::Expression;
12
13#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
14
15pub struct Property {
16    pub attributes: Vec<AttributeGroup>,
17    #[serde(flatten)]
18    pub modifiers: PropertyModifierGroup,
19    pub r#type: Option<Type>,
20    pub entries: Vec<PropertyEntry>,
21    pub end: Span,
22}
23
24impl Node for Property {
25    fn children(&mut self) -> Vec<&mut dyn Node> {
26        let mut children: Vec<&mut dyn Node> = vec![];
27        if let Some(r#type) = &mut self.r#type {
28            children.push(r#type);
29        }
30        children.extend(
31            self.entries
32                .iter_mut()
33                .map(|e| e as &mut dyn Node)
34                .collect::<Vec<&mut dyn Node>>(),
35        );
36        children
37    }
38}
39
40#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
41
42pub struct VariableProperty {
43    pub attributes: Vec<AttributeGroup>,
44    pub r#type: Option<Type>,
45    pub entries: Vec<PropertyEntry>,
46    pub end: Span,
47}
48
49impl Node for VariableProperty {
50    fn children(&mut self) -> Vec<&mut dyn Node> {
51        let mut children: Vec<&mut dyn Node> = vec![];
52        if let Some(r#type) = &mut self.r#type {
53            children.push(r#type);
54        }
55        children.extend(
56            self.entries
57                .iter_mut()
58                .map(|e| e as &mut dyn Node)
59                .collect::<Vec<&mut dyn Node>>(),
60        );
61        children
62    }
63}
64
65#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
66#[serde(tag = "type", content = "value")]
67pub enum PropertyEntry {
68    Uninitialized {
69        variable: SimpleVariable,
70    },
71    Initialized {
72        variable: SimpleVariable,
73        equals: Span,
74        value: Expression,
75    },
76}
77
78impl Node for PropertyEntry {
79    fn children(&mut self) -> Vec<&mut dyn Node> {
80        match self {
81            PropertyEntry::Uninitialized { variable } => vec![variable],
82            PropertyEntry::Initialized {
83                variable, value, ..
84            } => vec![variable, value],
85        }
86    }
87}
88
89impl PropertyEntry {
90    pub fn variable(&self) -> &SimpleVariable {
91        match self {
92            PropertyEntry::Uninitialized { variable } => variable,
93            PropertyEntry::Initialized { variable, .. } => variable,
94        }
95    }
96}