Skip to content

Commit

Permalink
feat: Improve err_trail
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Dec 23, 2024
1 parent 082ffbd commit 8d0f158
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions err_trail/src/tracing_log_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ mod sealed {
)]
pub trait ResultTrail<T, E>: sealed::Sealed {
/// If [Err], logging context as an "error".
fn error_trail(self, context: impl Display) -> Result<T, E>;
fn error_context(self, context: impl Display) -> Result<T, E>;
/// If [Err], logging context as an "warn".
fn warn_trail(self, context: impl Display) -> Result<T, E>;
fn warn_context(self, context: impl Display) -> Result<T, E>;

/// If [Err], lazily logging the result of [f] as an "error".
fn lazy_error_trail<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E>;
fn with_error_context<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E>;
/// If [Err], lazily logging the result of [f] as an "warn".
fn lazy_warn_trail<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E>;
fn with_warn_context<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E>;
}

/// For logging a [Option] when [None] is encountered.
Expand All @@ -40,14 +40,14 @@ pub trait ResultTrail<T, E>: sealed::Sealed {
)]
pub trait OptionTrail<T>: sealed::Sealed {
/// If [None], logging context as an "error".
fn error_trail(self, context: impl Display) -> Option<T>;
fn error_context(self, context: impl Display) -> Option<T>;
/// If [None], logging context as an "warn".
fn warn_trail(self, context: impl Display) -> Option<T>;
fn warn_context(self, context: impl Display) -> Option<T>;

/// Consumes the [Option]. If [None], lazily logging the result of [f] as an "error".
fn lazy_error_trail<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T>;
fn with_error_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T>;
/// Consumes the [Option]. If [None], lazily logging the result of [f] as an "warn".
fn lazy_warn_trail<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T>;
fn with_warn_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T>;
}

/// For logging a [Result]'s [Err] in the [Display] format when an [Err] is encountered.
Expand All @@ -63,18 +63,18 @@ pub trait OptionTrail<T>: sealed::Sealed {
pub trait ResultTrailDisplay<T, E: Display>: sealed::Sealed {
/// Consumes the [Err] of a Result. If [Err], logging the display of the error as an "error".
/// Represents a bad state in which the current process cannot continue.
fn error_trail_end(self) -> Option<T>;
fn error_context_end(self) -> Option<T>;
/// Consumes the [Err] of a Result. If [Err], logging the display of the error as an "warn".
/// Represents a bad state in which the current process can continue.
fn warn_trail_end(self) -> Option<T>;
fn warn_context_end(self) -> Option<T>;
}

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

impl<T, E> sealed::Sealed for Result<T, E> {}
impl<T, E> ResultTrail<T, E> for Result<T, E> {
#[inline]
fn error_trail(self, context: impl Display) -> Result<T, E> {
fn error_context(self, context: impl Display) -> Result<T, E> {
if self.is_err() {
#[cfg(feature = "tracing")]
tracing::error!("{}", context);
Expand All @@ -85,7 +85,7 @@ impl<T, E> ResultTrail<T, E> for Result<T, E> {
}

#[inline]
fn warn_trail(self, context: impl Display) -> Result<T, E> {
fn warn_context(self, context: impl Display) -> Result<T, E> {
if self.is_err() {
#[cfg(feature = "tracing")]
tracing::warn!("{}", context);
Expand All @@ -98,7 +98,7 @@ impl<T, E> ResultTrail<T, E> for Result<T, E> {
//************************************************************************//

#[inline]
fn lazy_error_trail<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E> {
fn with_error_context<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
#[cfg(feature = "tracing")]
tracing::error!("{}", f(&err));
Expand All @@ -109,7 +109,7 @@ impl<T, E> ResultTrail<T, E> for Result<T, E> {
}

#[inline]
fn lazy_warn_trail<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E> {
fn with_warn_context<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
#[cfg(feature = "tracing")]
tracing::warn!("{}", f(&err));
Expand All @@ -124,7 +124,7 @@ impl<T, E> ResultTrail<T, E> for Result<T, E> {

impl<T, E: Display> ResultTrailDisplay<T, E> for Result<T, E> {
#[inline]
fn error_trail_end(self) -> Option<T> {
fn error_context_end(self) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
Expand All @@ -138,7 +138,7 @@ impl<T, E: Display> ResultTrailDisplay<T, E> for Result<T, E> {
}

#[inline]
fn warn_trail_end(self) -> Option<T> {
fn warn_context_end(self) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
Expand All @@ -157,7 +157,7 @@ impl<T, E: Display> ResultTrailDisplay<T, E> for Result<T, E> {
impl<T> sealed::Sealed for Option<T> {}
impl<T> OptionTrail<T> for Option<T> {
#[inline]
fn error_trail(self, context: impl Display) -> Option<T> {
fn error_context(self, context: impl Display) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::error!("{}", context);
Expand All @@ -168,7 +168,7 @@ impl<T> OptionTrail<T> for Option<T> {
}

#[inline]
fn warn_trail(self, context: impl Display) -> Option<T> {
fn warn_context(self, context: impl Display) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::warn!("{}", context);
Expand All @@ -181,7 +181,7 @@ impl<T> OptionTrail<T> for Option<T> {
//************************************************************************//

#[inline]
fn lazy_error_trail<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T> {
fn with_error_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::error!("{}", f());
Expand All @@ -192,7 +192,7 @@ impl<T> OptionTrail<T> for Option<T> {
}

#[inline]
fn lazy_warn_trail<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T> {
fn with_warn_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::warn!("{}", f());
Expand Down

0 comments on commit 8d0f158

Please sign in to comment.