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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use super::*;
use std::fmt;

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

pub(super) trait EvalResultExt {
    fn add_errors(self, rhs: Self) -> Self;
}

impl EvalResultExt for EvalResult<(), Errors> {
    fn add_errors(self, rhs: Self) -> Self {
        match self {
            Err(mut lhs) => {
                lhs.extend_from_result(rhs);
                Err(lhs)
            }
            _ => rhs,
        }
    }
}

/// A type holding multiple errors that occurred during in-place expression evaluation via
/// [`Evaluate::evaluate_in_place`].
///
/// It is guaranteed that `Errors` instances hold at least one error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Errors {
    inner: Vec<Error>,
}

impl Errors {
    fn extend_from_result(&mut self, res: EvalResult<(), Errors>) {
        if let Err(errors) = res {
            self.inner.extend(errors);
        }
    }

    /// Returns the number of errors.
    #[inline]
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns an iterator over all errors.
    #[inline]
    pub fn iter(&self) -> std::slice::Iter<Error> {
        self.inner.iter()
    }
}

impl fmt::Display for Errors {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.len() == 1 {
            self.inner[0].fmt(f)
        } else {
            writeln!(f, "{} errors occurred:", self.len())?;

            for error in self {
                writeln!(f, "- {error}")?;
            }

            Ok(())
        }
    }
}

impl From<Error> for Errors {
    #[inline]
    fn from(error: Error) -> Self {
        Errors { inner: vec![error] }
    }
}

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

impl IntoIterator for Errors {
    type Item = Error;
    type IntoIter = std::vec::IntoIter<Error>;

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

impl<'a> IntoIterator for &'a Errors {
    type Item = &'a Error;
    type IntoIter = std::slice::Iter<'a, Error>;

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

/// The error type returned by all fallible operations within this module.
#[derive(Debug, Clone, PartialEq, Eq)]
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, PartialEq, Eq)]
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(FuncName),
    /// 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(FuncName, String),
}

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(func_name) => {
                write!(f, "undefined function `{func_name}`")
            }
            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 `{operator}` is not applicable to `{value}`",
            ),
            ErrorKind::BinaryOp(lhs, operator, rhs) => write!(
                f,
                "binary operator `{operator}` is not applicable to `{lhs}` and `{rhs}`",
            ),
            ErrorKind::FuncCall(name, msg) => {
                write!(f, "error calling function `{name}`: {msg}")
            }
        }
    }
}