expr/
error.rs

1use crate::Rule;
2use std::fmt::Debug;
3use thiserror::Error;
4/// An error that can occur when parsing or evaluating an expr program
5#[derive(Error)]
6pub enum Error {
7    #[error(transparent)]
8    PestError(#[from] Box<pest::error::Error<Rule>>),
9    #[error("{0}")]
10    ParseError(String),
11    #[error("{0}")]
12    ExprError(String),
13    #[error(transparent)]
14    RegexError(#[from] regex::Error),
15    #[cfg(feature = "serde")]
16    #[error("{0}")]
17    DeserializeError(String),
18    #[cfg(feature = "serde")]
19    #[error("{0}")]
20    SerializeError(String),
21}
22
23impl From<String> for Error {
24    fn from(s: String) -> Self {
25        Error::ExprError(s)
26    }
27}
28
29impl Debug for Error {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            Error::PestError(e) => write!(f, "PestError: {}", e),
33            Error::ParseError(e) => write!(f, "ParseError: {}", e),
34            Error::ExprError(e) => write!(f, "ExprError: {}", e),
35            Error::RegexError(e) => write!(f, "RegexError: {}", e),
36            #[cfg(feature = "serde")]
37            Error::DeserializeError(e) => write!(f, "DeserializeError: {}", e),
38            #[cfg(feature = "serde")]
39            Error::SerializeError(e) => write!(f, "SerializeError: {}", e),
40        }
41    }
42}
43
44pub type Result<T> = std::result::Result<T, Error>;
45
46#[macro_export]
47macro_rules! bail {
48    ($($arg:tt)*) => {
49        return Err($crate::Error::ExprError(format!($($arg)*)))
50    };
51}