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
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;

use std::fmt::Display;

use crate::lexer::byte_string::ByteString;
use crate::lexer::token::Span;
use crate::node::Node;
use crate::parser::ast::Expression;

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "type", content = "value")]
pub enum Variable {
    SimpleVariable(SimpleVariable),
    VariableVariable(VariableVariable),
    BracedVariableVariable(BracedVariableVariable),
}

impl Node for Variable {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        match self {
            Variable::SimpleVariable(variable) => variable.children(),
            Variable::VariableVariable(variable) => variable.children(),
            Variable::BracedVariableVariable(variable) => variable.children(),
        }
    }
}

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

pub struct SimpleVariable {
    pub span: Span,
    pub name: ByteString,
}

impl Node for SimpleVariable {
    //
}

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

pub struct VariableVariable {
    pub span: Span,
    pub variable: Box<Variable>,
}

impl Node for VariableVariable {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        vec![self.variable.as_mut()]
    }
}

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

pub struct BracedVariableVariable {
    pub start: Span,
    pub variable: Box<Expression>,
    pub end: Span,
}

impl Node for BracedVariableVariable {
    fn children(&mut self) -> Vec<&mut dyn Node> {
        vec![self.variable.as_mut()]
    }
}

impl Display for SimpleVariable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}