diff --git a/Cargo.toml b/Cargo.toml index 9d84342..73db19a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "skip_error" -version = "3.0.0" +version = "3.1.0" license = "MIT" authors = ["Kisio Digital "] description = "Utility helping skip and log Result::Error in iterations" diff --git a/src/lib.rs b/src/lib.rs index 92aa442..609eaa5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -272,6 +272,38 @@ where } } +#[cfg(any(feature = "log", feature = "tracing"))] +macro_rules! default_impl_skip_error_iterator { + ($method_name:ident, $log_level:expr) => { + default_impl_skip_error_iterator!($method_name, $log_level, $log_level); + }; + ($method_name:ident, $log_level:expr, $expected_log_level:expr) => { + #[doc = concat!( + "Shortcut for [`SkipError::skip_error_and_log`] with a log level of [`", + stringify!($log_level), + "`].\n\n", + "For example\n", + "```edition2018\n", + "use skip_error::SkipError;\n", + "# fn main() {\n", + "# testing_logger::setup();\n", + "vec![Ok(1), Ok(2), Err(\"'three' is not a valid number\"), Ok(4)]\n", + " .into_iter()\n", + " .", stringify!($method_name), "()\n", + " .collect::>();\n", + "testing_logger::validate(|captured_logs| {\n", + " assert!(captured_logs[0].body.contains(\"'three' is not a valid number\"));\n", + " assert_eq!(captured_logs[0].level, ", stringify!($expected_log_level), ");\n", + "});\n", + "# }\n", + "```\n" + )] + fn $method_name(self) -> SkipErrorIter { + self.skip_error_and_log($log_level) + } + }; +} + /// Trait to extend any [`Iterator`] where the [`Iterator::Item`] is a [`Result`]. /// This allows to skip errors and keep only the `Ok()` values. pub trait SkipError: Sized @@ -357,6 +389,39 @@ where fn skip_error_and_log(self, trace_level: L) -> SkipErrorIter where L: Into; + + #[cfg(all(feature = "log", not(feature = "tracing")))] + default_impl_skip_error_iterator!(skip_error_and_trace, log::Level::Trace); + #[cfg(all(feature = "log", not(feature = "tracing")))] + default_impl_skip_error_iterator!(skip_error_and_debug, log::Level::Debug); + #[cfg(all(feature = "log", not(feature = "tracing")))] + default_impl_skip_error_iterator!(skip_error_and_error, log::Level::Error); + #[cfg(all(feature = "log", not(feature = "tracing")))] + default_impl_skip_error_iterator!(skip_error_and_warn, log::Level::Warn); + #[cfg(all(feature = "log", not(feature = "tracing")))] + default_impl_skip_error_iterator!(skip_error_and_info, log::Level::Info); + #[cfg(feature = "tracing")] + default_impl_skip_error_iterator!( + skip_error_and_trace, + tracing::Level::TRACE, + log::Level::Trace + ); + #[cfg(feature = "tracing")] + default_impl_skip_error_iterator!( + skip_error_and_debug, + tracing::Level::DEBUG, + log::Level::Debug + ); + #[cfg(feature = "tracing")] + default_impl_skip_error_iterator!( + skip_error_and_error, + tracing::Level::ERROR, + log::Level::Error + ); + #[cfg(feature = "tracing")] + default_impl_skip_error_iterator!(skip_error_and_warn, tracing::Level::WARN, log::Level::Warn); + #[cfg(feature = "tracing")] + default_impl_skip_error_iterator!(skip_error_and_info, tracing::Level::INFO, log::Level::Info); } impl SkipError for I