1use std::borrow::Cow;
2use std::fmt;
3use std::fmt::Formatter;
4
5#[derive(Debug, PartialEq)]
6pub enum Error {
7 InvalidInput(String),
8 Eval(EvalError),
9}
10
11impl fmt::Display for Error {
12 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
13 use Error::*;
14 match self {
15 InvalidInput(msg) => write!(f, "invalid input: {}", msg),
16 Eval(err) => write!(f, "{}", err),
17 }
18 }
19}
20
21impl std::error::Error for Error {}
22
23#[derive(Debug)]
24pub(crate) struct InvalidOperatorError<'a> {
25 raw: Cow<'a, str>,
26}
27
28impl<'a> InvalidOperatorError<'a> {
29 pub(crate) fn new(op: impl Into<Cow<'a, str>>) -> Self {
30 InvalidOperatorError { raw: op.into() }
31 }
32}
33
34impl<'a> fmt::Display for InvalidOperatorError<'a> {
35 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
36 write!(
37 f,
38 "invalid operator '{}' currently ['+', '-', '*', '/'] supported",
39 self.raw
40 )
41 }
42}
43
44impl<'a> std::error::Error for InvalidOperatorError<'a> {}
45
46#[derive(Debug, PartialEq)]
47pub enum EvalError {
48 ZeroDivision,
49}
50
51impl fmt::Display for EvalError {
52 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53 use EvalError::*;
54 match self {
55 ZeroDivision => write!(f, "divided by zero"),
56 }
57 }
58}
59
60impl std::error::Error for EvalError {}