diff --git a/src/handler.rs b/src/handler.rs index 4aa5ec5b4..f529fd04c 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -34,6 +34,36 @@ pub trait Responder { fn respond_to(self, req: HttpRequest) -> Result; } +/// Combines two different responders types into a single type. +#[derive(Debug)] +pub enum Either { + /// First branch of the type + A(A), + /// Second branch of the type + B(B), +} + +impl Responder for Either + where A: Responder, B: Responder +{ + type Item = Reply; + type Error = Error; + + fn respond_to(self, req: HttpRequest) -> Result { + 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)] /// Convenience trait that convert `Future` object into `Boxed` future pub trait AsyncResponder: Sized { diff --git a/src/lib.rs b/src/lib.rs index f89549377..93cfe2662 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,7 +136,7 @@ pub use application::Application; pub use httpmessage::HttpMessage; pub use httprequest::HttpRequest; 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 resource::Resource; pub use context::HttpContext;