1use thiserror::Error;
7
8#[derive(Debug, Clone, PartialEq, Error)]
10#[non_exhaustive]
11pub enum EvaluationError {
12 #[error("undefined variable '{name}'")]
14 UndefinedVariable {
15 name: String,
17 },
18
19 #[error("type mismatch: expected {expected}, found {found}")]
21 TypeMismatch {
22 expected: String,
24 found: String,
26 },
27
28 #[error("division by zero")]
30 DivisionByZero,
31
32 #[error("function '{name}' not found")]
34 FunctionNotFound {
35 name: String,
37 },
38
39 #[error("wrong arity for '{name}': expected {expected}, got {got}")]
41 WrongArity {
42 name: String,
44 expected: usize,
46 got: usize,
48 },
49
50 #[error("JIT compilation error: {message}")]
52 JitCompilationError {
53 message: String,
55 },
56
57 #[error("unsupported operation: {message}")]
59 UnsupportedOperation {
60 message: String,
62 },
63}
64
65pub type Result<T> = std::result::Result<T, EvaluationError>;
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn display_undefined_variable() {
74 let err = EvaluationError::UndefinedVariable { name: "x".into() };
75 assert_eq!(err.to_string(), "undefined variable 'x'");
76 }
77
78 #[test]
79 fn display_division_by_zero() {
80 let err = EvaluationError::DivisionByZero;
81 assert_eq!(err.to_string(), "division by zero");
82 }
83
84 #[test]
85 fn display_wrong_arity() {
86 let err = EvaluationError::WrongArity {
87 name: "f".into(),
88 expected: 2,
89 got: 1,
90 };
91 assert_eq!(err.to_string(), "wrong arity for 'f': expected 2, got 1");
92 }
93}