1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
use std::fmt; pub mod argparse; pub trait OrExit<T> { fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T; } impl<T, E: fmt::Display> OrExit<T> for Result<T, E> { fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T { match self { Err(why) => { match with_msg_prefix { None => eprintln!("{}", why), Some(msg) => eprintln!("{}: {}", msg, why), }; std::process::exit(1); } Ok(value) => value, } } } impl<T> OrExit<T> for Option<T> { fn unwrap_or_exit<M: fmt::Display>(self, with_msg_prefix: Option<M>) -> T { match self { None => { match with_msg_prefix { None => eprintln!("expected the Option to have some value"), Some(msg) => eprintln!("{}", msg), }; std::process::exit(1); } Some(value) => value, } } }