mirror of https://github.com/rwf2/Rocket.git
Use TitleCase for all 'content::' struct names.
This commit is contained in:
parent
65886c8c4e
commit
897313730d
|
@ -111,7 +111,7 @@ impl<T: DeserializeOwned> FromData for Json<T> {
|
||||||
impl<T: Serialize> Responder<'static> for Json<T> {
|
impl<T: Serialize> Responder<'static> for Json<T> {
|
||||||
fn respond_to(self, req: &Request) -> response::Result<'static> {
|
fn respond_to(self, req: &Request) -> response::Result<'static> {
|
||||||
serde_json::to_string(&self.0).map(|string| {
|
serde_json::to_string(&self.0).map(|string| {
|
||||||
content::JSON(string).respond_to(req).unwrap()
|
content::Json(string).respond_to(req).unwrap()
|
||||||
}).map_err(|e| {
|
}).map_err(|e| {
|
||||||
error_!("JSON failed to serialize: {:?}", e);
|
error_!("JSON failed to serialize: {:?}", e);
|
||||||
Status::InternalServerError
|
Status::InternalServerError
|
||||||
|
|
|
@ -21,22 +21,22 @@ struct Person {
|
||||||
// preferred media type in the Accept header is matched against the `format` in
|
// preferred media type in the Accept header is matched against the `format` in
|
||||||
// the route attribute.
|
// the route attribute.
|
||||||
#[get("/<name>/<age>", format = "application/json")]
|
#[get("/<name>/<age>", format = "application/json")]
|
||||||
fn get_hello(name: String, age: u8) -> content::JSON<String> {
|
fn get_hello(name: String, age: u8) -> content::Json<String> {
|
||||||
// In a real application, we'd use the JSON contrib type.
|
// In a real application, we'd use the JSON contrib type.
|
||||||
let person = Person { name: name, age: age, };
|
let person = Person { name: name, age: age, };
|
||||||
content::JSON(serde_json::to_string(&person).unwrap())
|
content::Json(serde_json::to_string(&person).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
// In a `POST` request and all other payload supporting request types, the
|
// In a `POST` request and all other payload supporting request types, the
|
||||||
// content type is matched against the `format` in the route attribute.
|
// content type is matched against the `format` in the route attribute.
|
||||||
#[post("/<age>", format = "text/plain", data = "<name>")]
|
#[post("/<age>", format = "text/plain", data = "<name>")]
|
||||||
fn post_hello(age: u8, name: String) -> content::JSON<String> {
|
fn post_hello(age: u8, name: String) -> content::Json<String> {
|
||||||
let person = Person { name: name, age: age, };
|
let person = Person { name: name, age: age, };
|
||||||
content::JSON(serde_json::to_string(&person).unwrap())
|
content::Json(serde_json::to_string(&person).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[error(404)]
|
#[error(404)]
|
||||||
fn not_found(request: &Request) -> content::HTML<String> {
|
fn not_found(request: &Request) -> content::Html<String> {
|
||||||
let html = match request.format() {
|
let html = match request.format() {
|
||||||
Some(ref mt) if !mt.is_json() && !mt.is_plain() => {
|
Some(ref mt) if !mt.is_json() && !mt.is_plain() => {
|
||||||
format!("<p>'{}' requests are not supported.</p>", mt)
|
format!("<p>'{}' requests are not supported.</p>", mt)
|
||||||
|
@ -46,7 +46,7 @@ fn not_found(request: &Request) -> content::HTML<String> {
|
||||||
request.uri())
|
request.uri())
|
||||||
};
|
};
|
||||||
|
|
||||||
content::HTML(html)
|
content::Html(html)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -13,8 +13,8 @@ fn hello(name: String, age: i8) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[error(404)]
|
#[error(404)]
|
||||||
fn not_found(req: &rocket::Request) -> content::HTML<String> {
|
fn not_found(req: &rocket::Request) -> content::Html<String> {
|
||||||
content::HTML(format!("<p>Sorry, but '{}' is not a valid path!</p>
|
content::Html(format!("<p>Sorry, but '{}' is not a valid path!</p>
|
||||||
<p>Try visiting /hello/<name>/<age> instead.</p>",
|
<p>Try visiting /hello/<name>/<age> instead.</p>",
|
||||||
req.uri()))
|
req.uri()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,11 @@ use rocket::response::content;
|
||||||
struct HitCount(AtomicUsize);
|
struct HitCount(AtomicUsize);
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index(hit_count: State<HitCount>) -> content::HTML<String> {
|
fn index(hit_count: State<HitCount>) -> content::Html<String> {
|
||||||
hit_count.0.fetch_add(1, Ordering::Relaxed);
|
hit_count.0.fetch_add(1, Ordering::Relaxed);
|
||||||
let msg = "Your visit has been recorded!";
|
let msg = "Your visit has been recorded!";
|
||||||
let count = format!("Visits: {}", count(hit_count));
|
let count = format!("Visits: {}", count(hit_count));
|
||||||
content::HTML(format!("{}<br /><br />{}", msg, count))
|
content::Html(format!("{}<br /><br />{}", msg, count))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/count")]
|
#[get("/count")]
|
||||||
|
|
|
@ -158,7 +158,7 @@ macro_rules! default_errors {
|
||||||
$(
|
$(
|
||||||
fn $fn_name<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
|
fn $fn_name<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
|
||||||
status::Custom(Status::from_code($code).unwrap(),
|
status::Custom(Status::from_code($code).unwrap(),
|
||||||
content::HTML(error_page_template!($code, $name, $description))
|
content::Html(error_page_template!($code, $name, $description))
|
||||||
).respond_to(req)
|
).respond_to(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,16 +10,16 @@
|
||||||
//!
|
//!
|
||||||
//! # Example
|
//! # Example
|
||||||
//!
|
//!
|
||||||
//! The following snippet creates an `HTML` content response for a string.
|
//! The following snippet creates an `Html` content response for a string.
|
||||||
//! Normally, raw strings set their response Content-Type to `text/plain`. By
|
//! Normally, raw strings set their response Content-Type to `text/plain`. By
|
||||||
//! using the `HTML` content response, the Content-Type will be set to
|
//! using the `Html` content response, the Content-Type will be set to
|
||||||
//! `text/html` instead.
|
//! `text/html` instead.
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use rocket::response::content;
|
//! use rocket::response::content;
|
||||||
//!
|
//!
|
||||||
//! # #[allow(unused_variables)]
|
//! # #[allow(unused_variables)]
|
||||||
//! let response = content::HTML("<h1>Hello, world!</h1>");
|
//! let response = content::Html("<h1>Hello, world!</h1>");
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use request::Request;
|
use request::Request;
|
||||||
|
@ -57,7 +57,7 @@ impl<'r, R: Responder<'r>> Responder<'r> for Content<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! ctrs {
|
macro_rules! ctrs {
|
||||||
($($name:ident: $name_str:expr, $ct_str:expr),+) => {
|
($($name:ident: $ct:ident, $name_str:expr, $ct_str:expr),+) => {
|
||||||
$(
|
$(
|
||||||
#[doc="Override the `Content-Type` of the response to <b>"]
|
#[doc="Override the `Content-Type` of the response to <b>"]
|
||||||
#[doc=$name_str]
|
#[doc=$name_str]
|
||||||
|
@ -73,7 +73,7 @@ macro_rules! ctrs {
|
||||||
/// remainder of the response to the wrapped responder.
|
/// remainder of the response to the wrapped responder.
|
||||||
impl<'r, R: Responder<'r>> Responder<'r> for $name<R> {
|
impl<'r, R: Responder<'r>> Responder<'r> for $name<R> {
|
||||||
fn respond_to(self, req: &Request) -> Result<Response<'r>, Status> {
|
fn respond_to(self, req: &Request) -> Result<Response<'r>, Status> {
|
||||||
Content(ContentType::$name, self.0).respond_to(req)
|
Content(ContentType::$ct, self.0).respond_to(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)+
|
)+
|
||||||
|
@ -81,12 +81,12 @@ macro_rules! ctrs {
|
||||||
}
|
}
|
||||||
|
|
||||||
ctrs! {
|
ctrs! {
|
||||||
JSON: "JSON", "application/json",
|
Json: JSON, "JSON", "application/json",
|
||||||
XML: "XML", "text/xml",
|
Xml: XML, "XML", "text/xml",
|
||||||
MsgPack: "MessagePack", "application/msgpack",
|
MsgPack: MsgPack, "MessagePack", "application/msgpack",
|
||||||
HTML: "HTML", "text/html",
|
Html: HTML, "HTML", "text/html",
|
||||||
Plain: "plain text", "text/plain",
|
Plain: Plain, "plain text", "text/plain",
|
||||||
CSS: "CSS", "text/css",
|
Css: CSS, "CSS", "text/css",
|
||||||
JavaScript: "JavaScript", "application/javascript"
|
JavaScript: JavaScript, "JavaScript", "application/javascript"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,8 @@ fn index() -> &'static str {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[head("/other")]
|
#[head("/other")]
|
||||||
fn other() -> content::JSON<()> {
|
fn other() -> content::Json<()> {
|
||||||
content::JSON(())
|
content::Json(())
|
||||||
}
|
}
|
||||||
|
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -48,7 +48,7 @@ fn new(id: usize) -> status::Accepted<String> {
|
||||||
Similarly, the types in the [`content`
|
Similarly, the types in the [`content`
|
||||||
module](https://api.rocket.rs/rocket/response/content/index.html) can be used to
|
module](https://api.rocket.rs/rocket/response/content/index.html) can be used to
|
||||||
override the Content-Type of a response. For instance, to set the Content-Type
|
override the Content-Type of a response. For instance, to set the Content-Type
|
||||||
an `&'static str` to JSON, you can use the [`content::JSON`] type as follows:
|
an `&'static str` to JSON, you can use the [`content::Json`] type as follows:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use rocket::response::content;
|
use rocket::response::content;
|
||||||
|
@ -60,7 +60,7 @@ fn json() -> content::Json<&'static str> {
|
||||||
```
|
```
|
||||||
|
|
||||||
[`Accepted`]: https://api.rocket.rs/rocket/response/status/struct.Accepted.html
|
[`Accepted`]: https://api.rocket.rs/rocket/response/status/struct.Accepted.html
|
||||||
[`content::JSON`]: https://api.rocket.rs/rocket/response/content/struct.Json.html
|
[`content::Json`]: https://api.rocket.rs/rocket/response/content/struct.Json.html
|
||||||
|
|
||||||
### Errors
|
### Errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue