#![allow(unused_variables)]
use defmt::Format;
mod sealed {
pub trait Sealed {}
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait ResultContext<T, E>: sealed::Sealed {
fn error(self, context: impl Format) -> Result<T, E>;
fn warn(self, context: impl Format) -> Result<T, E>;
fn info(self, context: impl Format) -> Result<T, E>;
fn debug(self, context: impl Format) -> Result<T, E>;
fn trace(self, context: impl Format) -> Result<T, E>;
fn with_error<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E>;
fn with_warn<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E>;
fn with_info<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E>;
fn with_debug<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E>;
fn with_trace<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>;
fn consume_with_info<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T>;
fn consume_with_debug<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T>;
fn consume_with_trace<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T>;
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait ConsumeDebug<T>: sealed::Sealed {
fn consume_error(self) -> Option<T>;
fn consume_warn(self) -> Option<T>;
fn consume_info(self) -> Option<T>;
fn consume_debug(self) -> Option<T>;
fn consume_trace(self) -> Option<T>;
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait ConsumeDisplay<T>: sealed::Sealed {
fn consume_error(self) -> Option<T>;
fn consume_warn(self) -> Option<T>;
fn consume_info(self) -> Option<T>;
fn consume_debug(self) -> Option<T>;
fn consume_trace(self) -> Option<T>;
}
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
pub trait OptionContext<T>: sealed::Sealed {
fn error(self, context: impl Format) -> Option<T>;
fn warn(self, context: impl Format) -> Option<T>;
fn info(self, context: impl Format) -> Option<T>;
fn debug(self, context: impl Format) -> Option<T>;
fn trace(self, context: impl Format) -> Option<T>;
fn with_error<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
fn with_warn<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
fn with_info<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
fn with_debug<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
fn with_trace<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T>;
}
impl<T, E> sealed::Sealed for Result<T, E> {}
impl<T, E> ResultContext<T, E> for Result<T, E> {
#[inline]
fn error(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::error!("{}", context);
}
self
}
#[inline]
fn warn(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::warn!("{}", context);
}
self
}
#[inline]
fn info(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::info!("{}", context);
}
self
}
#[inline]
fn debug(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::debug!("{}", context);
}
self
}
#[inline]
fn trace(self, context: impl Format) -> Result<T, E> {
if self.is_err() {
defmt::trace!("{}", context);
}
self
}
#[inline]
fn with_error<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<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
defmt::warn!("{}", f(&err));
}
self
}
#[inline]
fn with_info<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
defmt::info!("{}", f(&err));
}
self
}
#[inline]
fn with_debug<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
defmt::debug!("{}", f(&err));
}
self
}
#[inline]
fn with_trace<F: FnOnce(&E) -> D, D: Format>(self, f: F) -> Result<T, E> {
if let Err(err) = &self {
defmt::trace!("{}", f(&err));
}
self
}
#[inline]
fn consume_with_error<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T> {
match self {
Ok(ok) => Some(ok),
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(ok) => Some(ok),
Err(err) => {
defmt::warn!("{}", f(err));
None
}
}
}
#[inline]
fn consume_with_info<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::info!("{}", f(err));
None
}
}
}
#[inline]
fn consume_with_debug<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::debug!("{}", f(err));
None
}
}
}
#[inline]
fn consume_with_trace<F: FnOnce(E) -> D, D: Format>(self, f: F) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::trace!("{}", f(err));
None
}
}
}
}
impl<T, E> ConsumeDebug<T> for Result<T, E>
where
E: Format,
{
#[inline]
fn consume_error(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::error!("{:?}", err);
None
}
}
}
#[inline]
fn consume_warn(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::warn!("{:?}", err);
None
}
}
}
#[inline]
fn consume_info(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::info!("{:?}", err);
None
}
}
}
#[inline]
fn consume_debug(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::debug!("{:?}", err);
None
}
}
}
#[inline]
fn consume_trace(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::trace!("{:?}", err);
None
}
}
}
}
impl<T, E> ConsumeDisplay<T> for Result<T, E>
where
E: Format,
{
#[inline]
fn consume_error(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::error!("{}", err);
None
}
}
}
#[inline]
fn consume_warn(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::warn!("{}", err);
None
}
}
}
#[inline]
fn consume_info(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::info!("{}", err);
None
}
}
}
#[inline]
fn consume_debug(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::debug!("{}", err);
None
}
}
}
#[inline]
fn consume_trace(self) -> Option<T> {
match self {
Ok(ok) => Some(ok),
Err(err) => {
defmt::trace!("{}", err);
None
}
}
}
}
impl<T> sealed::Sealed for Option<T> {}
impl<T> OptionContext<T> for Option<T> {
#[inline]
fn error(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::error!("{}", context);
}
self
}
#[inline]
fn warn(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::warn!("{}", context);
}
self
}
#[inline]
fn info(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::info!("{}", context);
}
self
}
#[inline]
fn debug(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::debug!("{}", context);
}
self
}
#[inline]
fn trace(self, context: impl Format) -> Option<T> {
if self.is_none() {
defmt::trace!("{}", context);
}
self
}
#[inline]
fn with_error<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::error!("{}", f());
}
self
}
#[inline]
fn with_warn<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::warn!("{}", f());
}
self
}
#[inline]
fn with_info<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::info!("{}", f());
}
self
}
#[inline]
fn with_debug<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::debug!("{}", f());
}
self
}
#[inline]
fn with_trace<F: FnOnce() -> D, D: Format>(self, f: F) -> Option<T> {
if self.is_none() {
defmt::trace!("{}", f());
}
self
}
}