Skip to content

Commit

Permalink
feat: Remove swallow methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Sep 14, 2024
1 parent bee5f4f commit 694b103
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 277 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,11 @@ Enables support for the `tracing` or `log` or `defmt` crates. Methods are added
```rust
let result: Result<(), &str> = Err("operation failed");

let value: Result<(), &str> = result.error("This is an error logged via tracing/log if `Err`");
let value: Result<(), &str> = result.warn("This is a warning logged via tracing/log if `Err`");
let value: () = result.error("This is an error logged via tracing/log if `Err`")?;
let value: Result<(), &str> = result.with_trace(|err| format!("Operation failed due to: {}", err));
let value: Option<()> = result.consume_warn();
let value: Option<()> = result.consume_with_error(|err| format!("Operation failed due to: {}", err));
result.swallow_info();
result.swallow_with_debug(|err| format!("Debug info: {:?}", err));
let value: Result<(), &str> = result.with_debug(|err| format!("Operation failed due to: {}", err));
let value: Option<()> = result.consume_info();
let value: Option<()> = result.consume_with_trace(|err| format!("Operation failed due to: {}", err));
```
> Note: a `context_stub` feature flag also exists to be used by libraries. This allows the api's to be used in libraries
> while a downstream binrary will ultimately decide the implementation.
Expand Down
126 changes: 0 additions & 126 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ pub trait ResultContext<T, E> {
fn consume_with_debug<F: FnOnce(E) -> D, D: Display>(self, f: F) -> Option<T>;
/// Consumes the [Err] of a Result. if [Err], logging as an "trace" with the result of [f].
fn consume_with_trace<F: FnOnce(E) -> D, D: Display>(self, f: F) -> Option<T>;

/// Swallows the Result. if [Err], logging as an "error" with the result of [f].
fn swallow_with_error<F: FnOnce(E) -> D, D: Display>(self, f: F);
/// Swallows the Result. if [Err], logging as an "warn" with the result of [f].
fn swallow_with_warn<F: FnOnce(E) -> D, D: Display>(self, f: F);
/// Swallows the Result. if [Err], logging as an "info" with the result of [f].
fn swallow_with_info<F: FnOnce(E) -> D, D: Display>(self, f: F);
/// Swallows the Result. if [Err], logging as an "debug" with the result of [f].
fn swallow_with_debug<F: FnOnce(E) -> D, D: Display>(self, f: F);
/// Swallows the Result. if [Err], logging as an "trace" with the result of [f].
fn swallow_with_trace<F: FnOnce(E) -> D, D: Display>(self, f: F);
}

/// For logging a [Result] when an [Err] is encountered and [Result] implements [Debug].
Expand All @@ -64,17 +53,6 @@ pub trait ResultContextDebug<T, E>: ResultContext<T, E> {
fn consume_debug(self) -> Option<T>;
/// Consumes the [Err] of a Result. if [Err], logging as an "trace".
fn consume_trace(self) -> Option<T>;

/// Swallows the Result. if [Err], logging as an "error".
fn swallow_error(self);
/// Swallows the Result. if [Err], logging as an "warn".
fn swallow_warn(self);
/// Swallows the Result. if [Err], logging as an "info".
fn swallow_info(self);
/// Swallows the Result. if [Err], logging as an "debug".
fn swallow_debug(self);
/// Swallows the Result. if [Err], logging as an "trace".
fn swallow_trace(self);
}

/// For logging when a [None] is encountered.
Expand Down Expand Up @@ -287,58 +265,6 @@ impl<T, E> ResultContext<T,E> for Result<T, E> {
}
}
}

//************************************************************************//

#[inline]
fn swallow_with_error<F: FnOnce(E) -> D, D: Display>(self, f: F) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::error!("{}", f(err));
#[cfg(feature = "log")]
log::error!("{}", f(err));
}
}

#[inline]
fn swallow_with_warn<F: FnOnce(E) -> D, D: Display>(self, f: F) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::warn!("{}", f(err));
#[cfg(feature = "log")]
log::warn!("{}", f(err));
}
}

#[inline]
fn swallow_with_info<F: FnOnce(E) -> D, D: Display>(self, f: F) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::info!("{}", f(err));
#[cfg(feature = "log")]
log::info!("{}", f(err));
}
}

#[inline]
fn swallow_with_debug<F: FnOnce(E) -> D, D: Display>(self, f: F) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::debug!("{}", f(err));
#[cfg(feature = "log")]
log::debug!("{}", f(err));
}
}

#[inline]
fn swallow_with_trace<F: FnOnce(E) -> D, D: Display>(self, f: F) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::trace!("{}", f(err));
#[cfg(feature = "log")]
log::trace!("{}", f(err));
}
}
}

impl<T, E> ResultContextDebug<T,E> for Result<T, E>
Expand Down Expand Up @@ -414,58 +340,6 @@ where
}
}
}

//************************************************************************//

#[inline]
fn swallow_error(self) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::error!("{:?}", err);
#[cfg(feature = "log")]
log::error!("{:?}", err);
}
}

#[inline]
fn swallow_warn(self) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::warn!("{:?}", err);
#[cfg(feature = "log")]
log::warn!("{:?}", err);
}
}

#[inline]
fn swallow_info(self) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::info!("{:?}", err);
#[cfg(feature = "log")]
log::info!("{:?}", err);
}
}

#[inline]
fn swallow_debug(self) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::debug!("{:?}", err);
#[cfg(feature = "log")]
log::debug!("{:?}", err);
}
}

