zon_middleware 0.0.6

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

use zon_core::{HttpMiddleware, HttpService};

/// Middleware to modify a request.
///
/// Currently, this middleware only accepts an `Fn(http::Request<B>) ->
/// http::Request<B>`. `FromRequest[Parts]` and `IntoResponse` return types are
/// going to be supported in the future.
#[derive(Clone, Copy, Debug)]
pub struct MapRequest<F> {
    map_fn: F,
}

impl<F> MapRequest<F> {
    pub fn new(map_fn: F) -> Self {
        Self { map_fn }
    }
}

impl<S, F> HttpMiddleware<S> for MapRequest<F> {
    type Service = MapRequestService<S, F>;

    fn apply(self, inner: S) -> Self::Service {
        MapRequestService::new(inner, self.map_fn)
    }
}

pub struct MapRequestService<S, F> {
    inner: S,
    map_fn: F,
}

impl<S, F> MapRequestService<S, F> {
    pub fn new(inner: S, map_fn: F) -> Self {
        Self { inner, map_fn }
    }
}

impl<S, F, Fut, B, B2> HttpService<B> for MapRequestService<S, F>
where
    S: HttpService<B2>,
    F: Fn(http::Request<B>) -> Fut + Sync,
    Fut: Future<Output = http::Request<B2>> + Send,
    B: Send,
    B2: Send,
{
    type ResponseBody = S::ResponseBody;

    async fn call(&self, request: http::Request<B>) -> http::Response<Self::ResponseBody> {
        let request = (self.map_fn)(request).await;
        self.inner.call(request).await
    }
}