http-types 1.0.0

Common types for HTTP operations.
Documentation
use crate::{Error, StatusCode};
use core::convert::{Infallible, TryInto};
use std::error::Error as StdError;

/// Provides the `status` method for `Result`.
///
/// This trait is sealed and cannot be implemented outside of `http-types`.
pub trait ResultExt<T, E>: private::Sealed {
    /// Wrap the error value with an additional status code.
    fn status<S>(self, status: S) -> Result<T, Error>
    where
        S: TryInto<StatusCode>,
        S::Error: std::fmt::Debug;

    /// Wrap the error value with an additional status code that is evaluated
    /// lazily only once an error does occur.
    fn with_status<S, F>(self, f: F) -> Result<T, Error>
    where
        S: TryInto<StatusCode>,
        S::Error: std::fmt::Debug,
        F: FnOnce() -> S;
}

impl<T, E> ResultExt<T, E> for Result<T, E>
where
    E: StdError + Send + Sync + 'static,
{
    fn status<S>(self, status: S) -> Result<T, Error>
    where
        S: TryInto<StatusCode>,
        S::Error: std::fmt::Debug,
    {
        self.map_err(|error| {
            let status = status.try_into().unwrap();
            Error::new(status, error)
        })
    }

    fn with_status<S, F>(self, f: F) -> Result<T, Error>
    where
        S: TryInto<StatusCode>,
        S::Error: std::fmt::Debug,
        F: FnOnce() -> S,
    {
        self.map_err(|error| {
            let status = f().try_into().unwrap();
            Error::new(status, error)
        })
    }
}

impl<T> ResultExt<T, Infallible> for Option<T> {
    fn status<S>(self, status: S) -> Result<T, Error>
    where
        S: TryInto<StatusCode>,
        S::Error: std::fmt::Debug,
    {
        self.ok_or_else(|| {
            let status = status.try_into().unwrap();
            Error::from_str(status, "NoneError")
        })
    }

    fn with_status<S, F>(self, f: F) -> Result<T, Error>
    where
        S: TryInto<StatusCode>,
        S::Error: std::fmt::Debug,
        F: FnOnce() -> S,
    {
        self.ok_or_else(|| {
            let status = f().try_into().unwrap();
            Error::from_str(status, "NoneError")
        })
    }
}

pub(crate) mod private {
    pub trait Sealed {}

    impl<T, E> Sealed for Result<T, E> {}
    impl<T> Sealed for Option<T> {}
}