equation_solver/
error.rs

1use std::fmt::{Display, Formatter};
2
3/// The EquationError struct is used to represent an error that can occur in the equation solver.
4#[derive(Debug, Clone, PartialEq)]
5pub struct EquationError {
6    /// The message of the error.
7    pub message: String,
8    /// The type of the error.
9    pub type_: EquationErrorType,
10}
11
12impl EquationError {
13    /// Creates a new EquationError from the message and the type of error.
14    pub fn new(message: String, type_: EquationErrorType) -> EquationError {
15        EquationError { message, type_ }
16    }
17}
18
19/// The EquationErrorType enum is used to represent the type of error that can occur in the equation solver.
20#[derive(Debug, Clone, PartialEq)]
21pub enum EquationErrorType {
22    /// Items were missing in the equation.
23    MissingItems,
24    /// An unexpected token was found in the equation.
25    UnexpectedToken,
26    /// An unset variable was found.
27    UnsetVariable,
28}
29
30impl Display for EquationError {
31    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32        write!(f, "{:?} {}", self.type_, self.message)
33    }
34}
35
36impl std::error::Error for EquationError {
37    fn description(&self) -> &str {
38        &self.message
39    }
40}