mathexpr 0.1.1

A fast, safe mathematical expression parser and evaluator with bytecode compilation
Documentation
//! Error types for mathexpr.
//!
//! This module provides error types for parsing and evaluation failures.

#[cfg(not(feature = "std"))]
use alloc::string::String;

use core::fmt;

/// Error returned when parsing a mathematical expression fails.
#[derive(Debug, Clone, PartialEq)]
pub enum ParseError {
    /// The parser succeeded but there was unexpected input remaining.
    UnexpectedTrailingInput(String),
    /// The expression contains invalid syntax.
    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 {}

/// Error returned when evaluating a compiled expression fails.
#[derive(Debug, Clone, PartialEq)]
pub enum EvalError {
    /// A variable was referenced that wasn't provided during compilation.
    UnknownVariable(String),
    /// Division or modulo by zero.
    DivisionByZero,
    /// An unknown function was called.
    UnknownFunction(String),
    /// A function was called with the wrong number of arguments.
    WrongArity {
        /// The function name.
        name: String,
        /// Expected number of arguments.
        expected: usize,
        /// Actual number of arguments provided.
        got: usize,
    },
    /// A mathematical error occurred (e.g., sqrt of negative number).
    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 {}

/// Error returned when compiling an expression fails.
#[derive(Debug, Clone, PartialEq)]
pub enum CompileError {
    /// A variable in the expression was not in the provided variable list.
    UndefinedVariable(String),
    /// An unknown function was used in the expression.
    UnknownFunction(String),
    /// A function was called with the wrong number of arguments.
    WrongArity {
        /// The function name.
        name: String,
        /// Expected number of arguments.
        expected: usize,
        /// Actual number of arguments provided.
        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 {}