Merge pull request #522 from hyperium/header-convenient-constructors
Header convenient constructors
This commit is contained in:
		| @@ -56,7 +56,6 @@ use std::io::Read; | |||||||
|  |  | ||||||
| use hyper::Client; | use hyper::Client; | ||||||
| use hyper::header::Connection; | use hyper::header::Connection; | ||||||
| use hyper::header::ConnectionOption; |  | ||||||
|  |  | ||||||
| fn main() { | fn main() { | ||||||
|     // Create a client. |     // Create a client. | ||||||
| @@ -65,7 +64,7 @@ fn main() { | |||||||
|     // Creating an outgoing request. |     // Creating an outgoing request. | ||||||
|     let mut res = client.get("http://www.gooogle.com/") |     let mut res = client.get("http://www.gooogle.com/") | ||||||
|         // set a header |         // set a header | ||||||
|         .header(Connection(vec![ConnectionOption::Close])) |         .header(Connection::close()) | ||||||
|         // let 'er go! |         // let 'er go! | ||||||
|         .send().unwrap(); |         .send().unwrap(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -8,7 +8,6 @@ use std::io; | |||||||
|  |  | ||||||
| use hyper::Client; | use hyper::Client; | ||||||
| use hyper::header::Connection; | use hyper::header::Connection; | ||||||
| use hyper::header::ConnectionOption::Close; |  | ||||||
|  |  | ||||||
| fn main() { | fn main() { | ||||||
|     env_logger::init().unwrap(); |     env_logger::init().unwrap(); | ||||||
| @@ -24,7 +23,7 @@ fn main() { | |||||||
|     let mut client = Client::new(); |     let mut client = Client::new(); | ||||||
|  |  | ||||||
|     let mut res = client.get(&*url) |     let mut res = client.get(&*url) | ||||||
|         .header(Connection(vec![Close])) |         .header(Connection::close()) | ||||||
|         .send().unwrap(); |         .send().unwrap(); | ||||||
|  |  | ||||||
|     println!("Response: {}", res.status); |     println!("Response: {}", res.status); | ||||||
|   | |||||||
| @@ -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!(close, Connection, { vec![b"close".to_vec()] }); | ||||||
| bench_header!(keep_alive, Connection, { vec![b"keep-alive".to_vec()] }); | bench_header!(keep_alive, Connection, { vec![b"keep-alive".to_vec()] }); | ||||||
| bench_header!(header, Connection, { vec![b"authorization".to_vec()] }); | bench_header!(header, Connection, { vec![b"authorization".to_vec()] }); | ||||||
|   | |||||||
| @@ -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()] }); | bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] }); | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/lib.rs
									
									
									
									
									
								
							| @@ -139,6 +139,9 @@ extern crate num_cpus; | |||||||
| extern crate traitobject; | extern crate traitobject; | ||||||
| extern crate typeable; | extern crate typeable; | ||||||
|  |  | ||||||
|  | #[macro_use] | ||||||
|  | extern crate mime as mime_crate; | ||||||
|  |  | ||||||
| #[macro_use] | #[macro_use] | ||||||
| extern crate log; | extern crate log; | ||||||
|  |  | ||||||
| @@ -146,7 +149,6 @@ extern crate log; | |||||||
| extern crate test; | extern crate test; | ||||||
|  |  | ||||||
|  |  | ||||||
| pub use mimewrapper::mime; |  | ||||||
| pub use url::Url; | pub use url::Url; | ||||||
| pub use client::Client; | pub use client::Client; | ||||||
| pub use error::{Result, Error}; | pub use error::{Result, Error}; | ||||||
| @@ -185,9 +187,9 @@ pub mod uri; | |||||||
| pub mod version; | pub mod version; | ||||||
|  |  | ||||||
|  |  | ||||||
| mod mimewrapper { | /// Re-exporting the mime crate, for convenience. | ||||||
|     /// Re-exporting the mime crate, for convenience. | pub mod mime { | ||||||
|     extern crate mime; |     pub use mime_crate::*; | ||||||
| } | } | ||||||
|  |  | ||||||
| #[allow(unconditional_recursion)] | #[allow(unconditional_recursion)] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user