datalogic_rs/
error.rs

1use std::fmt;
2
3/// Error type for DataLogic operations
4#[derive(Debug, Clone)]
5pub enum Error {
6    /// Invalid operator name
7    InvalidOperator(String),
8
9    /// Invalid arguments for an operator
10    InvalidArguments(String),
11
12    /// Variable not found in context
13    VariableNotFound(String),
14
15    /// Invalid context level access
16    InvalidContextLevel(isize),
17
18    /// Type conversion/coercion error
19    TypeError(String),
20
21    /// Arithmetic error (division by zero, overflow, etc.)
22    ArithmeticError(String),
23
24    /// Division by zero (deprecated - use ArithmeticError)
25    DivisionByZero,
26
27    /// Custom error for extensions
28    Custom(String),
29
30    /// JSON parsing/serialization error
31    ParseError(String),
32
33    /// Thrown error from throw operator
34    Thrown(serde_json::Value),
35
36    /// Invalid format string or pattern
37    FormatError(String),
38
39    /// Index out of bounds for array operations
40    IndexOutOfBounds { index: isize, length: usize },
41
42    /// Invalid operator configuration
43    ConfigurationError(String),
44}
45
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Error::InvalidOperator(op) => write!(f, "Invalid operator: {}", op),
50            Error::InvalidArguments(msg) => write!(f, "Invalid arguments: {}", msg),
51            Error::VariableNotFound(var) => write!(f, "Variable not found: {}", var),
52            Error::InvalidContextLevel(level) => write!(f, "Invalid context level: {}", level),
53            Error::TypeError(msg) => write!(f, "Type error: {}", msg),
54            Error::ArithmeticError(msg) => write!(f, "Arithmetic error: {}", msg),
55            Error::DivisionByZero => write!(f, "Division by zero"),
56            Error::Custom(msg) => write!(f, "{}", msg),
57            Error::ParseError(msg) => write!(f, "Parse error: {}", msg),
58            Error::Thrown(val) => write!(f, "Thrown: {}", val),
59            Error::FormatError(msg) => write!(f, "Format error: {}", msg),
60            Error::IndexOutOfBounds { index, length } => {
61                write!(
62                    f,
63                    "Index {} out of bounds for array of length {}",
64                    index, length
65                )
66            }
67            Error::ConfigurationError(msg) => write!(f, "Configuration error: {}", msg),
68        }
69    }
70}
71
72impl std::error::Error for Error {}
73
74impl From<serde_json::Error> for Error {
75    fn from(err: serde_json::Error) -> Self {
76        Error::ParseError(err.to_string())
77    }
78}