error_tools 0.27.0

Basic exceptions handling mechanism
Documentation
//! Core error handling utilities.

/// Assertions.
#[cfg(feature = "enabled")]
pub mod assert;

#[cfg(feature = "enabled")]
#[cfg(feature = "error_typed")]
/// Typed error handling, a facade for `thiserror`.
pub mod typed;

#[cfg(feature = "enabled")]
#[cfg(feature = "error_untyped")]
/// Untyped error handling, a facade for `anyhow`.
pub mod untyped;

/// Define a private namespace for all its items.
mod private {
  pub use core::error::Error as ErrorTrait;
  /// Trait to add extra context or information to an error.
  pub trait ErrWith<ReportErr, ReportOk, E> {
    /// Wraps an error with additional context generated by a closure.
    /// # Errors
    /// Returns `Err` if the original `Result` is `Err`.
    fn err_with<F>(self, f: F) -> core::result::Result<ReportOk, (ReportErr, E)>
    where
      F: FnOnce() -> ReportErr;
    /// Wraps an error with additional context provided by a reference.
    /// # Errors
    /// Returns `Err` if the original `Result` is `Err`.
    fn err_with_report(self, report: &ReportErr) -> core::result::Result<ReportOk, (ReportErr, E)>
    where
      ReportErr: Clone;
  }
  impl<ReportErr, ReportOk, E, IntoError> ErrWith<ReportErr, ReportOk, E> for core::result::Result<ReportOk, IntoError>
  where
    IntoError: Into<E>,
  {
    #[inline]
    /// Wraps an error with additional context generated by a closure.
    fn err_with<F>(self, f: F) -> core::result::Result<ReportOk, (ReportErr, E)>
    where
      F: FnOnce() -> ReportErr,
    {
      self.map_err(|error| (f(), error.into()))
    }
    #[inline(always)]
    /// Wraps an error with additional context provided by a reference.
    fn err_with_report(self, report: &ReportErr) -> core::result::Result<ReportOk, (ReportErr, E)>
    where
      ReportErr: Clone,
      Self: Sized,
    {
      self.map_err(|error| (report.clone(), error.into()))
    }
  }
  /// A type alias for a `Result` that contains an error which is a tuple of a report and an original error.
  pub type ResultWithReport<Report, Error> = Result<Report, (Report, Error)>;
}

#[cfg(feature = "enabled")]
pub use private::{ErrWith, ResultWithReport, ErrorTrait};

#[cfg(feature = "enabled")]
pub use assert::*;