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
use std::slice::Iter;

use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use crate::lexer::token::Span;
use crate::node::Node;
use crate::parser::ast::attributes::AttributeGroup;
use crate::parser::ast::comments::CommentGroup;
use crate::parser::ast::identifiers::SimpleIdentifier;
use crate::parser::ast::modifiers::ConstantModifierGroup;
use crate::parser::ast::Expression;

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]

pub struct ConstantEntry {
    pub name: SimpleIdentifier, // `FOO`
    pub equals: Span,           // `=`
    pub value: Expression,      // `123`
}

impl Node for ConstantEntry {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        vec![&mut self.name, &mut self.value]
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]

pub struct ConstantStatement {
    pub comments: CommentGroup,
    pub r#const: Span,               // `const`
    pub entries: Vec<ConstantEntry>, // `FOO = 123`
    pub semicolon: Span,             // `;`
}

impl ConstantStatement {
    pub fn iter(&self) -> Iter<'_, ConstantEntry> {
        self.entries.iter()
    }
}

impl IntoIterator for ConstantStatement {
    type Item = ConstantEntry;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.entries.into_iter()
    }
}

impl Node for ConstantStatement {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        self.entries
            .iter_mut()
            .map(|e| e as &mut dyn Node)
            .collect()
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]

pub struct ClassishConstant {
    pub comments: CommentGroup,
    pub attributes: Vec<AttributeGroup>,  // `#[Foo]`
    pub modifiers: ConstantModifierGroup, // `public`
    pub r#const: Span,                    // `const`
    #[serde(flatten)]
    pub entries: Vec<ConstantEntry>, // `FOO = 123`
    pub semicolon: Span,                  // `;`
}

impl ClassishConstant {
    pub fn iter(&self) -> Iter<'_, ConstantEntry> {
        self.entries.iter()
    }
}

impl IntoIterator for ClassishConstant {
    type Item = ConstantEntry;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.entries.into_iter()
    }
}

impl Node for ClassishConstant {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        self.entries
            .iter_mut()
            .map(|e| e as &mut dyn Node)
            .collect()
    }
}