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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::ast::{self, Span};
use std::fmt;

/// Represents arbitrary, even nested, expressions.
#[derive(Debug, Clone)]
pub enum Expression {
    /// Any numeric value e.g. floats or ints.
    NumericValue(String, Span),
    /// Any string value.
    StringValue(String, Span),
    /// Any literal constant, basically a string which was not inside "...". This is used for
    /// representing builtin enums and boolean constants (true and false).
    ConstantValue(String, Span),
    /// A function call like node with a name and arguments.
    Function(String, ast::ArgumentsList, Span),
    /// An array of other values.
    Array(Vec<Expression>, Span),
}

impl fmt::Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expression::NumericValue(val, _) => fmt::Display::fmt(val, f),
            Expression::StringValue(val, _) => write!(f, "{}", crate::string_literal(val)),
            Expression::ConstantValue(val, _) => fmt::Display::fmt(val, f),
            Expression::Function(fun, args, _) => {
                let args = args.iter().map(ToString::to_string).collect::<Vec<_>>().join(",");
                write!(f, "{fun}({args})")
            }
            Expression::Array(vals, _) => {
                let vals = vals.iter().map(ToString::to_string).collect::<Vec<_>>().join(",");
                write!(f, "[{vals}]")
            }
        }
    }
}

impl Expression {
    pub fn as_array(&self) -> Option<(&[Expression], Span)> {
        match self {
            Expression::Array(arr, span) => Some((arr, *span)),
            _ => None,
        }
    }

    pub fn as_string_value(&self) -> Option<(&str, Span)> {
        match self {
            Expression::StringValue(s, span) => Some((s, *span)),
            _ => None,
        }
    }

    pub fn as_constant_value(&self) -> Option<(&str, Span)> {
        match self {
            Expression::ConstantValue(s, span) => Some((s, *span)),
            _ => None,
        }
    }

    pub fn as_function(&self) -> Option<(&str, &ast::ArgumentsList, Span)> {
        match self {
            Expression::Function(name, args, span) => Some((name, args, *span)),
            _ => None,
        }
    }

    pub fn as_numeric_value(&self) -> Option<(&str, Span)> {
        match self {
            Expression::NumericValue(s, span) => Some((s, *span)),
            _ => None,
        }
    }

    pub fn span(&self) -> Span {
        match &self {
            Self::NumericValue(_, span) => *span,
            Self::StringValue(_, span) => *span,
            Self::ConstantValue(_, span) => *span,
            Self::Function(_, _, span) => *span,
            Self::Array(_, span) => *span,
        }
    }

    pub fn is_env_expression(&self) -> bool {
        match &self {
            Self::Function(name, _, _) => name == "env",
            _ => false,
        }
    }

    /// Creates a friendly readable representation for a value's type.
    pub fn describe_value_type(&self) -> &'static str {
        match self {
            Expression::NumericValue(_, _) => "numeric",
            Expression::StringValue(_, _) => "string",
            Expression::ConstantValue(_, _) => "literal",
            Expression::Function(_, _, _) => "functional",
            Expression::Array(_, _) => "array",
        }
    }

    pub fn is_function(&self) -> bool {
        matches!(self, Expression::Function(_, _, _))
    }

    pub fn is_array(&self) -> bool {
        matches!(self, Expression::Array(_, _))
    }

    pub fn is_string(&self) -> bool {
        matches!(self, Expression::StringValue(_, _))
    }
}