1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

more default impls for FromRequest

This commit is contained in:
Nikolay Kim 2017-12-02 17:14:55 -08:00
parent fb3185de94
commit 6bc7d60f52
2 changed files with 64 additions and 2 deletions

View File

@ -12,7 +12,9 @@ use serde::Serialize;
use Cookie; use Cookie;
use body::Body; use body::Body;
use error::Error; use error::Error;
use route::{Reply, FromRequest};
use encoding::ContentEncoding; use encoding::ContentEncoding;
use httprequest::HttpRequest;
/// Represents various types of connection /// Represents various types of connection
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
@ -458,6 +460,12 @@ impl From<HttpResponseBuilder> for HttpResponse {
} }
} }
impl FromRequest for HttpResponseBuilder {
fn from_request(self, _: HttpRequest) -> Reply {
Reply::response(self)
}
}
impl From<&'static str> for HttpResponse { impl From<&'static str> for HttpResponse {
fn from(val: &'static str) -> Self { fn from(val: &'static str) -> Self {
HttpResponse::build(StatusCode::OK) 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 { impl From<&'static [u8]> for HttpResponse {
fn from(val: &'static [u8]) -> Self { fn from(val: &'static [u8]) -> Self {
HttpResponse::build(StatusCode::OK) 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<String> for HttpResponse { impl From<String> for HttpResponse {
fn from(val: String) -> Self { fn from(val: String) -> Self {
HttpResponse::build(StatusCode::OK) HttpResponse::build(StatusCode::OK)
@ -485,6 +511,15 @@ impl From<String> 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 { impl<'a> From<&'a String> for HttpResponse {
fn from(val: &'a String) -> Self { fn from(val: &'a String) -> Self {
HttpResponse::build(StatusCode::OK) 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<Bytes> for HttpResponse { impl From<Bytes> for HttpResponse {
fn from(val: Bytes) -> Self { fn from(val: Bytes) -> Self {
HttpResponse::build(StatusCode::OK) HttpResponse::build(StatusCode::OK)
@ -503,6 +547,15 @@ impl From<Bytes> 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<BytesMut> for HttpResponse { impl From<BytesMut> for HttpResponse {
fn from(val: BytesMut) -> Self { fn from(val: BytesMut) -> Self {
HttpResponse::build(StatusCode::OK) HttpResponse::build(StatusCode::OK)
@ -512,6 +565,15 @@ impl From<BytesMut> 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -92,10 +92,10 @@ default impl<T: FromRequest> FromRequest for T
} }
#[cfg(actix_nightly)] #[cfg(actix_nightly)]
default impl<T: FromRequest, E: Into<Error>> FromRequest for StdResult<T, E> { default impl<T: Into<HttpResponse>, E: Into<Error>> FromRequest for StdResult<T, E> {
fn from_request(self, req: HttpRequest) -> Reply { fn from_request(self, req: HttpRequest) -> Reply {
match self { 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())), Err(err) => Reply(ReplyItem::Message(err.into().into())),
} }
} }