use crate::rules::RuleIndex;
use std::{error, fmt};
#[derive(Debug)]
pub struct RulesError {
rule_index: RuleIndex,
kind: RulesErrorKind,
}
impl RulesError {
pub fn rule_index(&self) -> RuleIndex {
self.rule_index
}
pub fn kind(&self) -> &RulesErrorKind {
&self.kind
}
pub(crate) fn resolve_ref(rule_index: RuleIndex, err: guppy::Error) -> Self {
Self {
rule_index,
kind: RulesErrorKind::ResolveRef(err),
}
}
pub(crate) fn glob_parse(rule_index: RuleIndex, err: globset::Error) -> Self {
let kind = RulesErrorKind::GlobParse {
glob: err.glob().map(|s| s.to_owned()),
err: Box::new(err),
};
Self { rule_index, kind }
}
}
impl fmt::Display for RulesError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"error while resolving determinator rules: {}: {}",
self.rule_index, self.kind
)
}
}
impl error::Error for RulesError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.kind {
RulesErrorKind::ResolveRef(err) => Some(err),
RulesErrorKind::GlobParse { err, .. } => Some(&**err),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum RulesErrorKind {
ResolveRef(guppy::Error),
GlobParse {
glob: Option<String>,
err: Box<dyn error::Error + Send + Sync>,
},
}
impl fmt::Display for RulesErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RulesErrorKind::ResolveRef(err) => write!(f, "{}", err),
RulesErrorKind::GlobParse {
glob: Some(glob),
err,
} => write!(f, "while parsing glob '{}': {}", glob, err),
RulesErrorKind::GlobParse { glob: None, err } => {
write!(f, "while parsing a glob: {}", err)
}
}
}
}