spacegate_kernel/helper_layers/
map_request.rs

1pub mod add_extension;
2
3use std::convert::Infallible;
4
5use hyper::{Request, Response};
6use tower_layer::Layer;
7
8use crate::SgBody;
9
10#[derive(Debug, Clone)]
11pub struct MapRequestLayer<F> {
12    map: F,
13}
14
15impl<F> MapRequestLayer<F> {
16    pub fn new(map: F) -> Self {
17        Self { map }
18    }
19}
20
21impl<F, S> Layer<S> for MapRequestLayer<F>
22where
23    F: Fn(Request<SgBody>) -> Request<SgBody> + Clone,
24{
25    type Service = MapRequest<F, S>;
26
27    fn layer(&self, inner: S) -> Self::Service {
28        MapRequest { map: self.map.clone(), inner }
29    }
30}
31#[derive(Debug)]
32pub struct MapRequest<F, S> {
33    map: F,
34    inner: S,
35}
36
37impl<F, S> Clone for MapRequest<F, S>
38where
39    F: Clone,
40    S: Clone,
41{
42    fn clone(&self) -> Self {
43        Self {
44            map: self.map.clone(),
45            inner: self.inner.clone(),
46        }
47    }
48}
49
50impl<F, S> hyper::service::Service<Request<SgBody>> for MapRequest<F, S>
51where
52    F: Fn(Request<SgBody>) -> Request<SgBody> + Clone,
53    S: hyper::service::Service<Request<SgBody>, Error = Infallible, Response = Response<SgBody>>,
54{
55    type Response = Response<SgBody>;
56    type Error = Infallible;
57    type Future = S::Future;
58
59    fn call(&self, request: Request<SgBody>) -> Self::Future {
60        self.inner.call((self.map)(request))
61    }
62}