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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use xitca_service::{ready::ReadyService, Service};

use crate::http::{BorrowReqMut, Extensions};

/// builder for middleware attaching typed data to [`Request`]'s [`Extensions`]
///
/// [`Request`]: crate::http::Request
#[derive(Clone)]
pub struct Extension<S> {
    state: S,
}

impl<S> Extension<S> {
    pub fn new(state: S) -> Self
    where
        S: Send + Sync + Clone + 'static,
    {
        Extension { state }
    }
}

impl<T, S, E> Service<Result<S, E>> for Extension<T>
where
    T: Clone,
{
    type Response = ExtensionService<S, T>;
    type Error = E;

    async fn call(&self, res: Result<S, E>) -> Result<Self::Response, Self::Error> {
        res.map(|service| ExtensionService {
            service,
            state: self.state.clone(),
        })
    }
}

pub struct ExtensionService<S, St> {
    service: S,
    state: St,
}

impl<S, St> Clone for ExtensionService<S, St>
where
    S: Clone,
    St: Clone,
{
    fn clone(&self) -> Self {
        Self {
            service: self.service.clone(),
            state: self.state.clone(),
        }
    }
}

impl<S, St, Req> Service<Req> for ExtensionService<S, St>
where
    S: Service<Req>,
    St: Send + Sync + Clone + 'static,
    Req: BorrowReqMut<Extensions>,
{
    type Response = S::Response;
    type Error = S::Error;

    #[inline]
    async fn call(&self, mut req: Req) -> Result<Self::Response, Self::Error> {
        req.borrow_mut().insert(self.state.clone());
        self.service.call(req).await
    }
}

impl<S, St> ReadyService for ExtensionService<S, St>
where
    S: ReadyService,
    St: Send + Sync + Clone + 'static,
{
    type Ready = S::Ready;

    #[inline]
    async fn ready(&self) -> Self::Ready {
        self.service.ready().await
    }
}

#[cfg(test)]
mod test {
    use xitca_service::{fn_service, ServiceExt};
    use xitca_unsafe_collection::futures::NowOrPanic;

    use crate::http::Request;

    use super::*;

    #[test]
    fn state_middleware() {
        let service = fn_service(|req: Request<()>| async move {
            assert_eq!("state", req.extensions().get::<String>().unwrap());
            Ok::<_, ()>("996")
        })
        .enclosed(Extension::new(String::from("state")))
        .call(())
        .now_or_panic()
        .unwrap();

        let res = service.call(Request::new(())).now_or_panic().unwrap();

        assert_eq!("996", res);
    }

    #[test]
    fn state_middleware_http_request() {
        let service = fn_service(|req: http::Request<()>| async move {
            assert_eq!("state", req.extensions().get::<String>().unwrap());
            Ok::<_, ()>("996")
        })
        .enclosed(Extension::new(String::from("state")))
        .call(())
        .now_or_panic()
        .unwrap();

        let res = service.call(http::Request::new(())).now_or_panic().unwrap();

        assert_eq!("996", res);
    }
}