diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-11-08 15:50:58 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2018-11-09 21:48:26 +0100 |
commit | 2cb46597f72cf1e733896f887dbe77dc6ff948e9 (patch) | |
tree | 17af5bfb711bd83c5b7420971227bbe48787e337 /lib/etc | |
parent | c767e6239ed7e21fd71b712716d0fdbcd23de645 (diff) |
Add Option extensions for logging optional values
This patch adds the same extensions that we already have for
Result<T, E> also for Option<T>.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'lib/etc')
-rw-r--r-- | lib/etc/libimagutil/src/debug_option.rs | 28 | ||||
-rw-r--r-- | lib/etc/libimagutil/src/info_option.rs | 28 | ||||
-rw-r--r-- | lib/etc/libimagutil/src/lib.rs | 3 | ||||
-rw-r--r-- | lib/etc/libimagutil/src/log_option.rs | 88 |
4 files changed, 147 insertions, 0 deletions
diff --git a/lib/etc/libimagutil/src/debug_option.rs b/lib/etc/libimagutil/src/debug_option.rs new file mode 100644 index 00000000..b8ae47fc --- /dev/null +++ b/lib/etc/libimagutil/src/debug_option.rs @@ -0,0 +1,28 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +// Generates a extension for the `Option<T>`, named `DebugOption` which has functionality to +// print `T` via `debug!()`. +generate_option_logging_extension!( + DebugOption, + map_dbg, + map_dbg_str, + |s| { debug!("{}", s); } +); + diff --git a/lib/etc/libimagutil/src/info_option.rs b/lib/etc/libimagutil/src/info_option.rs new file mode 100644 index 00000000..03800af0 --- /dev/null +++ b/lib/etc/libimagutil/src/info_option.rs @@ -0,0 +1,28 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +// Generates a extension for the `Option<T>`, named `DebugOption` which has functionality to +// print `T` via `info!()`. +generate_option_logging_extension!( + InfoResult, + map_info, + map_info_str, + |s| { info!("{}", s); } +); + diff --git a/lib/etc/libimagutil/src/lib.rs b/lib/etc/libimagutil/src/lib.rs index d9b33040..19bc795a 100644 --- a/lib/etc/libimagutil/src/lib.rs +++ b/lib/etc/libimagutil/src/lib.rs @@ -42,11 +42,14 @@ extern crate tempfile; extern crate chrono; #[macro_use] mod log_result; +#[macro_use] mod log_option; pub mod cli_validators; pub mod date; pub mod debug_result; +pub mod debug_option; pub mod edit; pub mod info_result; +pub mod info_option; pub mod key_value_split; pub mod variants; pub mod warn_exit; diff --git a/lib/etc/libimagutil/src/log_option.rs b/lib/etc/libimagutil/src/log_option.rs new file mode 100644 index 00000000..813eeefc --- /dev/null +++ b/lib/etc/libimagutil/src/log_option.rs @@ -0,0 +1,88 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + +/// This macro is used to generate extensions for the `Option<T>` type which only have +/// sideeffects. +/// +/// This macro is then used to generate debug/info/log/warning/etc extensions. +/// +/// It is exported, so other crates can use it to generate more specific extensions for +/// `Option<T>` types +/// +/// # Parameters +/// +/// The documentation for the parameters of the macro follow. +/// +/// ## `$name` +/// +/// name of the trait to generate +/// +/// ## `$map_name` +/// +/// Name of the function which is generated to call the closure with. +/// +/// This function gets `&T` from `Option<T>` and can now build the argument for +/// `$closure`. So, this function can, for example, `|e| format!("Look here: {:?}", e)`, the +/// Option gets fed to `$closure`. +/// +/// ## `$map_str_name` +/// +/// Name of the function which is generated to call the closure with. +/// +/// This function gets simply a `&str` which gets fed to the `$closure` later. +/// So it can be used to `foo().$map_str_name("Something happened")` +/// +/// ## `$closure` +/// +/// The closure which should be called when mapping. +/// +/// This closure can now do things, but the return value of the closure is discarded. +/// So, this closure can be used for its sideeffects (logging for example) only. +/// +/// An example would be: `|element| debug!("Element: {:?}", element)`. +/// +#[macro_export] +macro_rules! generate_option_logging_extension { + { + $name: ident, + $map_name: ident, + $map_str_name: ident, + $closure: expr + } => { + pub trait $name<T> : 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)) + } + + } + + impl<T> $name<T> for Option<T> { + + fn $map_name<F: FnOnce(&T) -> String>(self, f: F) -> Self { + self.map(|x| { $closure(f(&x)); x }) + } + + } + + } +} + |