Skip to content

Commit

Permalink
Merge pull request #12 from CanalTP/more-shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean SIMARD authored Sep 21, 2021
2 parents dc12347 + 4d1920b commit c280861
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skip_error"
version = "3.0.0"
version = "3.1.0"
license = "MIT"
authors = ["Kisio Digital <[email protected]>"]
description = "Utility helping skip and log Result::Error in iterations"
Expand Down
65 changes: 65 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();\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<I, T, E> {
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<I, T, E>: Sized
Expand Down Expand Up @@ -357,6 +389,39 @@ where
fn skip_error_and_log<L>(self, trace_level: L) -> SkipErrorIter<I, T, E>
where
L: Into<tracing::Level>;

#[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<I, T, E> SkipError<I, T, E> for I
Expand Down

0 comments on commit c280861

Please sign in to comment.