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
use std::marker::PhantomData;

use super::*;
use reply::Reply;

/// `serv_state` build `HyperService` with given function `F` and state `S`.
pub fn serv_state<F, S, Req, Resp, E>(state: S, f: F) -> HyperService
where
    F: for<'a> Fn(&'a S, Req) -> Box<Future<Item = Resp, Error = E>> + 'static,
    S: 'static,
    Req: for<'de> serde::Deserialize<'de> + 'static,
    Resp: serde::Serialize + 'static,
    E: From<Error> + Debug + std::error::Error + 'static,
{
    reply::ServiceReply::serv_state(state, f)
}

/// `service` builds `HyperService` with given function `F`.
pub fn serv<F, Req, Resp, E>(f: F) -> HyperService
where
    F: Fn(Req) -> Box<Future<Item = Resp, Error = E>> + 'static,
    Req: for<'de> serde::Deserialize<'de> + 'static,
    Resp: serde::Serialize + 'static,
    E: From<Error> + Debug + std::error::Error + 'static,
{
    reply::ServiceReply::serv(f)
}

/// `AsyncServiceFn` implements `AsyncService` for given `F`
pub(crate) struct AsyncServiceFn<F, Req, Resp, E>
where
    F: Fn(Req) -> Box<Future<Item = Resp, Error = E>>,
    Req: 'static,
    Resp: 'static,
{
    f: F,
    _req: PhantomData<Req>,
    _resp: PhantomData<Resp>,
}
impl<F, Req, Resp, E> AsyncServiceFn<F, Req, Resp, E>
where
    F: Fn(Req) -> Box<Future<Item = Resp, Error = E>>,
    Req: 'static,
    Resp: 'static,
{
    pub(crate) fn new(f: F) -> Self {
        Self {
            f,
            _req: Default::default(),
            _resp: Default::default(),
        }
    }
}
impl<F, Req, Resp, E> AsyncService for AsyncServiceFn<F, Req, Resp, E>
where
    F: Fn(Req) -> Box<Future<Item = Resp, Error = E>>,
    Req: 'static,
    Resp: 'static,
{
    type Req = Req;
    type Resp = Resp;
    type E = E;
    fn call(&self, req: Self::Req) -> Box<Future<Item = Self::Resp, Error = Self::E>> {
        let f = &self.f;
        f(req)
    }
}

/// Oneshot-style asynchronous service.
pub(crate) trait AsyncService {
    type Req;
    type Resp;
    type E;

    fn call(&self, req: Self::Req) -> Box<Future<Item = Self::Resp, Error = Self::E>>;
}

/// `AsyncServiceStateW` implementes `tokio_service::Service` for `AsyncService`
pub(crate) struct AsyncServiceStateW<T, Reply> {
    inner: SyncObj<T>,
    reply: PhantomData<Reply>,
}
impl<T, Reply> AsyncServiceStateW<T, Reply> {
    pub(crate) fn new(t: T) -> Self {
        Self {
            inner: SyncObj::new(t),
            reply: Default::default(),
        }
    }
}

impl<T, Req, Resp, E, Reply> hyper::service::Service for AsyncServiceStateW<T, Reply>
where
    T: AsyncService<Req = Req, Resp = Resp, E = E> + 'static,
    Req: for<'de> serde::Deserialize<'de> + 'static,
    Resp: serde::Serialize + 'static,
    E: From<Error> + 'static,
    Reply: reply::Reply<Resp, E> + 'static,
{
    type ReqBody = Body;
    type ResBody = Body;
    type Error = hyper::Error;
    type Future = HyperFuture;

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        let obj = self.inner.clone();
        let f = parse_req(req)
            .map_err(E::from)
            .and_then(move |req| T::call(&obj, req))
            .then(|resp| {
                let status = match resp.is_ok() {
                    true => hyper::StatusCode::OK,
                    false => match resp.as_ref().err() {
                        // TODO: dispatch error type?
                        _ => hyper::StatusCode::BAD_REQUEST,
                    },
                };
                Reply::from(resp).reply(status)
            });
        Box::new(f)
    }
}