1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
use {Response, Error}; use futures::Future; use futures::future::Then; use hyper::header::ContentType; pub trait Responder { fn respond(self) -> Response; } pub trait LiftError { type Future; fn lift_error(self) -> Self::Future; } impl<F, T, E> LiftError for F where F: Future<Item = T, Error = E> + 'static, T: Responder + 'static, E: Responder + 'static, Self: Sized + 'static, { type Future = Then<Self, Result<Response, Error>, fn(Result<T, E>) -> Result<Response, Error>>; fn lift_error(self) -> Self::Future { fn le<T, E>(res: Result<T, E>) -> Result<Response, Error> where T: Responder + 'static, E: Responder + 'static, { Ok(res.respond()) } self.then(le) } } impl Responder for String { fn respond(self) -> Response { Response::new() .with_header(ContentType::plaintext()) .with_body(self) } } impl Responder for &'static str { fn respond(self) -> Response { Response::new() .with_header(ContentType::plaintext()) .with_body(self) } } impl Responder for () { fn respond(self) -> Response { Response::new() } } impl<T, E> Responder for Result<T, E> where T: Responder, E: Responder, { fn respond(self) -> Response { match self { Ok(ok) => ok.respond(), Err(err) => err.respond(), } } } impl Responder for Response { fn respond(self) -> Response { self } }