use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
Compile(i32),
NulByte(usize),
}
impl Error {
pub fn code(&self) -> Option<i32> {
match *self {
Self::Compile(code) => Some(code),
Self::NulByte(_) => None,
}
}
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::NulByte(offset) => write!(f, "NUL byte in pattern at offset {offset}"),
Self::Compile(code) => {
let message = match code {
1 => "internal error",
2 => "expected closing group",
3 => "invalid backreference",
4 => "invalid options",
5 => "lookbehind does not have constant byte length",
6 => "malformed conditional",
7 => "maximum repetition count exceeded",
8 => "minimum repetition count exceeds maximum",
9 => "too many bytecode instructions",
10 => "too many captures",
11 => "too many progress checks",
12 => "unable to parse group type",
13 => "unable to parse repetition",
14 => "unable to resolve recursion target",
15 => "unexpected control character",
16 => "unexpected end of pattern",
17 => "unexpected group options",
18 => "unexpected hex character",
19 => "unexpected lookbehind type",
20 => "unexpected token",
21 => "unknown escape",
22 => "unknown POSIX character class",
23 => "malformed Unicode property",
24 => "unknown Unicode property",
_ => "unknown error",
};
write!(f, "JavelinPattern compilation failed: {message} ({code})")
}
}
}
}