Rename data_type to data and don't export inner types.

This commit is contained in:
Sergio Benitez 2016-09-20 20:02:33 -07:00
parent 96ee1b10d4
commit 47edc65d34
7 changed files with 18 additions and 20 deletions

View File

@ -4,8 +4,8 @@ extern crate serde_json;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use rocket::request::{Request, FromRequest}; use rocket::request::{Request, FromRequest};
use rocket::response::{header, Responder, Outcome, FreshHyperResponse}; use rocket::response::{Responder, Outcome, FreshHyperResponse};
use rocket::response::mime::{Mime, TopLevel, SubLevel}; use rocket::response::data;
use self::serde::{Serialize, Deserialize}; use self::serde::{Serialize, Deserialize};
use self::serde_json::Error as JSONError; use self::serde_json::Error as JSONError;
@ -69,11 +69,9 @@ impl<'r, 'c, T: Deserialize> FromRequest<'r, 'c> for JSON<T> {
} }
impl<T: Serialize> Responder for JSON<T> { impl<T: Serialize> Responder for JSON<T> {
fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a> { fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a> {
let mime = Mime(TopLevel::Application, SubLevel::Json, vec![]);
res.headers_mut().set(header::ContentType(mime));
match serde_json::to_string(&self.0) { match serde_json::to_string(&self.0) {
Ok(mut json_string) => json_string.respond(res), Ok(json_string) => data::JSON(json_string).respond(res),
Err(e) => { Err(e) => {
error_!("JSON failed to serialize: {:?}", e); error_!("JSON failed to serialize: {:?}", e);
Outcome::FailStop Outcome::FailStop
@ -128,4 +126,3 @@ macro_rules! map {
map!($($key => $value),+) map!($($key => $value),+)
}; };
} }

View File

@ -5,7 +5,7 @@ extern crate rocket;
extern crate serde_json; extern crate serde_json;
use rocket::{Rocket, Request, Error, ContentType}; use rocket::{Rocket, Request, Error, ContentType};
use rocket::response::JSON; use rocket::response::data;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Person { struct Person {
@ -14,14 +14,14 @@ struct Person {
} }
#[get("/<name>/<age>", format = "application/json")] #[get("/<name>/<age>", format = "application/json")]
fn hello(content_type: ContentType, name: String, age: i8) -> JSON<String> { fn hello(content_type: ContentType, name: String, age: i8) -> data::JSON<String> {
let person = Person { let person = Person {
name: name, name: name,
age: age, age: age,
}; };
println!("ContentType: {}", content_type); println!("ContentType: {}", content_type);
JSON(serde_json::to_string(&person).unwrap()) data::JSON(serde_json::to_string(&person).unwrap())
} }
#[error(404)] #[error(404)]

View File

@ -4,15 +4,15 @@
extern crate rocket; extern crate rocket;
use rocket::Rocket; use rocket::Rocket;
use rocket::response::{Plain, Stream}; use rocket::response::{data, Stream};
use std::io::{repeat, Repeat, Read, Take}; use std::io::{repeat, Repeat, Read, Take};
type LimitedRepeat = Take<Repeat>; type LimitedRepeat = Take<Repeat>;
#[get("/")] #[get("/")]
fn root() -> Plain<Stream<LimitedRepeat>> { fn root() -> data::Plain<Stream<LimitedRepeat>> {
Plain(Stream::from(repeat('a' as u8).take(25000))) data::Plain(Stream::from(repeat('a' as u8).take(25000)))
} }
fn main() { fn main() {

View File

@ -54,13 +54,14 @@ impl fmt::Display for Catcher {
pub mod defaults { pub mod defaults {
use request::Request; use request::Request;
use response::{StatusCode, Response, HTML}; use response::{StatusCode, Response};
use response::data;
use super::Catcher; use super::Catcher;
use error::Error; use error::Error;
use std::collections::HashMap; use std::collections::HashMap;
pub fn not_found<'r>(_error: Error, _request: &'r Request<'r>) -> Response<'r> { pub fn not_found<'r>(_error: Error, _request: &'r Request<'r>) -> Response<'r> {
Response::with_status(StatusCode::NotFound, HTML(r#" Response::with_status(StatusCode::NotFound, data::HTML(r#"
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@ -82,7 +83,7 @@ pub mod defaults {
pub fn internal_error<'r>(_error: Error, pub fn internal_error<'r>(_error: Error,
_request: &'r Request<'r>) _request: &'r Request<'r>)
-> Response<'r> { -> Response<'r> {
Response::with_status(StatusCode::InternalServerError, HTML(r#" Response::with_status(StatusCode::InternalServerError, data::HTML(r#"
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>

View File

@ -32,7 +32,7 @@ impl<R: Responder> Flash<R> {
pub fn cookie_pair(&self) -> CookiePair { pub fn cookie_pair(&self) -> CookiePair {
let content = format!("{}{}{}", self.name.len(), self.name, self.message); let content = format!("{}{}{}", self.name.len(), self.name, self.message);
let mut pair = CookiePair::new("flash".to_string(), content); let mut pair = CookiePair::new("_flash".to_string(), content);
pair.path = Some("/".to_string()); pair.path = Some("/".to_string());
pair.max_age = Some(300); pair.max_age = Some(300);
pair pair
@ -78,7 +78,7 @@ impl<'r, 'c> FromRequest<'r, 'c> for Flash<()> {
fn from_request(request: &'r Request<'c>) -> Result<Self, Self::Error> { fn from_request(request: &'r Request<'c>) -> Result<Self, Self::Error> {
trace_!("Flash: attemping to retrieve message."); trace_!("Flash: attemping to retrieve message.");
request.cookies().find("flash").ok_or(()).and_then(|cookie| { request.cookies().find("_flash").ok_or(()).and_then(|cookie| {
// Clear the flash message. // Clear the flash message.
trace_!("Flash: retrieving message: {:?}", cookie); trace_!("Flash: retrieving message: {:?}", cookie);
request.cookies().remove("flash"); request.cookies().remove("flash");

View File

@ -4,17 +4,17 @@ mod redirect;
mod with_status; mod with_status;
mod outcome; mod outcome;
mod flash; mod flash;
mod data_type;
mod named_file; mod named_file;
mod stream; mod stream;
pub mod data;
pub use hyper::server::Response as HyperResponse; pub use hyper::server::Response as HyperResponse;
pub use hyper::net::Fresh as HyperFresh; pub use hyper::net::Fresh as HyperFresh;
pub use hyper::status::StatusCode; pub use hyper::status::StatusCode;
pub use hyper::header; pub use hyper::header;
pub use hyper::mime; pub use hyper::mime;
pub use self::data_type::*;
pub use self::responder::Responder; pub use self::responder::Responder;
pub use self::empty::{Empty, Forward}; pub use self::empty::{Empty, Forward};
pub use self::redirect::Redirect; pub use self::redirect::Redirect;