extern crate alloc;
use alloc::string::String;
#[cfg(not(test))]
use core::num::ParseFloatError;
#[cfg(test)]
use std::num::ParseFloatError;
#[cfg(not(test))]
use core::result;
#[cfg(test)]
use std::result;
pub type Result<T> = result::Result<T, ExprError>;
#[repr(C, align(4))]
#[derive(Debug, Clone)]
pub enum ExprError {
Parse(ParseFloatError),
Tokenizer(String),
Syntax(String),
UnmatchedParenthesis { position: usize, found: String },
UnknownVariable { name: String },
UnknownFunction { name: String },
InvalidFunctionCall {
name: String,
expected: usize,
found: usize,
},
ArrayIndexOutOfBounds {
name: String,
index: usize,
len: usize,
},
AttributeNotFound {
base: String,
attr: String,
},
DivideByZero,
Other(String),
RecursionLimit(String),
CapacityExceeded(&'static str),
StringTooLong(String, usize),
DuplicateParameter(String),
InvalidParameterIndex(usize),
}
impl ExprError {
pub fn error_code(&self) -> i32 {
match self {
ExprError::Parse(_) => 1,
ExprError::Tokenizer(_) => 2,
ExprError::Syntax(_) => 3,
ExprError::UnmatchedParenthesis { .. } => 4,
ExprError::UnknownVariable { .. } => 5,
ExprError::UnknownFunction { .. } => 6,
ExprError::InvalidFunctionCall { .. } => 7,
ExprError::ArrayIndexOutOfBounds { .. } => 8,
ExprError::AttributeNotFound { .. } => 9,
ExprError::DivideByZero => 10,
ExprError::RecursionLimit(_) => 11,
ExprError::CapacityExceeded(_) => 12,
ExprError::StringTooLong(_, _) => 13,
ExprError::DuplicateParameter(_) => 14,
ExprError::InvalidParameterIndex(_) => 15,
ExprError::Other(_) => 99,
}
}
}
#[cfg(not(test))]
use core::fmt;
#[cfg(test)]
use std::fmt;
impl fmt::Display for ExprError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExprError::Parse(_) => write!(f, "Parse error"),
ExprError::Tokenizer(err) => write!(f, "Tokenizer error: {}", err),
ExprError::Syntax(err) => write!(f, "Syntax error: {}", err),
ExprError::UnmatchedParenthesis { position, found } => {
write!(
f,
"Unmatched parenthesis at position {}: found '{}'",
position, found
)
}
ExprError::UnknownVariable { name } => {
write!(f, "Unknown variable: '{}'", name)
}
ExprError::UnknownFunction { name } => {
write!(f, "Unknown function: '{}'", name)
}
ExprError::InvalidFunctionCall {
name,
expected,
found,
} => {
write!(
f,
"Invalid function call to '{}': expected {} arguments, found {}",
name, expected, found
)
}
ExprError::ArrayIndexOutOfBounds { name, index, len } => {
write!(
f,
"Array index out of bounds: index {} out of bounds for '{}', length {}",
index, name, len
)
}
ExprError::AttributeNotFound { base, attr } => {
write!(f, "Attribute not found: '{}' in '{}'", attr, base)
}
ExprError::DivideByZero => write!(f, "Division by zero"),
ExprError::Other(err) => write!(f, "{}", err),
ExprError::RecursionLimit(err) => write!(f, "Recursion limit exceeded: {}", err),
ExprError::CapacityExceeded(container_type) => {
write!(f, "Capacity exceeded for {}", container_type)
}
ExprError::StringTooLong(s, max_len) => write!(f, "String too long for heapless buffer (max {} chars): '{}'", max_len, s),
ExprError::DuplicateParameter(name) => write!(f, "Parameter '{}' already exists", name),
ExprError::InvalidParameterIndex(idx) => write!(f, "Invalid parameter index: {}", idx),
}
}
}
impl From<String> for ExprError {
fn from(err: String) -> ExprError {
ExprError::Other(err)
}
}
impl From<ParseFloatError> for ExprError {
fn from(err: ParseFloatError) -> ExprError {
ExprError::Parse(err)
}
}