1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

add Either responder

This commit is contained in:
Nikolay Kim 2018-03-10 09:39:43 -08:00
parent 4263574a58
commit cad55f9c80
2 changed files with 31 additions and 1 deletions

View File

@ -34,6 +34,36 @@ pub trait Responder {
fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error>; fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error>;
} }
/// Combines two different responders types into a single type.
#[derive(Debug)]
pub enum Either<A, B> {
/// First branch of the type
A(A),
/// Second branch of the type
B(B),
}
impl<A, B> Responder for Either<A, B>
where A: Responder, B: Responder
{
type Item = Reply;
type Error = Error;
fn respond_to(self, req: HttpRequest) -> Result<Reply, Error> {
match self {
Either::A(a) => match a.respond_to(req) {
Ok(val) => Ok(val.into()),
Err(err) => Err(err.into()),
},
Either::B(b) => match b.respond_to(req) {
Ok(val) => Ok(val.into()),
Err(err) => Err(err.into()),
},
}
}
}
#[doc(hidden)] #[doc(hidden)]
/// Convenience trait that convert `Future` object into `Boxed` future /// Convenience trait that convert `Future` object into `Boxed` future
pub trait AsyncResponder<I, E>: Sized { pub trait AsyncResponder<I, E>: Sized {

View File

@ -136,7 +136,7 @@ pub use application::Application;
pub use httpmessage::HttpMessage; pub use httpmessage::HttpMessage;
pub use httprequest::HttpRequest; pub use httprequest::HttpRequest;
pub use httpresponse::HttpResponse; pub use httpresponse::HttpResponse;
pub use handler::{Reply, Responder, NormalizePath, AsyncResponder}; pub use handler::{Either, Reply, Responder, NormalizePath, AsyncResponder};
pub use route::Route; pub use route::Route;
pub use resource::Resource; pub use resource::Resource;
pub use context::HttpContext; pub use context::HttpContext;