onedrop_eval/
error.rs

1//! Error types for the onedrop-eval crate.
2
3use std::fmt;
4
5/// Result type alias for onedrop-eval operations.
6pub type Result<T> = std::result::Result<T, EvalError>;
7
8/// Errors that can occur during expression evaluation.
9#[derive(Debug, Clone, PartialEq)]
10pub enum EvalError {
11    /// Syntax error in expression
12    SyntaxError { expression: String, reason: String },
13
14    /// Undefined variable
15    UndefinedVariable(String),
16
17    /// Undefined function
18    UndefinedFunction(String),
19
20    /// Type mismatch
21    TypeError { expected: String, got: String },
22
23    /// Division by zero
24    DivisionByZero,
25
26    /// Generic evaluation error
27    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}