use std::future::Future;
use zon_core::{HttpMiddleware, HttpService};
#[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
}
}