Skip to content

Commit 3f6c3c8

Browse files
authored
Merge pull request #584 from jbr/from-body-for-response
provide a From<Body> for Response
2 parents 3d2854e + 17533b5 commit 3f6c3c8

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/response.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,18 @@ impl Into<http::Response> for Response {
258258
}
259259
}
260260

261+
impl From<http::Body> for Response {
262+
fn from(body: http::Body) -> Self {
263+
let mut res = Response::new(200);
264+
res.set_body(body);
265+
res
266+
}
267+
}
268+
261269
impl From<serde_json::Value> for Response {
262270
fn from(json_value: serde_json::Value) -> Self {
263271
Body::from_json(&json_value)
264-
.map(|body| {
265-
let mut res = Response::new(200);
266-
res.set_body(body);
267-
res
268-
})
272+
.map(|body| body.into())
269273
.unwrap_or_else(|_| Response::new(StatusCode::InternalServerError))
270274
}
271275
}
@@ -281,23 +285,13 @@ impl From<http::Response> for Response {
281285

282286
impl From<String> for Response {
283287
fn from(s: String) -> Self {
284-
let mut res = http_types::Response::new(StatusCode::Ok);
285-
res.set_body(s);
286-
Self {
287-
res,
288-
cookie_events: vec![],
289-
}
288+
Body::from_string(s).into()
290289
}
291290
}
292291

293292
impl<'a> From<&'a str> for Response {
294293
fn from(s: &'a str) -> Self {
295-
let mut res = http_types::Response::new(StatusCode::Ok);
296-
res.set_body(String::from(s));
297-
Self {
298-
res,
299-
cookie_events: vec![],
300-
}
294+
Body::from_string(String::from(s)).into()
301295
}
302296
}
303297

0 commit comments

Comments
 (0)