expr/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::Rule;
use std::fmt::Debug;
use thiserror::Error;
/// An error that can occur when parsing or evaluating an expr program
#[derive(Error)]
pub enum Error {
    #[error(transparent)]
    PestError(#[from] Box<pest::error::Error<Rule>>),
    #[error("{0}")]
    ParseError(String),
    #[error("{0}")]
    ExprError(String),
    #[error(transparent)]
    RegexError(#[from] regex::Error),
}

impl From<String> for Error {
    fn from(s: String) -> Self {
        Error::ExprError(s)
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::PestError(e) => write!(f, "PestError: {}", e),
            Error::ParseError(e) => write!(f, "ParseError: {}", e),
            Error::ExprError(e) => write!(f, "ExprError: {}", e),
            Error::RegexError(e) => write!(f, "RegexError: {}", e),
        }
    }
}

pub type Result<T> = std::result::Result<T, Error>;

#[macro_export]
macro_rules! bail {
    ($($arg:tt)*) => {
        return Err($crate::Error::ExprError(format!($($arg)*)))
    };
}