From 0b1ea94dae6e323b96cc150cd22f285a704f6af7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 29 Jul 2016 16:47:35 +0200 Subject: [PATCH] Add utility to print debugging information while mapping --- libimagutil/src/debug_result.rs | 28 ++++++++++++++++++++++++++++ libimagutil/src/lib.rs | 1 + 2 files changed, 29 insertions(+) create mode 100644 libimagutil/src/debug_result.rs diff --git a/libimagutil/src/debug_result.rs b/libimagutil/src/debug_result.rs new file mode 100644 index 00000000..d01a2c8e --- /dev/null +++ b/libimagutil/src/debug_result.rs @@ -0,0 +1,28 @@ +pub trait DebugResult : Sized { + + fn map_dbg String>(self, f: F) -> Self; + + fn map_dbg_str(self, s: &str) -> Self { + self.map_dbg(|_| format!("{}", s)) + } + + fn map_dbg_err String>(self, f: F) -> Self; + + fn map_dbg_err_str(self, s: &str) -> Self { + self.map_dbg_err(|_| format!("{}", s)) + } + +} + +impl DebugResult for Result { + + fn map_dbg String>(self, f: F) -> Self { + self.map(|t| { debug!("{}", f(&t)); t }) + } + + fn map_dbg_err String>(self, f: F) -> Self { + self.map_err(|e| { debug!("{}", f(&e)); e }) + } + +} + diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 730970e8..0eafd4ef 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -17,6 +17,7 @@ #[macro_use] extern crate log; extern crate regex; +pub mod debug_result; pub mod ismatch; pub mod iter; pub mod key_value_split;