use hyper::server::{Handler, Request, Response};
use super::Multipart;
pub struct Switch<H, M> {
normal: H,
multipart: M,
}
impl<H, M> Switch<H, M> where H: Handler, M: MultipartHandler {
pub fn new(normal: H, multipart: M) -> Switch<H, M> {
Switch {
normal: normal,
multipart: multipart,
}
}
}
impl<H, M> Handler for Switch<H, M> where H: Handler, M: MultipartHandler {
fn handle(&self, req: Request, res: Response) {
match Multipart::from_request(req) {
Ok(multi) => self.multipart.handle_multipart(multi, res),
Err(req) => self.normal.handle(req, res),
}
}
}
pub trait MultipartHandler: Send + Sync {
fn handle_multipart<'a>(&self, multipart: Multipart<'a>, response: Response);
}
impl<F> MultipartHandler for F where F: for<'a> Fn(Multipart<'a>, Response) + Send + Sync {
fn handle_multipart<'a>(&self, multipart: Multipart<'a>, response: Response) {
(*self)(multipart, response);
}
}
pub struct UnboxedHandler<F> {
pub f: F,
}
impl<F> Handler for UnboxedHandler<F> where F: Fn(Request, Response) + Send + Sync {
fn handle(&self, req: Request, res: Response) {
(self.f)(req, res);
}
}