use crate::ffi;
use std::fmt;
use std::os::raw::c_int;
#[derive(Debug)]
pub enum Error {
Usage,
ExitCode(c_int),
GenericError(Box<dyn std::error::Error>),
}
impl Error {
#[doc(hidden)]
pub fn print_on_return(&self) -> bool {
let ignore = matches!(self, Error::Usage | Error::ExitCode(_));
!ignore
}
#[doc(hidden)]
pub fn exit_code(&self) -> c_int {
match self {
Error::Usage => ffi::exit::EX_USAGE,
Error::ExitCode(s) => *s,
_ => ffi::exit::EXECUTION_FAILURE,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Usage => fmt.write_str("usage error"),
Error::ExitCode(s) => write!(fmt, "exit code {}", s),
Error::GenericError(e) => e.fmt(fmt),
}
}
}
impl<E> From<E> for Error
where
E: std::error::Error + 'static,
{
fn from(error: E) -> Self {
Error::GenericError(Box::new(error))
}
}
pub type Result<T> = std::result::Result<T, Error>;