From b2f764d004cf4ea6972000142f846274dfd66c8b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 4 Sep 2016 17:50:55 +0200 Subject: [PATCH] Add warn_exit() convenience helper --- libimagutil/src/lib.rs | 1 + libimagutil/src/warn_exit.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 libimagutil/src/warn_exit.rs diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 4d561a4d..16183e7b 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -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; diff --git a/libimagutil/src/warn_exit.rs b/libimagutil/src/warn_exit.rs new file mode 100644 index 00000000..399ebcd8 --- /dev/null +++ b/libimagutil/src/warn_exit.rs @@ -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 = 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); +} +