1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, EvalError>;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum EvalError {
11 SyntaxError { expression: String, reason: String },
13
14 UndefinedVariable(String),
16
17 UndefinedFunction(String),
19
20 TypeError { expected: String, got: String },
22
23 DivisionByZero,
25
26 EvalFailed(String),
28}
29
30impl fmt::Display for EvalError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 EvalError::SyntaxError { expression, reason } => {
34 write!(f, "Syntax error in '{}': {}", expression, reason)
35 }
36 EvalError::UndefinedVariable(var) => {
37 write!(f, "Undefined variable: {}", var)
38 }
39 EvalError::UndefinedFunction(func) => {
40 write!(f, "Undefined function: {}", func)
41 }
42 EvalError::TypeError { expected, got } => {
43 write!(f, "Type error: expected {}, got {}", expected, got)
44 }
45 EvalError::DivisionByZero => {
46 write!(f, "Division by zero")
47 }
48 EvalError::EvalFailed(msg) => {
49 write!(f, "Evaluation failed: {}", msg)
50 }
51 }
52 }
53}
54
55impl std::error::Error for EvalError {}
56
57impl From<evalexpr::EvalexprError> for EvalError {
58 fn from(err: evalexpr::EvalexprError) -> Self {
59 EvalError::EvalFailed(err.to_string())
60 }
61}