From a6dd9c847660c776e77dc47e2691a4c6c8d1b6c6 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 11 May 2015 18:48:00 -0700 Subject: [PATCH 1/3] refactor(lib): macro_use the mime! macro --- src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index edcba65629..595a33afd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -139,6 +139,9 @@ extern crate num_cpus; extern crate traitobject; extern crate typeable; +#[macro_use] +extern crate mime as mime_crate; + #[macro_use] extern crate log; @@ -146,7 +149,6 @@ extern crate log; extern crate test; -pub use mimewrapper::mime; pub use url::Url; pub use client::Client; pub use error::{Result, Error}; @@ -185,9 +187,9 @@ pub mod uri; pub mod version; -mod mimewrapper { - /// Re-exporting the mime crate, for convenience. - extern crate mime; +/// Re-exporting the mime crate, for convenience. +pub mod mime { + pub use mime_crate::*; } #[allow(unconditional_recursion)] From c2938fb45f9c1fff2a1235d82b7741531de21445 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 11 May 2015 18:48:33 -0700 Subject: [PATCH 2/3] feat(header): add Connection::close() and ::keep_alive() constructors --- README.md | 3 +-- examples/client.rs | 3 +-- src/header/common/connection.rs | 12 ++++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 82b6a98087..da4e3e5de5 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,6 @@ use std::io::Read; use hyper::Client; use hyper::header::Connection; -use hyper::header::ConnectionOption; fn main() { // Create a client. @@ -65,7 +64,7 @@ fn main() { // Creating an outgoing request. let mut res = client.get("http://www.gooogle.com/") // set a header - .header(Connection(vec![ConnectionOption::Close])) + .header(Connection::close()) // let 'er go! .send().unwrap(); diff --git a/examples/client.rs b/examples/client.rs index ba638c66dd..daca4c27d0 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -8,7 +8,6 @@ use std::io; use hyper::Client; use hyper::header::Connection; -use hyper::header::ConnectionOption::Close; fn main() { env_logger::init().unwrap(); @@ -24,7 +23,7 @@ fn main() { let mut client = Client::new(); let mut res = client.get(&*url) - .header(Connection(vec![Close])) + .header(Connection::close()) .send().unwrap(); println!("Response: {}", res.status); diff --git a/src/header/common/connection.rs b/src/header/common/connection.rs index c4ad673db5..cf544e9cf6 100644 --- a/src/header/common/connection.rs +++ b/src/header/common/connection.rs @@ -71,6 +71,18 @@ header! { } } +impl Connection { + /// A constructor to easily create a `Connection: close` header. + pub fn close() -> Connection { + Connection(vec![ConnectionOption::Close]) + } + + /// A constructor to easily create a `Connection: keep-alive` header. + pub fn keep_alive() -> Connection { + Connection(vec![ConnectionOption::KeepAlive]) + } +} + bench_header!(close, Connection, { vec![b"close".to_vec()] }); bench_header!(keep_alive, Connection, { vec![b"keep-alive".to_vec()] }); bench_header!(header, Connection, { vec![b"authorization".to_vec()] }); From b6114ecd2e65bd59e79a67a45913adaf0f1552f0 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 11 May 2015 18:49:19 -0700 Subject: [PATCH 3/3] feat(header): add ContentType::json(), plaintext(), html(), jpeg(), and png() constructors --- src/header/common/content_type.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/header/common/content_type.rs b/src/header/common/content_type.rs index d88273595b..1d9e284178 100644 --- a/src/header/common/content_type.rs +++ b/src/header/common/content_type.rs @@ -34,4 +34,30 @@ header! { } } +impl ContentType { + /// A constructor to easily create a `Content-Type: application/json; charset=utf-8` header. + pub fn json() -> ContentType { + ContentType(mime!(Application/Json; Charset=Utf8)) + } + + /// A constructor to easily create a `Content-Type: text/plain; charset=utf-8` header. + pub fn plaintext() -> ContentType { + ContentType(mime!(Text/Plain; Charset=Utf8)) + } + + /// A constructor to easily create a `Content-Type: text/html; charset=utf-8` header. + pub fn html() -> ContentType { + ContentType(mime!(Text/Html; Charset=Utf8)) + } + + /// A constructor to easily create a `Content-Type: image/jpeg` header. + pub fn jpeg() -> ContentType { + ContentType(mime!(Image/Jpeg)) + } + + /// A constructor to easily create a `Content-Type: image/png` header. + pub fn png() -> ContentType { + ContentType(mime!(Image/Png)) + } +} bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] });