diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-07-30 11:57:42 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-07-30 11:57:42 +0200 |
commit | d050be4cbcd8ea1970c30e6295f00afc89890d6d (patch) | |
tree | e79c22e51b6b8912c7e1027955312b25e8ecfa2c /libimagutil | |
parent | deb1544bc566b3117a97b72ee2d98efdbba58616 (diff) |
Refactor, generate code with macro
Diffstat (limited to 'libimagutil')
-rw-r--r-- | libimagutil/src/debug_result.rs | 36 | ||||
-rw-r--r-- | libimagutil/src/info_result.rs | 36 | ||||
-rw-r--r-- | libimagutil/src/lib.rs | 1 | ||||
-rw-r--r-- | libimagutil/src/log_result.rs | 40 |
4 files changed, 57 insertions, 56 deletions
diff --git a/libimagutil/src/debug_result.rs b/libimagutil/src/debug_result.rs index d01a2c8e..76ed1085 100644 --- a/libimagutil/src/debug_result.rs +++ b/libimagutil/src/debug_result.rs @@ -1,28 +1,8 @@ -pub trait DebugResult<T, E> : Sized { - - fn map_dbg<F: FnOnce(&T) -> String>(self, f: F) -> Self; - - fn map_dbg_str(self, s: &str) -> Self { - self.map_dbg(|_| format!("{}", s)) - } - - fn map_dbg_err<F: FnOnce(&E) -> String>(self, f: F) -> Self; - - fn map_dbg_err_str(self, s: &str) -> Self { - self.map_dbg_err(|_| format!("{}", s)) - } - -} - -impl<T, E> DebugResult<T, E> for Result<T, E> { - - fn map_dbg<F: FnOnce(&T) -> String>(self, f: F) -> Self { - self.map(|t| { debug!("{}", f(&t)); t }) - } - - fn map_dbg_err<F: FnOnce(&E) -> String>(self, f: F) -> Self { - self.map_err(|e| { debug!("{}", f(&e)); e }) - } - -} - +generate_result_logging_extension!( + DebugResult, + map_dbg, + map_dbg_str, + map_dbg_err, + map_dbg_err_str, + |s| { debug!("{}", s); } +); diff --git a/libimagutil/src/info_result.rs b/libimagutil/src/info_result.rs index 0c40612b..6b036e31 100644 --- a/libimagutil/src/info_result.rs +++ b/libimagutil/src/info_result.rs @@ -1,28 +1,8 @@ -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 }) - } - -} - +generate_result_logging_extension!( + InfoResult, + map_info, + map_info_str, + map_info_err, + map_info_err_str, + |s| { info!("{}", s); } +); diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index a05d14e8..58b9271a 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -17,6 +17,7 @@ #[macro_use] extern crate log; extern crate regex; +#[macro_use] mod log_result; pub mod debug_result; pub mod info_result; pub mod ismatch; diff --git a/libimagutil/src/log_result.rs b/libimagutil/src/log_result.rs new file mode 100644 index 00000000..fa69ac40 --- /dev/null +++ b/libimagutil/src/log_result.rs @@ -0,0 +1,40 @@ +#[macro_export] +macro_rules! generate_result_logging_extension { + { + $name: ident, + $map_name: ident, + $map_str_name: ident, + $map_err_name: ident, + $map_err_str_name: ident, + $closure: expr + } => { + pub trait InfoResult<T, E> : Sized { + + fn $map_name<F: FnOnce(&T) -> String>(self, f: F) -> Self; + + fn $map_str_name(self, s: &str) -> Self { + self.$map_name(|_| format!("{}", s)) + } + + fn $map_err_name<F: FnOnce(&E) -> String>(self, f: F) -> Self; + + fn $map_err_str_name(self, s: &str) -> Self { + self.$map_err_name(|_| format!("{}", s)) + } + + } + + impl<T, E> InfoResult<T, E> for Result<T, E> { + + fn $map_name<F: FnOnce(&T) -> String>(self, f: F) -> Self { + self.map(|x| { $closure(f(&x)); x }) + } + + fn $map_err_name<F: FnOnce(&E) -> String>(self, f: F) -> Self { + self.map_err(|e| { $closure(f(&e)); e }) + } + + } + + } +} |