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    /// Custom error for extensions
25    Custom(String),
26
27    /// JSON parsing/serialization error
28    ParseError(String),
29
30    /// Thrown error from throw operator
31    Thrown(serde_json::Value),
32
33    /// Invalid format string or pattern
34    FormatError(String),
35
36    /// Index out of bounds for array operations
37    IndexOutOfBounds { index: isize, length: usize },
38
39    /// Invalid operator configuration
40    ConfigurationError(String),
41}
42
43impl fmt::Display for Error {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Error::InvalidOperator(op) => write!(f, "Invalid operator: {}", op),
47            Error::InvalidArguments(msg) => write!(f, "Invalid arguments: {}", msg),
48            Error::VariableNotFound(var) => write!(f, "Variable not found: {}", var),
49            Error::InvalidContextLevel(level) => write!(f, "Invalid context level: {}", level),
50            Error::TypeError(msg) => write!(f, "Type error: {}", msg),
51            Error::ArithmeticError(msg) => write!(f, "Arithmetic error: {}", msg),
52            Error::Custom(msg) => write!(f, "{}", msg),
53            Error::ParseError(msg) => write!(f, "Parse error: {}", msg),
54            Error::Thrown(val) => write!(f, "Thrown: {}", val),
55            Error::FormatError(msg) => write!(f, "Format error: {}", msg),
56            Error::IndexOutOfBounds { index, length } => {
57                write!(
58                    f,
59                    "Index {} out of bounds for array of length {}",
60                    index, length
61                )
62            }
63            Error::ConfigurationError(msg) => write!(f, "Configuration error: {}", msg),
64        }
65    }
66}
67
68impl std::error::Error for Error {}
69
70impl From<serde_json::Error> for Error {
71    fn from(err: serde_json::Error) -> Self {
72        Error::ParseError(err.to_string())
73    }
74}