spacegate_kernel/helper_layers/
map_request.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
pub mod add_extension;

use std::convert::Infallible;

use hyper::{Request, Response};
use tower_layer::Layer;

use crate::SgBody;

#[derive(Debug, Clone)]
pub struct MapRequestLayer<F> {
    map: F,
}

impl<F> MapRequestLayer<F> {
    pub fn new(map: F) -> Self {
        Self { map }
    }
}

impl<F, S> Layer<S> for MapRequestLayer<F>
where
    F: Fn(Request<SgBody>) -> Request<SgBody> + Clone,
{
    type Service = MapRequest<F, S>;

    fn layer(&self, inner: S) -> Self::Service {
        MapRequest { map: self.map.clone(), inner }
    }
}
#[derive(Debug)]
pub struct MapRequest<F, S> {
    map: F,
    inner: S,
}

impl<F, S> Clone for MapRequest<F, S>
where
    F: Clone,
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            map: self.map.clone(),
            inner: self.inner.clone(),
        }
    }
}

impl<F, S> hyper::service::Service<Request<SgBody>> for MapRequest<F, S>
where
    F: Fn(Request<SgBody>) -> Request<SgBody> + Clone,
    S: hyper::service::Service<Request<SgBody>, Error = Infallible, Response = Response<SgBody>>,
{
    type Response = Response<SgBody>;
    type Error = Infallible;
    type Future = S::Future;

    fn call(&self, request: Request<SgBody>) -> Self::Future {
        self.inner.call((self.map)(request))
    }
}