From 6bc7d60f5226e9d7df34d8db8479f80d0bd5b303 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 2 Dec 2017 17:14:55 -0800 Subject: [PATCH] more default impls for FromRequest --- src/httpresponse.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++ src/route.rs | 4 +-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 75f45c844..35499ef55 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -12,7 +12,9 @@ use serde::Serialize; use Cookie; use body::Body; use error::Error; +use route::{Reply, FromRequest}; use encoding::ContentEncoding; +use httprequest::HttpRequest; /// Represents various types of connection #[derive(Copy, Clone, PartialEq, Debug)] @@ -458,6 +460,12 @@ impl From for HttpResponse { } } +impl FromRequest for HttpResponseBuilder { + fn from_request(self, _: HttpRequest) -> Reply { + Reply::response(self) + } +} + impl From<&'static str> for HttpResponse { fn from(val: &'static str) -> Self { HttpResponse::build(StatusCode::OK) @@ -467,6 +475,15 @@ impl From<&'static str> for HttpResponse { } } +impl FromRequest for &'static str { + fn from_request(self, req: HttpRequest) -> Reply { + HttpResponse::build(StatusCode::OK) + .content_type("text/plain; charset=utf-8") + .body(self) + .from_request(req) + } +} + impl From<&'static [u8]> for HttpResponse { fn from(val: &'static [u8]) -> Self { HttpResponse::build(StatusCode::OK) @@ -476,6 +493,15 @@ impl From<&'static [u8]> for HttpResponse { } } +impl FromRequest for &'static [u8] { + fn from_request(self, req: HttpRequest) -> Reply { + HttpResponse::build(StatusCode::OK) + .content_type("application/octet-stream") + .body(self) + .from_request(req) + } +} + impl From for HttpResponse { fn from(val: String) -> Self { HttpResponse::build(StatusCode::OK) @@ -485,6 +511,15 @@ impl From for HttpResponse { } } +impl FromRequest for String { + fn from_request(self, req: HttpRequest) -> Reply { + HttpResponse::build(StatusCode::OK) + .content_type("text/plain; charset=utf-8") + .body(self) + .from_request(req) + } +} + impl<'a> From<&'a String> for HttpResponse { fn from(val: &'a String) -> Self { HttpResponse::build(StatusCode::OK) @@ -494,6 +529,15 @@ impl<'a> From<&'a String> for HttpResponse { } } +impl<'a> FromRequest for &'a String { + fn from_request(self, req: HttpRequest) -> Reply { + HttpResponse::build(StatusCode::OK) + .content_type("text/plain; charset=utf-8") + .body(self) + .from_request(req) + } +} + impl From for HttpResponse { fn from(val: Bytes) -> Self { HttpResponse::build(StatusCode::OK) @@ -503,6 +547,15 @@ impl From for HttpResponse { } } +impl FromRequest for Bytes { + fn from_request(self, req: HttpRequest) -> Reply { + HttpResponse::build(StatusCode::OK) + .content_type("application/octet-stream") + .body(self) + .from_request(req) + } +} + impl From for HttpResponse { fn from(val: BytesMut) -> Self { HttpResponse::build(StatusCode::OK) @@ -512,6 +565,15 @@ impl From for HttpResponse { } } +impl FromRequest for BytesMut { + fn from_request(self, req: HttpRequest) -> Reply { + HttpResponse::build(StatusCode::OK) + .content_type("application/octet-stream") + .body(self) + .from_request(req) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/route.rs b/src/route.rs index 1ab32a56f..e949fd853 100644 --- a/src/route.rs +++ b/src/route.rs @@ -92,10 +92,10 @@ default impl FromRequest for T } #[cfg(actix_nightly)] -default impl> FromRequest for StdResult { +default impl, E: Into> FromRequest for StdResult { fn from_request(self, req: HttpRequest) -> Reply { match self { - Ok(val) => val.from_request(req), + Ok(val) => Reply(ReplyItem::Message(val.into())), //val.from_request(req), Err(err) => Reply(ReplyItem::Message(err.into().into())), } }