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 Custom(String),
26
27 ParseError(String),
29
30 Thrown(serde_json::Value),
32
33 FormatError(String),
35
36 IndexOutOfBounds { index: isize, length: usize },
38
39 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}