zon_core 0.0.4

part of a new WIP, very incomplete async http service stack
Documentation
use std::{convert::Infallible, future::Future};

use super::{FromRequestParts, Request};
use crate::IntoResponse;

#[allow(unreachable_pub)] // intentionally unnameable types
mod private {
    #[derive(Debug, Clone, Copy)]
    pub enum ViaParts {}

    #[derive(Debug, Clone, Copy)]
    pub enum ViaRequest {}
}

pub trait FromRequest<B, M = private::ViaRequest>: Sized {
    /// If the extractor fails it'll use this "rejection" type. A rejection is
    /// a kind of error that can be converted into a response.
    type Rejection: IntoResponse;

    /// Perform the extraction.
    fn from_request(req: Request<B>) -> impl Future<Output = Result<Self, Self::Rejection>> + Send;
}

impl<B, T> FromRequest<B, private::ViaParts> for T
where
    B: Send,
    T: FromRequestParts,
{
    type Rejection = <Self as FromRequestParts>::Rejection;

    fn from_request(req: Request<B>) -> impl Future<Output = Result<Self, Self::Rejection>> + Send {
        let (mut parts, _) = req.into_parts();
        async move { Self::from_request_parts(&mut parts).await }
    }
}

impl<B: Send> FromRequest<B> for Request<B> {
    type Rejection = Infallible;

    async fn from_request(req: Request<B>) -> Result<Self, Self::Rejection> {
        Ok(req)
    }
}

impl<B: Send, T> FromRequest<B> for Result<T, T::Rejection>
where
    T: FromRequest<B>,
{
    type Rejection = Infallible;

    async fn from_request(req: Request<B>) -> Result<Self, Self::Rejection> {
        Ok(T::from_request(req).await)
    }
}