try-exit 1.0.0

Dependency free, simple error handling for small programs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//! Dependency free, simple error handling for small programs

/// Attempts to unwrap a `Result`. If it's an `Err`, exit program with given error message.
pub fn try<T, E>(value: Result<T, E>, error_message: &str) -> T {
    value.unwrap_or_else(|_| {
        eprintln!("{}", error_message);
        std::process::exit(1);
    })
}

/// Attempts to unwrap an `Option`. If it's a `None`, exit program with given error message.
pub fn try_opt<T>(value: Option<T>, error_message: &str) -> T {
    value.unwrap_or_else(|| {
        eprintln!("{}", error_message);
        std::process::exit(1);
    })
}