#![allow(unused_variables)]
use core::fmt::Display;
mod sealed {
pub trait Sealed {}
}
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "tracing", feature = "log", feature = "stub")))
)]
pub trait ErrContext<T, E>: sealed::Sealed {
fn error_context(self, context: impl Display) -> Result<T, E>;
fn warn_context(self, context: impl Display) -> Result<T, E>;
fn with_error_context<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>;
fn consume_with_error<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Option<T>;
fn consume_with_warn<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Option<T>;
}
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "tracing", feature = "log", feature = "stub")))
)]
pub trait ErrContextDisplay<T,E: Display>: ErrContext<T,E> + sealed::Sealed {
fn consume_as_error(self) -> Option<T>;
fn consume_as_warn(self) -> Option<T>;
}
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "tracing", feature = "log", feature = "stub")))
)]
pub trait NoneContext<T>: sealed::Sealed {
fn error_context(self, context: impl Display) -> Option<T>;
fn warn_context(self, context: impl Display) -> Option<T>;
fn with_error_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T>;
fn with_warn_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T>;
}
impl<T, E> sealed::Sealed for Result<T, E> {}
impl<T, E> ErrContext<T, E> for Result<T, E> {
#[inline]
fn error_context(self, context: impl Display) -> Result<T, E> {
if self.is_err() {
#[cfg(feature = "tracing")]
tracing::error!("{}", context);
#[cfg(feature = "log")]
log::error!("{}", context);
}
self
}
#[inline]
fn warn_context(self, context: impl Display) -> Result<T, E> {
if self.is_err() {
#[cfg(feature = "tracing")]
tracing::warn!("{}", context);
#[cfg(feature = "log")]
log::warn!("{}", context);
}
self
}
#[inline]
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));
#[cfg(feature = "log")]
log::error!("{}", f(&err));
}
self
}
#[inline]
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));
#[cfg(feature = "log")]
log::warn!("{}", f(&err));
}
self
}
#[inline]
fn consume_with_error<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
#[cfg(feature = "tracing")]
tracing::error!("{}", f(&err));
#[cfg(feature = "log")]
log::error!("{}", f(&err));
None
}
}
}
#[inline]
fn consume_with_warn<F: FnOnce(&E) -> D, D: Display>(self, f: F) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
#[cfg(feature = "tracing")]
tracing::warn!("{}", f(&err));
#[cfg(feature = "log")]
log::warn!("{}", f(&err));
None
}
}
}
}
impl<T, E: Display> ErrContextDisplay<T, E> for Result<T, E> {
#[inline]
fn consume_as_error(self) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
#[cfg(feature = "tracing")]
tracing::error!("{}", err);
#[cfg(feature = "log")]
log::error!("{}", err);
None
}
}
}
#[inline]
fn consume_as_warn(self) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
#[cfg(feature = "tracing")]
tracing::warn!("{}", err);
#[cfg(feature = "log")]
log::warn!("{}", err);
None
}
}
}
}
impl<T> sealed::Sealed for Option<T> {}
impl<T> NoneContext<T> for Option<T> {
#[inline]
fn error_context(self, context: impl Display) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::error!("{}", context);
#[cfg(feature = "log")]
log::error!("{}", context);
}
self
}
#[inline]
fn warn_context(self, context: impl Display) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::warn!("{}", context);
#[cfg(feature = "log")]
log::warn!("{}", context);
}
self
}
#[inline]
fn with_error_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::error!("{}", f());
#[cfg(feature = "log")]
log::error!("{}", f());
}
self
}
#[inline]
fn with_warn_context<F: FnOnce() -> D, D: Display>(self, f: F) -> Option<T> {
if self.is_none() {
#[cfg(feature = "tracing")]
tracing::warn!("{}", f());
#[cfg(feature = "log")]
log::warn!("{}", f());
}
self
}
}