From ce4aea46c36d95b46c6b13c373e46df2788a8318 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 6 Oct 2017 23:36:36 -0700 Subject: [PATCH] better naming --- src/httpcodes.rs | 2 +- src/main.rs | 4 ++-- src/resource.rs | 21 +++++++-------------- src/route.rs | 14 +++++++++++++- src/router.rs | 2 +- src/task.rs | 13 ++++--------- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/httpcodes.rs b/src/httpcodes.rs index 517d0de2a..57cea818c 100644 --- a/src/httpcodes.rs +++ b/src/httpcodes.rs @@ -20,7 +20,7 @@ pub const HTTPMethodNotAllowed: StaticResponse = StaticResponse(StatusCode::METH impl RouteHandler for StaticResponse { fn handle(&self, req: HttpRequest, _: Option, _: Rc) -> Task { - Task::reply(HttpResponse::new(req, self.0, Body::Empty), None) + Task::reply(HttpResponse::new(req, self.0, Body::Empty)) } } diff --git a/src/main.rs b/src/main.rs index 66c8c290e..471a67c7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,9 +25,9 @@ impl Route for MyRoute { { if let Some(pl) = payload { ctx.add_stream(pl); - HttpMessage::Stream(MyRoute{req: Some(req)}) + Self::stream(MyRoute{req: Some(req)}) } else { - HttpMessage::Reply(req, httpcodes::HTTPOk) + Self::reply(req, httpcodes::HTTPOk) } } } diff --git a/src/resource.rs b/src/resource.rs index 679e8d3b2..05806843b 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -3,7 +3,6 @@ use std::marker::PhantomData; use std::collections::HashMap; use actix::Actor; -use bytes::Bytes; use http::Method; use task::Task; @@ -88,7 +87,7 @@ impl RouteHandler for HttpResource { #[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))] enum HttpMessageItem where A: Actor> + Route { - Message(HttpResponse, Option), + Message(HttpResponse), Actor(A), } @@ -97,27 +96,21 @@ pub struct HttpMessage> + Route> (HttpMessageIte impl HttpMessage where A: Actor> + Route { /// Create async response - #[allow(non_snake_case)] - pub fn Stream(act: A) -> Self { + pub fn stream(act: A) -> Self { HttpMessage(HttpMessageItem::Actor(act)) } - #[allow(non_snake_case)] - pub fn Reply(req: HttpRequest, msg: I) -> Self + /// Create response with empty body + pub fn reply(req: HttpRequest, msg: I) -> Self where I: IntoHttpResponse { - HttpMessage(HttpMessageItem::Message(msg.into_response(req), None)) - } - - #[allow(non_snake_case)] - pub fn ReplyMessage(msg: HttpResponse, body: Option) -> Self { - HttpMessage(HttpMessageItem::Message(msg, body)) + HttpMessage(HttpMessageItem::Message(msg.into_response(req))) } pub(crate) fn into(self, mut ctx: HttpContext) -> Task { match self.0 { - HttpMessageItem::Message(msg, body) => { - Task::reply(msg, body) + HttpMessageItem::Message(msg) => { + Task::reply(msg) }, HttpMessageItem::Actor(act) => { ctx.set_actor(act); diff --git a/src/route.rs b/src/route.rs index cbbbaae28..4cf57e919 100644 --- a/src/route.rs +++ b/src/route.rs @@ -8,7 +8,7 @@ use futures::unsync::mpsc::Receiver; use task::Task; use context::HttpContext; use resource::HttpMessage; -use httpmessage::{HttpRequest, HttpResponse}; +use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse}; /// Stream of `PayloadItem`'s pub type Payload = Receiver; @@ -60,6 +60,18 @@ pub trait Route: Actor> { fn factory() -> RouteFactory { RouteFactory(PhantomData) } + + /// Create async response + fn stream(act: Self) -> HttpMessage { + HttpMessage::stream(act) + } + + /// Create response + fn reply(req: HttpRequest, msg: I) -> HttpMessage + where I: IntoHttpResponse + { + HttpMessage::reply(req, msg) + } } diff --git a/src/router.rs b/src/router.rs index 08baab08d..02fe2ab26 100644 --- a/src/router.rs +++ b/src/router.rs @@ -91,7 +91,7 @@ impl Router { } } - Task::reply(IntoHttpResponse::into_response(HTTPNotFound, req), None) + Task::reply(IntoHttpResponse::into_response(HTTPNotFound, req)) } } } diff --git a/src/task.rs b/src/task.rs index 047896d8b..034f91b9a 100644 --- a/src/task.rs +++ b/src/task.rs @@ -4,7 +4,7 @@ use std::fmt::Write; use std::collections::VecDeque; use http::{StatusCode, Version}; -use bytes::{Bytes, BytesMut}; +use bytes::BytesMut; use futures::{Async, Future, Poll, Stream}; use tokio_core::net::TcpStream; @@ -57,15 +57,10 @@ pub struct Task { impl Task { - pub(crate) fn reply(msg: HttpResponse, body: Option) -> Self { + pub(crate) fn reply(msg: HttpResponse) -> Self { let mut frames = VecDeque::new(); - if let Some(body) = body { - frames.push_back(Frame::Message(msg)); - frames.push_back(Frame::Payload(Some(body))); - frames.push_back(Frame::Payload(None)); - } else { - frames.push_back(Frame::Message(msg)); - } + frames.push_back(Frame::Message(msg)); + frames.push_back(Frame::Payload(None)); Task { state: TaskRunningState::Running,