Skip to main content

ocas_eval/
error.rs

1//! Evaluation-specific error types.
2//!
3//! These errors are produced during numeric evaluation, compilation,
4//! and JIT code generation.
5
6use thiserror::Error;
7
8/// Errors that can occur during expression evaluation.
9#[derive(Debug, Clone, PartialEq, Error)]
10#[non_exhaustive]
11pub enum EvaluationError {
12    /// A variable referenced in the expression was not provided.
13    #[error("undefined variable '{name}'")]
14    UndefinedVariable {
15        /// Name of the undefined variable.
16        name: String,
17    },
18
19    /// A type mismatch occurred (e.g. integer where float was expected).
20    #[error("type mismatch: expected {expected}, found {found}")]
21    TypeMismatch {
22        /// Expected type description.
23        expected: String,
24        /// Actual type description.
25        found: String,
26    },
27
28    /// Division by zero.
29    #[error("division by zero")]
30    DivisionByZero,
31
32    /// A user-defined function was not found in the registry.
33    #[error("function '{name}' not found")]
34    FunctionNotFound {
35        /// Name of the missing function.
36        name: String,
37    },
38
39    /// A function was called with the wrong number of arguments.
40    #[error("wrong arity for '{name}': expected {expected}, got {got}")]
41    WrongArity {
42        /// Name of the function.
43        name: String,
44        /// Expected number of arguments.
45        expected: usize,
46        /// Actual number of arguments.
47        got: usize,
48    },
49
50    /// JIT compilation failed.
51    #[error("JIT compilation error: {message}")]
52    JitCompilationError {
53        /// Description of the compilation failure.
54        message: String,
55    },
56
57    /// The requested operation is not supported for this domain.
58    #[error("unsupported operation: {message}")]
59    UnsupportedOperation {
60        /// Description of the unsupported operation.
61        message: String,
62    },
63}
64
65/// A convenient result type for evaluation operations.
66pub 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}