use std::{error::Error, fmt};
use crate::error::parse::variable::VariableError;
#[derive(Debug)]
pub struct ExpressionError {
pub content: String,
pub kind: ExpressionErrorKind,
}
#[derive(Debug)]
pub enum ExpressionErrorKind {
Empty,
InvalidHead { head: String },
InvalidVariable(VariableError),
NoOperator { content: String },
UnmatchedParenthesis,
}
impl Error for ExpressionError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
impl Error for ExpressionErrorKind {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self {
ExpressionErrorKind::InvalidVariable(err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for ExpressionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} (expression string: '{}')", self.kind, self.content)
}
}
impl fmt::Display for ExpressionErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ExpressionErrorKind::*;
match &self {
Empty => write!(f, "empty string"),
InvalidHead { head } => write!(f, "no left hand side value before '{}'", head),
InvalidVariable(err) => write!(f, "invalid variable: {}", err),
NoOperator { content } => {
write!(f, "no mathematical operator before operand '{}'", content)
}
UnmatchedParenthesis => write!(f, "found unmatched parenthesis",),
}
}
}