fastn_resolved/evalexpr/error/
display.rs

1use std::fmt;
2
3use fastn_resolved::evalexpr::EvalexprError;
4
5impl fmt::Display for EvalexprError {
6    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
7        use fastn_resolved::evalexpr::EvalexprError::*;
8        match self {
9            WrongOperatorArgumentAmount { expected, actual } => write!(
10                f,
11                "An operator expected {} arguments, but got {}.",
12                expected, actual
13            ),
14            WrongFunctionArgumentAmount { expected, actual } => write!(
15                f,
16                "A function expected {} arguments, but got {}.",
17                expected, actual
18            ),
19            ExpectedString { actual } => {
20                write!(f, "Expected a Value::String, but got {:?}.", actual)
21            }
22            ExpectedInt { actual } => write!(f, "Expected a Value::Int, but got {:?}.", actual),
23            ExpectedFloat { actual } => write!(f, "Expected a Value::Float, but got {:?}.", actual),
24            ExpectedNumber { actual } => write!(
25                f,
26                "Expected a Value::Float or Value::Int, but got {:?}.",
27                actual
28            ),
29            ExpectedNumberOrString { actual } => write!(
30                f,
31                "Expected a Value::Number or a Value::String, but got {:?}.",
32                actual
33            ),
34            ExpectedBoolean { actual } => {
35                write!(f, "Expected a Value::Boolean, but got {:?}.", actual)
36            }
37            ExpectedTuple { actual } => write!(f, "Expected a Value::Tuple, but got {:?}.", actual),
38            ExpectedFixedLenTuple {
39                expected_len,
40                actual,
41            } => write!(
42                f,
43                "Expected a Value::Tuple of len {}, but got {:?}.",
44                expected_len, actual
45            ),
46            ExpectedEmpty { actual } => write!(f, "Expected a Value::Empty, but got {:?}.", actual),
47            AppendedToLeafNode => write!(f, "Tried to append a node to a leaf node."),
48            PrecedenceViolation => write!(
49                f,
50                "Tried to append a node to another node with higher precedence."
51            ),
52            VariableIdentifierNotFound(identifier) => write!(
53                f,
54                "Variable identifier is not bound to anything by context: {:?}.",
55                identifier
56            ),
57            FunctionIdentifierNotFound(identifier) => write!(
58                f,
59                "Function identifier is not bound to anything by context: {:?}.",
60                identifier
61            ),
62            TypeError { expected, actual } => {
63                write!(f, "Expected one of {:?}, but got {:?}.", expected, actual)
64            }
65            WrongTypeCombination { operator, actual } => write!(
66                f,
67                "The operator {:?} was called with a wrong combination of types: {:?}",
68                operator, actual
69            ),
70            UnmatchedLBrace => write!(f, "Found an unmatched opening parenthesis '('."),
71            UnmatchedRBrace => write!(f, "Found an unmatched closing parenthesis ')'."),
72            MissingOperatorOutsideOfBrace { .. } => write!(
73                f,
74                "Found an opening parenthesis that is preceded by something that does not take \
75                 any arguments on the right, or found a closing parenthesis that is succeeded by \
76                 something that does not take any arguments on the left."
77            ),
78            UnmatchedPartialToken { first, second } => {
79                if let Some(second) = second {
80                    write!(
81                        f,
82                        "Found a partial token '{}' that should not be followed by '{}'.",
83                        first, second
84                    )
85                } else {
86                    write!(
87                        f,
88                        "Found a partial token '{}' that should be followed by another partial \
89                         token.",
90                        first
91                    )
92                }
93            }
94            AdditionError { augend, addend } => write!(f, "Error adding {} + {}", augend, addend),
95            SubtractionError {
96                minuend,
97                subtrahend,
98            } => write!(f, "Error subtracting {} - {}", minuend, subtrahend),
99            NegationError { argument } => write!(f, "Error negating -{}", argument),
100            MultiplicationError {
101                multiplicand,
102                multiplier,
103            } => write!(f, "Error multiplying {} * {}", multiplicand, multiplier),
104            DivisionError { dividend, divisor } => {
105                write!(f, "Error dividing {} / {}", dividend, divisor)
106            }
107            ModulationError { dividend, divisor } => {
108                write!(f, "Error modulating {} % {}", dividend, divisor)
109            }
110            InvalidRegex { regex, message } => write!(
111                f,
112                "Regular expression {:?} is invalid: {:?}",
113                regex, message
114            ),
115            ContextNotMutable => write!(f, "Cannot manipulate context"),
116            IllegalEscapeSequence(string) => write!(f, "Illegal escape sequence: {}", string),
117            CustomMessage(message) => write!(f, "Error: {}", message),
118        }
119    }
120}