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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use bytes::Bytes;
use http::header::{self, HeaderMap, HeaderValue};
#[cfg(feature = "server-warp")]
use http::response::Builder;
pub use http::StatusCode;
#[cfg(feature = "server-warp")]
use hyper::Body;
use std::fmt::Debug;
#[cfg(feature = "server-warp")]
use warp::{
reject::{self, Reject, Rejection},
reply::{Reply, Response},
};
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "server-warp")]
use warp::reject;
#[test]
fn new_status_contains_correct_code() {
assert_eq!(
Status::new(StatusCode::IM_A_TEAPOT).code(),
&StatusCode::IM_A_TEAPOT
);
}
#[test]
fn new_status_contains_correct_specified_message() {
assert_eq!(
Status::with_message(StatusCode::IM_A_TEAPOT, String::from("foobar")).message(),
Some("foobar")
);
}
#[test]
fn new_status_does_not_contain_message() {
assert_eq!(Status::new(StatusCode::IM_A_TEAPOT).message(), None);
}
#[test]
fn new_status_has_empty_headers() {
assert!(Status::new(StatusCode::IM_A_TEAPOT).headers().is_empty());
}
#[test]
fn server_error_does_not_contain_error_message() {
let server_msg = "foobar";
let status =
Status::with_message(StatusCode::INTERNAL_SERVER_ERROR, String::from(server_msg));
let client_msg = status.to_string();
assert!(!client_msg.contains(server_msg));
}
#[test]
#[cfg(feature = "server-warp")]
fn status_rejection_is_a_status() {
assert!(Status::<Empty>::rejection_is_status(&reject::custom(Status::new(
StatusCode::IM_A_TEAPOT
))));
}
#[test]
#[cfg(feature = "server-warp")]
fn non_status_rejection_is_not_status() {
assert!(!Status::<Empty>::rejection_is_status(&reject::not_found()));
}
#[test]
#[cfg(feature = "server-warp")]
fn rejection_from_status() {
let data = vec![0u8, 1u8, 2u8, 3u8, 4u8];
let status = Status::with_data(StatusCode::IM_A_TEAPOT, data.clone());
let rej = reject::Rejection::from(status.clone());
let rej_status = rej.find::<Status<Vec<u8>>>().unwrap();
assert_eq!(&status, rej_status);
}
}
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "server-warp")]
NotStatus(Rejection),
}
pub trait StatusInnerData: Clone + Debug + Send + Sync + 'static {}
impl<T: Clone + Debug + Send + Sync + 'static> StatusInnerData for T {}
pub trait StatusData: Into<Bytes> + StatusInnerData {}
impl<T: Into<Bytes> + StatusInnerData> StatusData for T {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Empty;
impl Into<Bytes> for Empty {
fn into(self) -> Bytes {
Bytes::new()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Status<T = Empty>
where
T: StatusData,
{
c: StatusCode,
data: T,
data_bytes: Bytes,
h: HeaderMap<HeaderValue>,
}
impl Status {
pub fn new(code: StatusCode) -> Status<Empty> {
Status {
c: code,
data: Empty{},
data_bytes: Bytes::new(),
h: HeaderMap::new(),
}
}
pub fn with_message(code: StatusCode, msg: String) -> Status<String> {
let mut status = Status::with_data(code, msg);
status.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_str(mime::TEXT_PLAIN_UTF_8.as_ref()).unwrap(),
);
status
}
pub fn with_data<T: StatusData>(code: StatusCode, data: T) -> Status<T> {
Status {
c: code,
data: data.clone(),
data_bytes: data.into(),
h: HeaderMap::new(),
}
}
}
impl<T: StatusData> Status<T> {
pub fn code(&self) -> &StatusCode {
&self.c
}
fn data_as_message(&self) -> Option<&str> {
std::str::from_utf8(self.data_bytes.as_ref()).ok()
}
pub fn message(&self) -> Option<&str> {
if self.h.contains_key(header::CONTENT_TYPE) {
match self.h.get(header::CONTENT_TYPE).unwrap().to_str() {
Err(_) => None,
Ok(content_type) => match content_type.parse::<mime::Mime>().ok() {
None => None,
Some(mime_type) => match mime_type.type_() {
mime::TEXT => self.data_as_message(),
_ => {
if mime_type == mime::APPLICATION_JSON {
self.data_as_message()
} else {
None
}
}
},
},
}
} else {
None
}
}
pub fn data(&self) -> &T {
&self.data
}
pub fn bytes(&self) -> &[u8] {
&self.data_bytes
}
pub fn headers(&self) -> &HeaderMap<HeaderValue> {
&self.h
}
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue> {
&mut self.h
}
#[cfg(feature = "server-warp")]
pub fn rejection_is_status(err: &Rejection) -> bool {
err.find::<Self>().is_some()
}
#[cfg(feature = "server-warp")]
pub fn recover(err: Rejection) -> std::result::Result<Self, Error> {
err.find::<Self>()
.map(|stat| stat.clone())
.ok_or(Error::NotStatus(err))
}
}
#[cfg(feature = "server-warp")]
impl <T: StatusData> From<Status<T>> for Rejection {
fn from(status: Status<T>) -> Self {
reject::custom(status)
}
}
impl<T: StatusData> std::fmt::Display for Status<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.message() {
None => write!(f, "{}", self.code()),
Some(msg) => {
if self.code().as_u16() < 500 {
write!(f, "{}\n{}", self.code(), msg)
} else {
write!(f, "{}", self.code())
}
}
}
}
}
impl<T: StatusData> std::error::Error for Status<T> {}
impl<T: StatusData> From<Status<T>> for Result<Status<T>, Status<T>> {
fn from(s: Status<T>) -> Result<Status<T>, Status<T>> {
if s.code().as_u16() < 400 {
Ok(s)
} else {
Err(s)
}
}
}
#[cfg(feature = "server-warp")]
impl<T: StatusData> From<Status<T>> for Response {
fn from(s: Status<T>) -> Response {
let mut build = Builder::new().status(s.code());
for (key, val) in s.headers().iter() {
build = build.header(key, val)
}
build.body(Body::from(s.data_bytes)).unwrap()
}
}
#[cfg(feature = "server-warp")]
impl<T: StatusData> Reject for Status<T> {}
#[cfg(feature = "server-warp")]
impl<T: StatusData> Reply for Status<T> {
fn into_response(self) -> Response {
self.into()
}
}