From 0d54b6f38e271e65401bb73eed75c13dcc6ec365 Mon Sep 17 00:00:00 2001 From: David McNeil Date: Mon, 11 Jun 2018 05:05:41 -0600 Subject: [PATCH] Implement Responder for Option #294 (#297) --- src/application.rs | 18 ++++++++++++++++++ src/handler.rs | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/application.rs b/src/application.rs index e16f85cd..b9fa3e32 100644 --- a/src/application.rs +++ b/src/application.rs @@ -737,6 +737,7 @@ impl Iterator for App { #[cfg(test)] mod tests { use super::*; + use body::{Body, Binary}; use http::StatusCode; use httprequest::HttpRequest; use httpresponse::HttpResponse; @@ -957,4 +958,21 @@ mod tests { let response = srv.execute(request.send()).unwrap(); assert_eq!(response.status(), StatusCode::NOT_FOUND); } + + #[test] + fn test_option_responder() { + let mut app = App::new() + .resource("/none", |r| r.f(|_| -> Option<&'static str> { None })) + .resource("/some", |r| r.f(|_| Some("some"))) + .finish(); + + let req = TestRequest::with_uri("/none").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND); + + let req = TestRequest::with_uri("/some").finish(); + let resp = app.run(req); + assert_eq!(resp.as_msg().status(), StatusCode::OK); + assert_eq!(resp.as_msg().body(), &Body::Binary(Binary::Slice(b"some"))); + } } diff --git a/src/handler.rs b/src/handler.rs index 8b550059..d330e071 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -5,6 +5,7 @@ use futures::future::{err, ok, Future}; use futures::{Async, Poll}; use error::Error; +use http::StatusCode; use httprequest::HttpRequest; use httpresponse::HttpResponse; @@ -132,6 +133,26 @@ where } } +impl Responder for Option +where + T: Responder, +{ + type Item = AsyncResult; + type Error = Error; + + fn respond_to( + self, req: &HttpRequest, + ) -> Result, Error> { + match self { + Some(t) => match t.respond_to(req) { + Ok(val) => Ok(val.into()), + Err(err) => Err(err.into()), + }, + None => Ok(req.build_response(StatusCode::NOT_FOUND).finish().into()), + } + } +} + /// Convenience trait that converts `Future` object to a `Boxed` future /// /// For example loading json from request's body is async operation.