use crate::registry::Violation;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Config(String),
Validation(Vec<Violation>),
NotFound(String),
Stale { expected: String, actual: String },
Io(String),
Parse(String),
Schema(String),
}
impl Error {
pub fn exit_code(&self) -> u8 {
match self {
Error::Validation(_) | Error::NotFound(_) => 1,
Error::Stale { .. } => 2,
Error::Config(_) | Error::Io(_) | Error::Parse(_) | Error::Schema(_) => 3,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Config(m) => write!(f, "config error: {m}"),
Error::Validation(v) => write!(f, "validation failed: {} violation(s)", v.len()),
Error::NotFound(m) => write!(f, "not found: {m}"),
Error::Stale { expected, actual } => {
write!(
f,
"index is stale: expected content-hash {expected}, got {actual}"
)
}
Error::Io(m) => write!(f, "io error: {m}"),
Error::Parse(m) => write!(f, "parse error: {m}"),
Error::Schema(m) => write!(f, "schema error: {m}"),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;