mago_syntax/ast/ast/
variable.rs1use serde::Serialize;
2use strum::Display;
3
4use mago_span::HasSpan;
5use mago_span::Span;
6
7use crate::ast::ast::expression::Expression;
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord, Display)]
19#[serde(tag = "type", content = "value")]
20pub enum Variable<'arena> {
21 Direct(DirectVariable<'arena>),
22 Indirect(IndirectVariable<'arena>),
23 Nested(NestedVariable<'arena>),
24}
25
26#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
36pub struct DirectVariable<'arena> {
37 pub span: Span,
38 pub name: &'arena str,
39}
40
41#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
53pub struct IndirectVariable<'arena> {
54 pub dollar_left_brace: Span,
55 pub expression: &'arena Expression<'arena>,
56 pub right_brace: Span,
57}
58
59#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
71pub struct NestedVariable<'arena> {
72 pub dollar: Span,
73 pub variable: &'arena Variable<'arena>,
74}
75
76impl HasSpan for Variable<'_> {
77 fn span(&self) -> Span {
78 match self {
79 Variable::Direct(node) => node.span(),
80 Variable::Indirect(node) => node.span(),
81 Variable::Nested(node) => node.span(),
82 }
83 }
84}
85
86impl HasSpan for DirectVariable<'_> {
87 fn span(&self) -> Span {
88 self.span
89 }
90}
91
92impl HasSpan for IndirectVariable<'_> {
93 fn span(&self) -> Span {
94 Span::between(self.dollar_left_brace, self.right_brace)
95 }
96}
97
98impl HasSpan for NestedVariable<'_> {
99 fn span(&self) -> Span {
100 Span::between(self.dollar, self.variable.span())
101 }
102}