#![allow(unused_variables)]
use defmt::Format;
mod sealed {
pub trait Sealed {}
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait ErrContextDefmt<T, E>: sealed::Sealed {
fn error_context(self, context: impl Format) -> Result<T, E>;
fn warn_context(self, context: impl Format) -> Result<T, E>;
fn with_error_context<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E>;
fn with_warn_context<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E>;
fn consume_with_error<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Option<T>;
fn consume_with_warn<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Option<T>;
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait ErrContextDisplayDefmt<T, E: Format>: sealed::Sealed {
fn consume_as_error(self) -> Option<T>;
fn consume_as_warn(self) -> Option<T>;
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait NoneContextDefmt<T>: sealed::Sealed {
fn error_context(self, context: impl Format) -> Option<T>;
fn warn_context(self, context: impl Format) -> Option<T>;
fn with_error_context<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
fn with_warn_context<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
}
impl<T, E> sealed::Sealed for Result<T, E> {}
impl<T, E> ErrContextDefmt<T, E> for Result<T, E> {
#[inline]
fn error_context(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::error!("{}", context);
}
self
}
#[inline]
fn warn_context(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::warn!("{}", context);
}
self
}
#[inline]
fn with_error_context<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
defmt::error!("{}", f(err));
}
self
}
#[inline]
fn with_warn_context<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
defmt::warn!("{}", f(err));
}
self
}
#[inline]
fn consume_with_error<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
defmt::error!("{}", f(&err));
None
}
}
}
#[inline]
fn consume_with_warn<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(err) => {
defmt::warn!("{}", f(&err));
None
}
}
}
}
impl<T, E: Format> ErrContextDisplayDefmt<T, E> for Result<T, E> {
#[inline]
fn consume_as_error(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::error!("{}", err);
None
}
}
}
#[inline]
fn consume_as_warn(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::warn!("{}", err);
None
}
}
}
}
impl<T> sealed::Sealed for Option<T> {}
impl<T> NoneContextDefmt<T> for Option<T> {
#[inline]
fn error_context(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::error!("{}", context);
}
self
}
#[inline]
fn warn_context(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::warn!("{}", context);
}
self
}
#[inline]
fn with_error_context<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::error!("{}", f());
}
self
}
#[inline]
fn with_warn_context<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::warn!("{}", f());
}
self
}
}