Merge pull request #594 from matthiasbeyer/libimagutil/debug-mapping

Add utility to print debugging information while mapping
This commit is contained in:
Matthias Beyer 2016-07-30 15:28:45 +02:00 committed by GitHub
commit 6a8c93ee52
4 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,8 @@
generate_result_logging_extension!(
DebugResult,
map_dbg,
map_dbg_str,
map_dbg_err,
map_dbg_err_str,
|s| { debug!("{}", s); }
);

View file

@ -0,0 +1,8 @@
generate_result_logging_extension!(
InfoResult,
map_info,
map_info_str,
map_info_err,
map_info_err_str,
|s| { info!("{}", s); }
);

View file

@ -17,6 +17,9 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate regex; extern crate regex;
#[macro_use] mod log_result;
pub mod debug_result;
pub mod info_result;
pub mod ismatch; pub mod ismatch;
pub mod iter; pub mod iter;
pub mod key_value_split; pub mod key_value_split;

View file

@ -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 })
}
}
}
}