mirror of https://github.com/rwf2/Rocket.git
Remove 'Content' in favor of 'content::Custom'.
Also adds the shorthand '(ContentType, R)', where 'R: Responder', responder implementation. This brings it to parity with the 'response::status' API.
This commit is contained in:
parent
8d40450f36
commit
d21608ca7b
|
@ -140,7 +140,7 @@ use std::error::Error;
|
|||
use rocket::{Rocket, Orbit, Ignite, Sentinel};
|
||||
use rocket::request::Request;
|
||||
use rocket::fairing::Fairing;
|
||||
use rocket::response::{self, Content, Responder};
|
||||
use rocket::response::{self, Responder};
|
||||
use rocket::http::{ContentType, Status};
|
||||
|
||||
const DEFAULT_TEMPLATE_DIR: &str = "templates";
|
||||
|
@ -430,7 +430,7 @@ impl<'r> Responder<'r, 'static> for Template {
|
|||
self.finalize(&ctxt)?
|
||||
};
|
||||
|
||||
Content(content_type, render).respond_to(req)
|
||||
(content_type, render).respond_to(req)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,21 @@
|
|||
//! remainder of the response to the wrapped responder. This allows for setting
|
||||
//! the Content-Type of a type that doesn't set it itself or for overriding one
|
||||
//! that does.
|
||||
//!
|
||||
//! The [`Custom`] type allows responding with _any_ `Content-Type`. As a
|
||||
//! convenience, `(ContentType, R)` where `R: Responder` is _also_ a
|
||||
//! `Responder`, identical to `Custom`.
|
||||
//!
|
||||
//! ```rust
|
||||
//! # use rocket::get;
|
||||
//! use rocket::http::ContentType;
|
||||
//!
|
||||
//! #[get("/")]
|
||||
//! fn index() -> (ContentType, &'static str) {
|
||||
//! (ContentType::HTML, "Is this HTML? <p>Sure, why not!</p>")
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
|
@ -35,18 +50,18 @@ use crate::http::ContentType;
|
|||
/// Set the Content-Type of a string to PDF.
|
||||
///
|
||||
/// ```rust
|
||||
/// use rocket::response::content::Content;
|
||||
/// use rocket::response::content::Custom;
|
||||
/// use rocket::http::ContentType;
|
||||
///
|
||||
/// # #[allow(unused_variables)]
|
||||
/// let response = Content(ContentType::PDF, "Hi.");
|
||||
/// let response = Custom(ContentType::PDF, "Hi.");
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Content<R>(pub ContentType, pub R);
|
||||
pub struct Custom<R>(pub ContentType, pub R);
|
||||
|
||||
/// Overrides the Content-Type of the response to the wrapped `ContentType` then
|
||||
/// delegates the remainder of the response to the wrapped responder.
|
||||
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Content<R> {
|
||||
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Custom<R> {
|
||||
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
|
||||
Response::build()
|
||||
.merge(self.1.respond_to(req)?)
|
||||
|
@ -72,7 +87,7 @@ macro_rules! ctrs {
|
|||
/// remainder of the response to the wrapped responder.
|
||||
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for $name<R> {
|
||||
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
|
||||
Content(ContentType::$ct, self.0).respond_to(req)
|
||||
Custom(ContentType::$ct, self.0).respond_to(req)
|
||||
}
|
||||
}
|
||||
)+
|
||||
|
@ -89,3 +104,10 @@ ctrs! {
|
|||
Css: CSS, "CSS", "text/css",
|
||||
JavaScript: JavaScript, "JavaScript", "application/javascript"
|
||||
}
|
||||
|
||||
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for (ContentType, R) {
|
||||
#[inline(always)]
|
||||
fn respond_to(self, request: &'r Request<'_>) -> response::Result<'o> {
|
||||
Custom(self.0, self.1).respond_to(request)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,6 @@ pub use self::flash::Flash;
|
|||
pub use self::named_file::NamedFile;
|
||||
pub use self::stream::Stream;
|
||||
pub use self::debug::Debug;
|
||||
#[doc(inline)] pub use self::content::Content;
|
||||
|
||||
/// Type alias for the `Result` of a [`Responder::respond_to()`] call.
|
||||
pub type Result<'r> = std::result::Result<Response<'r>, crate::http::Status>;
|
||||
|
|
Loading…
Reference in New Issue