traq_bot_http/handler/
future.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_core::ready;
use http::{Response, StatusCode};
use http_body::Body;
use pin_project_lite::pin_project;
use tower_service::Service;

use crate::error::{Error, Result};
use crate::events::Event;
use crate::parser::ParseRequest;

pin_project! {
    /// <code>impl [Future]<Output = Result<(), [Error]>></code>
    ///
    /// `F: Future<Output = Result<(), E>>`を受け取り、エラー型`E`を[`Error`]に変換した[`Future`]を返します。
    /// 以下のコードと同様です。
    ///
    /// ```ignore
    /// use futures::TryFutureExt;
    ///
    /// async fn f() -> Result<(), E> { ... }
    ///
    /// let wrap_error = f().map_err(|e| -> traq_bot_http::Error { ... });
    /// ```
    ///
    /// [Future]: std::future::Future
    /// [Error]: crate::error::Error
    /// [`Future`]: std::future::Future
    /// [`Error`]: crate::error::Error
    #[must_use]
    #[project = WrapErrorFutureProject]
    #[derive(Debug)]
    pub struct WrapErrorFuture<F, E> {
        _error: PhantomData<E>,
        #[pin]
        inner: F,
    }
}

impl<F, E> WrapErrorFuture<F, E> {
    pub(crate) fn new(inner: F) -> Self {
        Self {
            _error: PhantomData,
            inner,
        }
    }
}

impl<F, E> Future for WrapErrorFuture<F, E>
where
    F: Future<Output = Result<(), E>>,
    E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
    type Output = Result<()>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let s = self.project();
        let res = ready!(s.inner.poll(cx));
        Poll::Ready(res.map_err(Error::handler))
    }
}

pin_project! {
    #[must_use]
    #[project = HandlerCallParseRequestProject]
    struct HandlerCallParseRequest<B, S>
    where
        B: Body,
    {
        #[pin]
        parse_request: ParseRequest<B>,
        service: S,
    }
}

impl<B, S> Future for HandlerCallParseRequest<B, S>
where
    B: Body,
    B::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    S: Service<Event>,
{
    type Output = Result<S::Future>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let s = self.project();
        let event = match ready!(s.parse_request.poll(cx)) {
            Ok(e) => e,
            Err(e) => return Poll::Ready(Err(e)),
        };
        let call = s.service.call(event);
        Poll::Ready(Ok(call))
    }
}

pin_project! {
    #[must_use]
    #[project = HandlerCallServiceCallProject]
    struct HandlerCallServiceCall<F> {
        #[pin]
        inner: F,
    }
}

impl<F> Future for HandlerCallServiceCall<F>
where
    F: Future<Output = Result<(), Error>>,
{
    type Output = Result<Response<String>>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let s = self.project();
        if let Err(e) = ready!(s.inner.poll(cx)) {
            return Poll::Ready(Err(e));
        }
        let res = Response::builder()
            .status(StatusCode::NO_CONTENT)
            .body(String::new())
            .map_err(Error::handler);
        Poll::Ready(res)
    }
}

pin_project! {
    #[must_use]
    #[project = HandlerCallInnerProject]
    #[project_replace = HandlerCallInnerProjectReplace]
    enum HandlerCallInner<B, S>
    where
        B: Body,
        S: Service<Event>,
    {
        ParseRequest {
            #[pin]
            inner: HandlerCallParseRequest<B, S>,
        },
        ServiceCall {
            #[pin]
            inner: HandlerCallServiceCall<S::Future>,
        },
    }
}

impl<B, S> Future for HandlerCallInner<B, S>
where
    B: Body,
    B::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    S: Service<Event, Response = (), Error = Error>,
{
    type Output = Result<Response<String>>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let s = self.as_mut().project();
        let service_call = match s {
            HandlerCallInnerProject::ParseRequest { inner } => match ready!(inner.poll(cx)) {
                Ok(c) => c,
                Err(e) => return Poll::Ready(Err(e)),
            },
            HandlerCallInnerProject::ServiceCall { inner } => return inner.poll(cx),
        };
        self.project_replace(HandlerCallInner::ServiceCall {
            inner: HandlerCallServiceCall {
                inner: service_call,
            },
        });
        cx.waker().wake_by_ref();
        Poll::Pending
    }
}

pin_project! {
    /// <code><[Handler] as [Service]<[Request]<[B]>>>::[Future]</code>
    ///
    /// [Handler]: crate::Handler
    /// [Service]: tower::Service
    /// [Request]: http::Request
    /// [B]: http_body::Body
    /// [Future]: crate::Handler::Future
    #[must_use]
    #[project = HandlerCallProject]
    pub struct HandlerCall<B, S>
    where
        B: Body,
        S: Service<Event>,
    {
        #[pin]
        inner: HandlerCallInner<B, S>,
    }
}

impl<B, S> HandlerCall<B, S>
where
    B: Body,
    S: Service<Event>,
{
    pub(super) fn new(parse_request: ParseRequest<B>, service: S) -> Self {
        Self {
            inner: HandlerCallInner::ParseRequest {
                inner: HandlerCallParseRequest {
                    parse_request,
                    service,
                },
            },
        }
    }
}

impl<B, S> Future for HandlerCall<B, S>
where
    B: Body,
    B::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    S: Service<Event, Response = (), Error = Error>,
{
    type Output = Result<Response<String>>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let s = self.project();
        s.inner.poll(cx)
    }
}