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
use futures::{self, Future, Poll};
use std::fmt;
use error::Error;
use input::{self, Input};
use output::Output;
pub trait Modifier {
#[allow(unused_variables)]
fn before_handle(&self, input: &mut Input) -> BeforeHandle {
BeforeHandle::ready(Ok(None))
}
#[allow(unused_variables)]
fn after_handle(&self, input: &mut Input, result: Result<Output, Error>) -> AfterHandle {
AfterHandle::ready(result)
}
}
#[derive(Debug)]
pub struct BeforeHandle(BeforeHandleState);
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
enum BeforeHandleState {
Ready(Option<Result<Option<Output>, Error>>),
Polling(Box<dyn FnMut(&mut Input) -> Poll<Option<Output>, Error> + Send + 'static>),
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for BeforeHandleState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::BeforeHandleState::*;
match *self {
Ready(ref res) => f.debug_tuple("Ready").field(res).finish(),
Polling(..) => f.debug_tuple("Polling").finish(),
}
}
}
impl<E> From<Result<Option<Output>, E>> for BeforeHandle
where
Error: From<E>,
{
fn from(result: Result<Option<Output>, E>) -> BeforeHandle {
BeforeHandle::ready(result.map_err(Into::into))
}
}
impl BeforeHandle {
pub fn ready(result: Result<Option<Output>, Error>) -> BeforeHandle {
BeforeHandle(BeforeHandleState::Ready(Some(result)))
}
pub fn polling(f: impl FnMut(&mut Input) -> Poll<Option<Output>, Error> + Send + 'static) -> BeforeHandle {
BeforeHandle(BeforeHandleState::Polling(Box::new(f)))
}
#[inline(always)]
pub fn wrap_future(mut future: impl Future<Item = Option<Output>, Error = Error> + Send + 'static) -> BeforeHandle {
BeforeHandle::polling(move |input| input::with_set_current(input, || future.poll()))
}
pub(crate) fn poll_ready(&mut self, input: &mut Input) -> Poll<Option<Output>, Error> {
use self::BeforeHandleState::*;
match self.0 {
Ready(ref mut res) => res.take()
.expect("BeforeHandle has already polled")
.map(futures::Async::Ready),
Polling(ref mut f) => f(input),
}
}
}
#[derive(Debug)]
pub struct AfterHandle(AfterHandleState);
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
enum AfterHandleState {
Ready(Option<Result<Output, Error>>),
Polling(Box<dyn FnMut(&mut Input) -> Poll<Output, Error> + Send + 'static>),
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for AfterHandleState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::AfterHandleState::*;
match *self {
Ready(ref res) => f.debug_tuple("Ready").field(res).finish(),
Polling(..) => f.debug_tuple("Polling").finish(),
}
}
}
impl<T, E> From<Result<T, E>> for AfterHandle
where
T: Into<Output>,
Error: From<E>,
{
fn from(result: Result<T, E>) -> AfterHandle {
AfterHandle::ready(result.map(Into::into).map_err(Into::into))
}
}
impl AfterHandle {
pub fn ready(result: Result<Output, Error>) -> AfterHandle {
AfterHandle(AfterHandleState::Ready(Some(result)))
}
pub fn polling(f: impl FnMut(&mut Input) -> Poll<Output, Error> + Send + 'static) -> AfterHandle {
AfterHandle(AfterHandleState::Polling(Box::new(f)))
}
#[inline(always)]
pub fn wrap_future(mut future: impl Future<Item = Output, Error = Error> + Send + 'static) -> AfterHandle {
AfterHandle::polling(move |input| input::with_set_current(input, || future.poll()))
}
pub(crate) fn poll_ready(&mut self, input: &mut Input) -> Poll<Output, Error> {
use self::AfterHandleState::*;
match self.0 {
Ready(ref mut res) => res.take()
.expect("AfterHandle has already polled")
.map(futures::Async::Ready),
Polling(ref mut f) => (*f)(input),
}
}
}