axum-error-object 0.0.4

Provides a `Result<T>` type and related utility types that can be used to holistically provide object response errors.
Documentation
use crate::ErrorResponse;

pub trait Context<T> {
    #[inline]
    fn context<C>(self, context: C) -> crate::Result<T>
    where
        Self: Sized,
        C: Into<ErrorResponse>,
    {
        self.with_context(move || context)
    }

    fn with_context<C>(self, context: impl FnOnce() -> C) -> crate::Result<T>
    where
        C: Into<ErrorResponse>;
}

impl<T, E> Context<T> for Result<T, E>
where
    E: Into<anyhow::Error>,
{
    fn with_context<C>(self, context: impl FnOnce() -> C) -> crate::Result<T>
    where
        C: Into<ErrorResponse>,
    {
        match self {
            Ok(value) => Ok(value),
            Err(error) => Err(context().into().with_source(error.into())),
        }
    }
}

impl<T> Context<T> for Option<T> {
    fn with_context<C>(self, context: impl FnOnce() -> C) -> crate::Result<T>
    where
        C: Into<ErrorResponse>,
    {
        match self {
            Some(value) => Ok(value),
            None => Err(context().into()),
        }
    }
}