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}
16
17impl From<String> for Error {
18    fn from(s: String) -> Self {
19        Error::ExprError(s)
20    }
21}
22
23impl Debug for Error {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Error::PestError(e) => write!(f, "PestError: {}", e),
27            Error::ParseError(e) => write!(f, "ParseError: {}", e),
28            Error::ExprError(e) => write!(f, "ExprError: {}", e),
29            Error::RegexError(e) => write!(f, "RegexError: {}", e),
30        }
31    }
32}
33
34pub type Result<T> = std::result::Result<T, Error>;
35
36#[macro_export]
37macro_rules! bail {
38    ($($arg:tt)*) => {
39        return Err($crate::Error::ExprError(format!($($arg)*)))
40    };
41}