#[inline]
fn swallow_trace(self) {
if let Err(err) = self {
#[cfg(feature = "tracing")]
tracing::trace!("{:?}", err);
#[cfg(feature = "log")]
log::trace!("{:?}", err);
}
}
}

//************************************************************************//
Expand Down
96 changes: 0 additions & 96 deletions src/defmt_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ pub trait ResultContext<T, E> {
fn consume_with_debug<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T>;
/// Consumes the [Err] of a Result. if [Err], logging as an "trace" with the result of [f].
fn consume_with_trace<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T>;

/// Swallows the Result. if [Err], logging as an "error" with the result of [f].
fn swallow_with_error<F: FnOnce(E) -> D, D: Format>(self, f: F);
/// Swallows the Result. if [Err], logging as an "warn" with the result of [f].
fn swallow_with_warn<F: FnOnce(E) -> D, D: Format>(self, f: F);
/// Swallows the Result. if [Err], logging as an "info" with the result of [f].
fn swallow_with_info<F: FnOnce(E) -> D, D: Format>(self, f: F);
/// Swallows the Result. if [Err], logging as an "debug" with the result of [f].
fn swallow_with_debug<F: FnOnce(E) -> D, D: Format>(self, f: F);
/// Swallows the Result. if [Err], logging as an "trace" with the result of [f].
fn swallow_with_trace<F: FnOnce(E) -> D, D: Format>(self, f: F);
}

/// For logging a [Result] when an [Err] is encountered and [Result] implements [Debug].
Expand All @@ -61,17 +50,6 @@ pub trait ResultContextDebug<T, E>: ResultContext<T, E> {
fn consume_debug(self) -> Option<T>;
/// Consumes the [Err] of a Result. if [Err], logging as an "trace".
fn consume_trace(self) -> Option<T>;

/// Swallows the Result. if [Err], logging as an "error".
fn swallow_error(self);
/// Swallows the Result. if [Err], logging as an "warn".
fn swallow_warn(self);
/// Swallows the Result. if [Err], logging as an "info".
fn swallow_info(self);
/// Swallows the Result. if [Err], logging as an "debug".
fn swallow_debug(self);
/// Swallows the Result. if [Err], logging as an "trace".
fn swallow_trace(self);
}

/// For logging when a [None] is encountered.
Expand Down Expand Up @@ -243,43 +221,6 @@ impl<T, E> ResultContext<T, E> for Result<T, E> {
}
}
}

//************************************************************************//

#[inline]
fn swallow_with_error<F: FnOnce(E) -> D, D: Format>(self, f: F) {
if let Err(err) = self {
defmt::error!("{}", f(err));
}
}

#[inline]
fn swallow_with_warn<F: FnOnce(E) -> D, D: Format>(self, f: F) {
if let Err(err) = self {
defmt::warn!("{}", f(err));
}
}

#[inline]
fn swallow_with_info<F: FnOnce(E) -> D, D: Format>(self, f: F) {
if let Err(err) = self {
defmt::info!("{}", f(err));
}
}

#[inline]
fn swallow_with_debug<F: FnOnce(E) -> D, D: Format>(self, f: F) {
if let Err(err) = self {
defmt::debug!("{}", f(err));
}
}

#[inline]
fn swallow_with_trace<F: FnOnce(E) -> D, D: Format>(self, f: F) {
if let Err(err) = self {
defmt::trace!("{}", f(err));
}
}
}

impl<T, E> ResultContextDebug<T, E> for Result<T, E>
Expand Down Expand Up @@ -345,43 +286,6 @@ where
}
}
}

//************************************************************************//

#[inline]
fn swallow_error(self) {
if let Err(err) = self {
defmt::error!("{:?}", err);
}
}

#[inline]
fn swallow_warn(self) {
if let Err(err) = self {
defmt::warn!("{:?}", err);
}
}

#[inline]
fn swallow_info(self) {
if let Err(err) = self {
defmt::info!("{:?}", err);
}
}

#[inline]
fn swallow_debug(self) {
if let Err(err) = self {
defmt::debug!("{:?}", err);
}
}

#[inline]
fn swallow_trace(self) {
if let Err(err) = self {
defmt::trace!("{:?}", err);
}
}
}

//************************************************************************//
Expand Down
49 changes: 0 additions & 49 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,6 @@ mod tracing {

// todo implement consume_with tests

// todo implement swallow_with tests

//************************************************************************//

#[traced_test]
Expand Down Expand Up @@ -588,53 +586,6 @@ mod tracing {

assert!(logs_contain("trace consumed"));
}

//************************************************************************//

#[traced_test]
#[test]
fn test_log_swallow_error() {
let result: Result<(), &str> = Err("error swallowed");
let _ = result.swallow_error();

assert!(logs_contain("error swallowed"));
}

#[traced_test]
#[test]
fn test_log_swallow_warn() {
let result: Result<(), &str> = Err("warning swallowed");
let _ = result.swallow_warn();

assert!(logs_contain("warning swallowed"));
}

#[traced_test]
#[test]
fn test_log_swallow_info() {
let result: Result<(), &str> = Err("info swallowed");
let _ = result.swallow_info();

assert!(logs_contain("info swallowed"));
}

#[traced_test]
#[test]
fn test_log_swallow_debug() {
let result: Result<(), &str> = Err("debug swallowed");
let _ = result.swallow_debug();

assert!(logs_contain("debug swallowed"));
}

#[traced_test]
#[test]
fn test_log_swallow_trace() {
let result: Result<(), &str> = Err("trace swallowed");
let _ = result.swallow_trace();

assert!(logs_contain("trace swallowed"));
}
}

#[cfg(feature = "log")]
Expand Down

0 comments on commit 694b103

Please sign in to comment.