Use TitleCase for all 'content::' struct names.

This commit is contained in:
Sergio Benitez 2017-07-12 15:21:45 -07:00
parent 65886c8c4e
commit 897313730d
8 changed files with 28 additions and 28 deletions

View File

@ -111,7 +111,7 @@ impl<T: DeserializeOwned> FromData for Json<T> {
impl<T: Serialize> Responder<'static> for Json<T> {
fn respond_to(self, req: &Request) -> response::Result<'static> {
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| {
error_!("JSON failed to serialize: {:?}", e);
Status::InternalServerError

View File

@ -21,22 +21,22 @@ struct Person {
// preferred media type in the Accept header is matched against the `format` in
// the route attribute.
#[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.
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
// content type is matched against the `format` in the route attribute.
#[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, };
content::JSON(serde_json::to_string(&person).unwrap())
content::Json(serde_json::to_string(&person).unwrap())
}
#[error(404)]
fn not_found(request: &Request) -> content::HTML<String> {
fn not_found(request: &Request) -> content::Html<String> {
let html = match request.format() {
Some(ref mt) if !mt.is_json() && !mt.is_plain() => {
format!("<p>'{}' requests are not supported.</p>", mt)
@ -46,7 +46,7 @@ fn not_found(request: &Request) -> content::HTML<String> {
request.uri())
};
content::HTML(html)
content::Html(html)
}
fn main() {

View File

@ -13,8 +13,8 @@ fn hello(name: String, age: i8) -> String {
}
#[error(404)]
fn not_found(req: &rocket::Request) -> content::HTML<String> {
content::HTML(format!("<p>Sorry, but '{}' is not a valid path!</p>
fn not_found(req: &rocket::Request) -> content::Html<String> {
content::Html(format!("<p>Sorry, but '{}' is not a valid path!</p>
<p>Try visiting /hello/&lt;name&gt;/&lt;age&gt; instead.</p>",
req.uri()))
}

View File

@ -13,11 +13,11 @@ use rocket::response::content;
struct HitCount(AtomicUsize);
#[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);
let msg = "Your visit has been recorded!";
let count = format!("Visits: {}", count(hit_count));
content::HTML(format!("{}<br /><br />{}", msg, count))
content::Html(format!("{}<br /><br />{}", msg, count))
}
#[get("/count")]

View File

@ -158,7 +158,7 @@ macro_rules! default_errors {
$(
fn $fn_name<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
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)
}

View File

@ -10,16 +10,16 @@
//!
//! # 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
//! 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.
//!
//! ```rust
//! use rocket::response::content;
//!
//! # #[allow(unused_variables)]
//! let response = content::HTML("<h1>Hello, world!</h1>");
//! let response = content::Html("<h1>Hello, world!</h1>");
//! ```
use request::Request;
@ -57,7 +57,7 @@ impl<'r, R: Responder<'r>> Responder<'r> for Content<R> {
}
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=$name_str]
@ -73,7 +73,7 @@ macro_rules! ctrs {
/// remainder of the response to the wrapped responder.
impl<'r, R: Responder<'r>> Responder<'r> for $name<R> {
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! {
JSON: "JSON", "application/json",
XML: "XML", "text/xml",
MsgPack: "MessagePack", "application/msgpack",
HTML: "HTML", "text/html",
Plain: "plain text", "text/plain",
CSS: "CSS", "text/css",
JavaScript: "JavaScript", "application/javascript"
Json: JSON, "JSON", "application/json",
Xml: XML, "XML", "text/xml",
MsgPack: MsgPack, "MessagePack", "application/msgpack",
Html: HTML, "HTML", "text/html",
Plain: Plain, "plain text", "text/plain",
Css: CSS, "CSS", "text/css",
JavaScript: JavaScript, "JavaScript", "application/javascript"
}

View File

@ -16,8 +16,8 @@ fn index() -> &'static str {
}
#[head("/other")]
fn other() -> content::JSON<()> {
content::JSON(())
fn other() -> content::Json<()> {
content::Json(())
}
mod tests {

View File

@ -48,7 +48,7 @@ fn new(id: usize) -> status::Accepted<String> {
Similarly, the types in the [`content`
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
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
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
[`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