use std::fmt;
use forbidden_regex::CompileError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoadError {
NoRules,
UnsupportedFlag {
index: usize,
flag: char,
},
Compile {
index: usize,
reason: CompileError,
},
Precompiled {
reason: CompileError,
},
NearHeader {
line: usize,
},
PreHeaderContent {
line: usize,
},
EmptySection {
line: usize,
},
DuplicateName {
first_line: usize,
line: usize,
},
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LoadError::NoRules => {
return write!(f, "no rules loaded")
}
LoadError::UnsupportedFlag { index, flag } => {
return write!(
f,
"rule {index}: unsupported flag '{flag}'; only 'm' and 'x' are accepted as no-ops",
)
}
LoadError::Compile { index, reason } => {
return write!(f, "rule {index}: {reason}")
}
LoadError::Precompiled { reason } => {
return write!(f, "precompiled ruleset failed to load: {reason}")
}
LoadError::NearHeader { line } => {
return write!(
f,
"line {line}: line starts with '==>' but is not a strict '==> name <==' section header; reshape genuine content as '[=]=>'",
)
}
LoadError::PreHeaderContent { line } => {
return write!(f, "line {line}: content before the first section header")
}
LoadError::EmptySection { line } => {
return write!(f, "line {line}: section header has no rule body")
}
LoadError::DuplicateName { first_line, line } => {
return write!(
f,
"line {line}: duplicate section name first declared at line {first_line}",
)
}
}
}
}
impl std::error::Error for LoadError {}
#[cfg(test)]
#[path = "error_tests.rs"]
mod error_tests;