diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-07-30 11:45:31 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-07-30 11:45:31 +0200 |
commit | deb1544bc566b3117a97b72ee2d98efdbba58616 (patch) | |
tree | b34af05eb905cdf33d4e2b24fd2b82306b495cc5 /libimagutil | |
parent | 0b1ea94dae6e323b96cc150cd22f285a704f6af7 (diff) |
Add info_result
Diffstat (limited to 'libimagutil')
-rw-r--r-- | libimagutil/src/info_result.rs | 28 | ||||
-rw-r--r-- | libimagutil/src/lib.rs | 1 |
2 files changed, 29 insertions, 0 deletions
diff --git a/libimagutil/src/info_result.rs b/libimagutil/src/info_result.rs new file mode 100644 index 00000000..0c40612b --- /dev/null +++ b/libimagutil/src/info_result.rs @@ -0,0 +1,28 @@ +pub trait InfoResult<T, E> : Sized { + + fn map_info<F: FnOnce(&T) -> String>(self, f: F) -> Self; + + fn map_info_str(self, s: &str) -> Self { + self.map_info(|_| format!("{}", s)) + } + + fn map_info_err<F: FnOnce(&E) -> String>(self, f: F) -> Self; + + fn map_info_err_str(self, s: &str) -> Self { + self.map_info_err(|_| format!("{}", s)) + } + +} + +impl<T, E> InfoResult<T, E> for Result<T, E> { + + fn map_info<F: FnOnce(&T) -> String>(self, f: F) -> Self { + self.map(|t| { info!("{}", f(&t)); t }) + } + + fn map_info_err<F: FnOnce(&E) -> String>(self, f: F) -> Self { + self.map_err(|e| { info!("{}", f(&e)); e }) + } + +} + diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 0eafd4ef..a05d14e8 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -18,6 +18,7 @@ extern crate regex; pub mod debug_result; +pub mod info_result; pub mod ismatch; pub mod iter; pub mod key_value_split; |