1use std::fmt;
2
3#[derive(Debug, Clone)]
5pub enum Error {
6 InvalidOperator(String),
8
9 InvalidArguments(String),
11
12 VariableNotFound(String),
14
15 InvalidContextLevel(isize),
17
18 TypeError(String),
20
21 ArithmeticError(String),
23
24 DivisionByZero,
26
27 Custom(String),
29
30 ParseError(String),
32
33 Thrown(serde_json::Value),
35
36 FormatError(String),
38
39 IndexOutOfBounds { index: isize, length: usize },
41
42 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}