From 462f1f9298cb3fb5d1f5b3e0352fe0630590c794 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 4 Jul 2017 02:43:00 -0700 Subject: [PATCH] Add a 'status::NotFound' responder. --- CHANGELOG.md | 3 +++ lib/src/response/status.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4af4095a..5eb2ee1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ This release includes the following new features: * Log coloring is disabled when `stdout` is not a TTY. * [`FromForm`] is implemented for `Option`, `Result`. + * The [`NotFound`] responder was added for simple **404** response + construction. [Fairings]: #FIXME [Native TLS support]: #FIXME @@ -73,6 +75,7 @@ This release includes the following new features: [`Config::get_datetime()`]: https://api.rocket.rs/rocket/struct.Config.html#method.get_datetime [`LenientForm`]: https://api.rocket.rs/rocket/request/struct.LenientForm.html [configuration parameters]: https://api.rocket.rs/rocket/config/index.html#environment-variables +[`NotFound`]: https://api.rocket.rs/rocket/response/status/struct.NotFound.html ## Breaking Changes diff --git a/lib/src/response/status.rs b/lib/src/response/status.rs index bed23c4e..f09ce744 100644 --- a/lib/src/response/status.rs +++ b/lib/src/response/status.rs @@ -155,6 +155,30 @@ impl Responder<'static> for Reset { } } +/// Sets the status of the response to 404 (Not Found). +/// +/// The remainder of the response is delegated to the wrapped `Responder`. +/// +/// # Example +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::NotFound("Sorry, I couldn't find it!"); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct NotFound(pub R); + +/// Sets the status code of the response to 404 Not Found. +impl<'r, R: Responder<'r>> Responder<'r> for NotFound { + fn respond_to(self, req: &Request) -> Result, Status> { + Response::build_from(self.0.respond_to(req)?) + .status(Status::NotFound) + .ok() + } +} + /// Creates a response with the given status code and underyling responder. /// /// # Example