#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum ParseError {
UnexpectedTrailingInput(String),
InvalidSyntax(String),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseError::UnexpectedTrailingInput(s) => {
write!(f, "Unexpected trailing input: {}", s)
}
ParseError::InvalidSyntax(s) => write!(f, "Invalid syntax: {}", s),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {}
#[derive(Debug, Clone, PartialEq)]
pub enum EvalError {
UnknownVariable(String),
DivisionByZero,
UnknownFunction(String),
WrongArity {
name: String,
expected: usize,
got: usize,
},
MathError(String),
}
impl fmt::Display for EvalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EvalError::UnknownVariable(name) => write!(f, "Unknown variable: {}", name),
EvalError::DivisionByZero => write!(f, "Division by zero"),
EvalError::UnknownFunction(name) => write!(f, "Unknown function: {}", name),
EvalError::WrongArity {
name,
expected,
got,
} => {
write!(
f,
"Function {} expects {} args, got {}",
name, expected, got
)
}
EvalError::MathError(msg) => write!(f, "Math error: {}", msg),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for EvalError {}
#[derive(Debug, Clone, PartialEq)]
pub enum CompileError {
UndefinedVariable(String),
UnknownFunction(String),
WrongArity {
name: String,
expected: usize,
got: usize,
},
}
impl fmt::Display for CompileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CompileError::UndefinedVariable(name) => {
write!(f, "Undefined variable: {}", name)
}
CompileError::UnknownFunction(name) => {
write!(f, "Unknown function: {}", name)
}
CompileError::WrongArity {
name,
expected,
got,
} => {
write!(
f,
"Function {} expects {} args, got {}",
name, expected, got
)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for CompileError {}