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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use super::*;
use std::fmt;

/// The result type used by this module.
pub type EvalResult<T, E = Error> = std::result::Result<T, E>;

/// The error type returned by all fallible operations within this module.
#[derive(Debug, Clone)]
pub struct Error {
    inner: Box<ErrorInner>,
}

impl Error {
    pub(super) fn new<T>(kind: T) -> Error
    where
        T: Into<ErrorKind>,
    {
        Error::new_with_expr(kind, None)
    }

    pub(super) fn new_with_expr<T>(kind: T, expr: Option<Expression>) -> Error
    where
        T: Into<ErrorKind>,
    {
        Error {
            inner: Box::new(ErrorInner::new(kind.into(), expr)),
        }
    }

    pub(super) fn unexpected<T>(value: T, expected: &'static str) -> Error
    where
        T: Into<Value>,
    {
        Error::new(ErrorKind::Unexpected(value.into(), expected))
    }

    /// Return a reference to the `ErrorKind` for further error matching.
    pub fn kind(&self) -> &ErrorKind {
        &self.inner.kind
    }

    /// Return a reference to the `Expression` that caused the error, if there is one.
    pub fn expr(&self) -> Option<&Expression> {
        self.inner.expr.as_ref()
    }

    /// Consume the `Error` and return the `ErrorKind`.
    pub fn into_kind(self) -> ErrorKind {
        self.inner.kind
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Error::new(kind)
    }
}

impl From<crate::Error> for Error {
    fn from(err: crate::Error) -> Self {
        Error::new(ErrorKind::Message(err.to_string()))
    }
}

impl std::error::Error for Error {}

// The inner type that holds the actual error data.
//
// This is a separate type because it gets boxed to keep the size of the `Error` struct small.
#[derive(Debug, Clone)]
struct ErrorInner {
    kind: ErrorKind,
    expr: Option<Expression>,
}

impl ErrorInner {
    fn new(kind: ErrorKind, expr: Option<Expression>) -> ErrorInner {
        ErrorInner { kind, expr }
    }
}

impl fmt::Display for ErrorInner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.kind)?;

        if let Some(expr) = &self.expr {
            write!(f, " in expression `{expr}`")?;
        }

        Ok(())
    }
}

/// An enum representing all kinds of errors that can happen during the evaluation of HCL
/// expressions and templates.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ErrorKind {
    /// A generic error message.
    Message(String),
    /// An expression contained an undefined variable.
    UndefinedVar(Identifier),
    /// An expression contained a call to an undefined function.
    UndefinedFunc(Identifier),
    /// A different type of value was expected.
    Unexpected(Value, &'static str),
    /// An expression tried to access a non-existing array index.
    Index(usize),
    /// An unary operator was applied to a value that does not support it.
    UnaryOp(UnaryOperator, Value),
    /// A binary operator was applied to values that do not support it.
    BinaryOp(Value, BinaryOperator, Value),
    /// An expression tried to access an object key which does not exist.
    NoSuchKey(String),
    /// A `for` expression attempted to set the same object key twice.
    KeyExists(String),
    /// A function call in an expression returned an error.
    FuncCall(Identifier, String),
    /// It was attempted to evaluate a raw expression.
    RawExpression,
}

impl From<Error> for ErrorKind {
    fn from(err: Error) -> Self {
        err.into_kind()
    }
}

impl From<&str> for ErrorKind {
    fn from(msg: &str) -> Self {
        ErrorKind::Message(msg.to_owned())
    }
}

impl From<String> for ErrorKind {
    fn from(msg: String) -> Self {
        ErrorKind::Message(msg)
    }
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorKind::Message(msg) => f.write_str(msg),
            ErrorKind::UndefinedVar(ident) => {
                write!(f, "undefined variable `{ident}`")
            }
            ErrorKind::UndefinedFunc(ident) => {
                write!(f, "undefined function `{ident}`")
            }
            ErrorKind::Unexpected(value, expected) => {
                write!(f, "unexpected value `{value}`, expected {expected}")
            }
            ErrorKind::Index(index) => write!(f, "index out of bounds: {index}"),
            ErrorKind::NoSuchKey(key) => write!(f, "no such key: `{key}`"),
            ErrorKind::KeyExists(key) => write!(f, "key `{key}` already exists"),
            ErrorKind::UnaryOp(operator, value) => write!(
                f,
                "unary operator `{}` is not applicable to `{}`",
                operator, value,
            ),
            ErrorKind::BinaryOp(lhs, operator, rhs) => write!(
                f,
                "binary operator `{}` is not applicable to `{}` and `{}`",
                operator, lhs, rhs
            ),
            ErrorKind::FuncCall(name, msg) => {
                write!(f, "error calling function `{name}`: {msg}")
            }
            ErrorKind::RawExpression => f.write_str("raw expressions cannot be evaluated"),
        }
    }
}