try_exit/lib.rs
1//! Dependency free, simple error handling for small programs
2
3/// Attempts to unwrap a `Result`. If it's an `Err`, exit program with given error message.
4pub fn try<T, E>(value: Result<T, E>, error_message: &str) -> T {
5 value.unwrap_or_else(|_| {
6 eprintln!("{}", error_message);
7 std::process::exit(1);
8 })
9}
10
11/// Attempts to unwrap an `Option`. If it's a `None`, exit program with given error message.
12pub fn try_opt<T>(value: Option<T>, error_message: &str) -> T {
13 value.unwrap_or_else(|| {
14 eprintln!("{}", error_message);
15 std::process::exit(1);
16 })
17}