1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00

better naming

This commit is contained in:
Nikolay Kim 2017-10-06 23:36:36 -07:00
parent a505be9321
commit ce4aea46c3
6 changed files with 28 additions and 28 deletions

View File

@ -20,7 +20,7 @@ pub const HTTPMethodNotAllowed: StaticResponse = StaticResponse(StatusCode::METH
impl<S> RouteHandler<S> for StaticResponse { impl<S> RouteHandler<S> for StaticResponse {
fn handle(&self, req: HttpRequest, _: Option<Payload>, _: Rc<S>) -> Task fn handle(&self, req: HttpRequest, _: Option<Payload>, _: Rc<S>) -> Task
{ {
Task::reply(HttpResponse::new(req, self.0, Body::Empty), None) Task::reply(HttpResponse::new(req, self.0, Body::Empty))
} }
} }

View File

@ -25,9 +25,9 @@ impl Route for MyRoute {
{ {
if let Some(pl) = payload { if let Some(pl) = payload {
ctx.add_stream(pl); ctx.add_stream(pl);
HttpMessage::Stream(MyRoute{req: Some(req)}) Self::stream(MyRoute{req: Some(req)})
} else { } else {
HttpMessage::Reply(req, httpcodes::HTTPOk) Self::reply(req, httpcodes::HTTPOk)
} }
} }
} }

View File

@ -3,7 +3,6 @@ use std::marker::PhantomData;
use std::collections::HashMap; use std::collections::HashMap;
use actix::Actor; use actix::Actor;
use bytes::Bytes;
use http::Method; use http::Method;
use task::Task; use task::Task;
@ -88,7 +87,7 @@ impl<S: 'static> RouteHandler<S> for HttpResource<S> {
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))] #[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
enum HttpMessageItem<A> where A: Actor<Context=HttpContext<A>> + Route { enum HttpMessageItem<A> where A: Actor<Context=HttpContext<A>> + Route {
Message(HttpResponse, Option<Bytes>), Message(HttpResponse),
Actor(A), Actor(A),
} }
@ -97,27 +96,21 @@ pub struct HttpMessage<A: Actor<Context=HttpContext<A>> + Route> (HttpMessageIte
impl<A> HttpMessage<A> where A: Actor<Context=HttpContext<A>> + Route impl<A> HttpMessage<A> where A: Actor<Context=HttpContext<A>> + Route
{ {
/// Create async response /// Create async response
#[allow(non_snake_case)] pub fn stream(act: A) -> Self {
pub fn Stream(act: A) -> Self {
HttpMessage(HttpMessageItem::Actor(act)) HttpMessage(HttpMessageItem::Actor(act))
} }
#[allow(non_snake_case)] /// Create response with empty body
pub fn Reply<I>(req: HttpRequest, msg: I) -> Self pub fn reply<I>(req: HttpRequest, msg: I) -> Self
where I: IntoHttpResponse where I: IntoHttpResponse
{ {
HttpMessage(HttpMessageItem::Message(msg.into_response(req), None)) HttpMessage(HttpMessageItem::Message(msg.into_response(req)))
}
#[allow(non_snake_case)]
pub fn ReplyMessage(msg: HttpResponse, body: Option<Bytes>) -> Self {
HttpMessage(HttpMessageItem::Message(msg, body))
} }
pub(crate) fn into(self, mut ctx: HttpContext<A>) -> Task { pub(crate) fn into(self, mut ctx: HttpContext<A>) -> Task {
match self.0 { match self.0 {
HttpMessageItem::Message(msg, body) => { HttpMessageItem::Message(msg) => {
Task::reply(msg, body) Task::reply(msg)
}, },
HttpMessageItem::Actor(act) => { HttpMessageItem::Actor(act) => {
ctx.set_actor(act); ctx.set_actor(act);

View File

@ -8,7 +8,7 @@ use futures::unsync::mpsc::Receiver;
use task::Task; use task::Task;
use context::HttpContext; use context::HttpContext;
use resource::HttpMessage; use resource::HttpMessage;
use httpmessage::{HttpRequest, HttpResponse}; use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse};
/// Stream of `PayloadItem`'s /// Stream of `PayloadItem`'s
pub type Payload = Receiver<PayloadItem>; pub type Payload = Receiver<PayloadItem>;
@ -60,6 +60,18 @@ pub trait Route: Actor<Context=HttpContext<Self>> {
fn factory() -> RouteFactory<Self, Self::State> { fn factory() -> RouteFactory<Self, Self::State> {
RouteFactory(PhantomData) RouteFactory(PhantomData)
} }
/// Create async response
fn stream(act: Self) -> HttpMessage<Self> {
HttpMessage::stream(act)
}
/// Create response
fn reply<I>(req: HttpRequest, msg: I) -> HttpMessage<Self>
where I: IntoHttpResponse
{
HttpMessage::reply(req, msg)
}
} }

View File

@ -91,7 +91,7 @@ impl Router {
} }
} }
Task::reply(IntoHttpResponse::into_response(HTTPNotFound, req), None) Task::reply(IntoHttpResponse::into_response(HTTPNotFound, req))
} }
} }
} }

View File

@ -4,7 +4,7 @@ use std::fmt::Write;
use std::collections::VecDeque; use std::collections::VecDeque;
use http::{StatusCode, Version}; use http::{StatusCode, Version};
use bytes::{Bytes, BytesMut}; use bytes::BytesMut;
use futures::{Async, Future, Poll, Stream}; use futures::{Async, Future, Poll, Stream};
use tokio_core::net::TcpStream; use tokio_core::net::TcpStream;
@ -57,15 +57,10 @@ pub struct Task {
impl Task { impl Task {
pub(crate) fn reply(msg: HttpResponse, body: Option<Bytes>) -> Self { pub(crate) fn reply(msg: HttpResponse) -> Self {
let mut frames = VecDeque::new(); let mut frames = VecDeque::new();
if let Some(body) = body { frames.push_back(Frame::Message(msg));
frames.push_back(Frame::Message(msg)); frames.push_back(Frame::Payload(None));
frames.push_back(Frame::Payload(Some(body)));
frames.push_back(Frame::Payload(None));
} else {
frames.push_back(Frame::Message(msg));
}
Task { Task {
state: TaskRunningState::Running, state: TaskRunningState::Running,