use std::fmt;
#[derive(Debug)]
pub enum AppError {
Usage(String),
Verify(String),
Io(String),
Internal(String),
}
impl AppError {
pub fn exit_code(&self) -> u8 {
match self {
AppError::Usage(_) => 2,
AppError::Internal(_) | AppError::Verify(_) => 70,
AppError::Io(_) => 74,
}
}
}
impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppError::Usage(m) | AppError::Verify(m) | AppError::Io(m) | AppError::Internal(m) => {
write!(f, "{m}")
}
}
}
}
impl std::error::Error for AppError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exit_codes_map() {
assert_eq!(AppError::Usage("x".into()).exit_code(), 2);
assert_eq!(AppError::Verify("x".into()).exit_code(), 70);
assert_eq!(AppError::Internal("x".into()).exit_code(), 70);
assert_eq!(AppError::Io("x".into()).exit_code(), 74);
}
}