use std::error::Error;
use std::fmt::{Formatter, Display};
#[derive(Debug)]
pub struct ExitCode(i32);
impl From<i32> for ExitCode {
fn from(i: i32) -> ExitCode {
ExitCode(i)
}
}
impl ExitCode {
pub fn code(self) -> i32 {
self.0
}
}
impl Display for ExitCode {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
write!(f, "ExitCode {}", self.0)
}
}
impl Error for ExitCode {
fn description(&self) -> &str {
"ExitCode"
}
fn cause(&self) -> Option<&dyn Error> {
None
}
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
pub trait ExitUnwrap<T> {
fn unwrap_or_exit(self) -> T;
}
impl<T, E: Into<ExitCode>> ExitUnwrap<T> for Result<T, E> {
fn unwrap_or_exit(self) -> T {
self.map_err(Into::into).unwrap_or_else(|e| ::std::process::exit(e.0))
}
}