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
//! # `EvalError`
use std::error;
use std::fmt;

#[derive(Debug)]
pub struct EvalError {
    pub bad_input: String,
    pub message: String,
}

impl fmt::Display for EvalError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}: {}", self.message, self.bad_input)
    }
}

impl EvalError {
    pub(crate) fn new<B, M>(bad_input: B, message: M) -> Self
    where
        B: Into<String>,
        M: Into<String>,
    {
        Self {
            bad_input: bad_input.into(),
            message: message.into(),
        }
    }
}

impl error::Error for EvalError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display_eval_error() {
        assert_eq!(
            EvalError::new("foo_bar(1, 2)", "The function expected one argument").to_string(),
            "The function expected one argument: foo_bar(1, 2)"
        );
    }
}