use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Error)]
#[non_exhaustive]
pub enum EvaluationError {
#[error("undefined variable '{name}'")]
UndefinedVariable {
name: String,
},
#[error("type mismatch: expected {expected}, found {found}")]
TypeMismatch {
expected: String,
found: String,
},
#[error("division by zero")]
DivisionByZero,
#[error("function '{name}' not found")]
FunctionNotFound {
name: String,
},
#[error("wrong arity for '{name}': expected {expected}, got {got}")]
WrongArity {
name: String,
expected: usize,
got: usize,
},
#[error("JIT compilation error: {message}")]
JitCompilationError {
message: String,
},
#[error("unsupported operation: {message}")]
UnsupportedOperation {
message: String,
},
}
pub type Result<T> = std::result::Result<T, EvaluationError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_undefined_variable() {
let err = EvaluationError::UndefinedVariable { name: "x".into() };
assert_eq!(err.to_string(), "undefined variable 'x'");
}
#[test]
fn display_division_by_zero() {
let err = EvaluationError::DivisionByZero;
assert_eq!(err.to_string(), "division by zero");
}
#[test]
fn display_wrong_arity() {
let err = EvaluationError::WrongArity {
name: "f".into(),
expected: 2,
got: 1,
};
assert_eq!(err.to_string(), "wrong arity for 'f': expected 2, got 1");
}
}