Merge pull request #695 from matthiasbeyer/libimagutil/warn-exit

libimagutil: Add warn_exit() convenience helper
This commit is contained in:
Matthias Beyer 2016-09-05 19:24:01 +02:00 committed by GitHub
commit 7bf654158e
2 changed files with 23 additions and 0 deletions

View file

@ -24,4 +24,5 @@ pub mod ismatch;
pub mod iter;
pub mod key_value_split;
pub mod variants;
pub mod warn_exit;
pub mod warn_result;

View file

@ -0,0 +1,22 @@
/// This function prints the string `s` via the `warn!()` macro and then exits with the code `code`
/// as status.
///
/// Convenience function to be used in matches to remove one scope:
///
/// ```ignore
/// use libimagutil::warn_exit::warn_exit;
///
/// let r: Result<i32, i32> = Err(1);
/// match r {
/// Err(e) => warn_exit("Warning!", 42),
/// Ok(num) => { /* ... */ }
/// }
/// ```
///
pub fn warn_exit(s: &str, code: i32) -> ! {
use std::process::exit;
warn!("{}", s);
exit(code);
}