use std::{
env,
fmt::Display,
process::{ExitCode, Termination},
};
pub struct MainResult<T, E>
where
T: Termination,
E: Display,
{
underlying: Result<T, E>,
}
impl<T, E> Termination for MainResult<T, E>
where
T: Termination,
E: Display,
{
fn report(self) -> ExitCode {
match self.underlying {
Ok(x) => x.report(),
Err(e) => {
let argv0 = env::args().next().unwrap_or("Error".into());
eprint!("{argv0}: {e}");
ExitCode::FAILURE
}
}
}
}
impl<T, E> From<Result<T, E>> for MainResult<T, E>
where
T: Termination,
E: Display,
{
fn from(value: Result<T, E>) -> Self {
MainResult { underlying: value }
}
}