-
Notifications
You must be signed in to change notification settings - Fork 25
/
factory.rs
197 lines (163 loc) · 3.94 KB
/
factory.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
use futures::future::ok;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg(feature = "multipart")]
use warp::filters::multipart;
#[cfg(feature = "websocket")]
use warp::filters::ws::Ws;
use warp::{
filters::BoxedFilter,
reply::{json, Response},
Filter, Rejection, Reply,
};
pub trait FromRequest: Sized {
/// Extract should be `(Self,),`
type Filter: Filter<Error = Rejection>;
/// It's true iff the type represents whole request body.
///
/// It returns true for `Json<T>` and `Form<T>`.
fn is_body() -> bool {
false
}
/// It's true if the type is optional.
///
/// It returns true for `Option<T>`.
fn is_optional() -> bool {
false
}
/// It's true iff the type represents whole request query.
///
/// It returns true for `Query<T>`.
fn is_query() -> bool {
false
}
fn content_type() -> &'static str {
"*/*"
}
fn new() -> Self::Filter;
}
impl<T> FromRequest for Option<T>
where
T: 'static + FromRequest + Send + Send,
T::Filter: Send + Sync + Filter<Extract = (T,), Error = Rejection>,
{
type Filter = BoxedFilter<(Option<T>,)>;
fn is_body() -> bool {
T::is_body()
}
fn is_optional() -> bool {
true
}
fn is_query() -> bool {
T::is_query()
}
fn new() -> Self::Filter {
T::new()
.map(Some)
.or_else(|_| ok::<_, Rejection>((None,)))
.boxed()
}
}
/// Represents request body or response.
///
///
/// If it is in a parameter, content-type should be `application/json` and
/// request body will be deserialized.
///
/// If it is in a return type, response will contain a content type header with
/// value `application/json`, and value is serialized.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Json<T>(T);
impl<T> Json<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> From<T> for Json<T> {
#[inline]
fn from(v: T) -> Self {
Json(v)
}
}
impl<T> FromRequest for Json<T>
where
T: 'static + Send + DeserializeOwned,
{
type Filter = BoxedFilter<(Json<T>,)>;
fn is_body() -> bool {
true
}
fn content_type() -> &'static str {
"application/json"
}
fn new() -> Self::Filter {
warp::body::json().boxed()
}
}
impl<T> Reply for Json<T>
where
T: Serialize + Send,
{
fn into_response(self) -> Response {
json(&self.0).into_response()
}
}
/// Represents a request body with `www-url-form-encoded` content type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize)]
#[serde(transparent)]
pub struct Form<T>(T);
impl<T> Form<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> FromRequest for Form<T>
where
T: 'static + Send + DeserializeOwned,
{
type Filter = BoxedFilter<(Form<T>,)>;
fn is_body() -> bool {
true
}
fn content_type() -> &'static str {
"x-www-form-urlencoded"
}
fn new() -> Self::Filter {
warp::body::form().boxed()
}
}
/// Represents all query parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize)]
#[serde(transparent)]
pub struct Query<T>(T);
impl<T> Query<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> FromRequest for Query<T>
where
T: 'static + Send + DeserializeOwned,
{
type Filter = BoxedFilter<(Query<T>,)>;
fn is_query() -> bool {
true
}
fn new() -> Self::Filter {
warp::query().boxed()
}
}
#[cfg(feature = "websocket")]
impl FromRequest for Ws {
type Filter = BoxedFilter<(Ws,)>;
fn new() -> Self::Filter {
warp::ws().boxed()
}
}
#[cfg(feature = "multipart")]
impl FromRequest for multipart::FormData {
type Filter = BoxedFilter<(Self,)>;
fn new() -> Self::Filter {
warp::multipart::form().boxed()
}
}