use thiserror::Error;
pub type Result<T> = std::result::Result<T, ScnrError>;
#[derive(Error, Debug)]
pub struct ScnrError {
pub source: Box<ScnrErrorKind>,
}
impl ScnrError {
pub fn new(kind: ScnrErrorKind) -> Self {
ScnrError {
source: Box::new(kind),
}
}
}
impl std::fmt::Display for ScnrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.source)
}
}
#[derive(Error, Debug)]
pub enum ScnrErrorKind {
#[error("'{1}' {0}")]
RegexSyntaxError(regex_syntax::ast::Error, String),
#[cfg(feature = "regex_automata")]
#[error(transparent)]
RegexBuildError(#[from] regex_automata::dfa::dense::BuildError),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("Unsupported regex feature: {0}")]
UnsupportedFeature(String),
#[error("Empty tokens are not allowed.")]
EmptyToken,
}
impl From<regex_syntax::ast::Error> for ScnrError {
fn from(error: regex_syntax::ast::Error) -> Self {
ScnrError::new(ScnrErrorKind::RegexSyntaxError(error, "!".to_string()))
}
}
impl From<std::io::Error> for ScnrError {
fn from(error: std::io::Error) -> Self {
ScnrError::new(ScnrErrorKind::IoError(error))
}
}