ts-error 0.4.0

Error models and reporting for my projects
Documentation
//! Extension traits to log errors.

/// Trait to log an error.
#[cfg(feature = "log")]
pub trait LogError {
    /// Log the error.
    #[track_caller]
    fn log_err(self) -> Self;
}

#[cfg(feature = "std")]
/// Trait to log an error to `stderr`.
pub trait StderrError {
    /// Display the error in `stderr`.
    #[track_caller]
    fn stderr_err(self) -> Self;
}

#[cfg(feature = "log")]
impl<T, E: core::fmt::Display> LogError for Result<T, E> {
    #[track_caller]
    fn log_err(self) -> Self {
        if let Err(error) = self.as_ref() {
            let location = core::panic::Location::caller();
            log::error!("[{location}] {error}");
        }
        self
    }
}

#[cfg(feature = "std")]
impl<T, E: core::fmt::Display> StderrError for Result<T, E> {
    #[track_caller]
    fn stderr_err(self) -> Self {
        if let Err(error) = self.as_ref() {
            let location = core::panic::Location::caller();
            std::eprintln!("{}", ts_ansi::format_error!("[{location}] {error}"));
        }
        self
    }
}

#[cfg(feature = "log")]
impl<T> LogError for Option<T> {
    #[track_caller]
    fn log_err(self) -> Self {
        if self.is_none() {
            let location = core::panic::Location::caller();
            log::error!("[{location}] value was None");
        }
        self
    }
}

#[cfg(feature = "std")]
impl<T> StderrError for Option<T> {
    #[track_caller]
    fn stderr_err(self) -> Self {
        if self.is_none() {
            let location = core::panic::Location::caller();
            std::eprintln!("{}", ts_ansi::format_error!("[{location}] value was None"));
        }
        self
    }
}