diff --git a/.travis.yml b/.travis.yml index 6c2d1a7..c8b2c19 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ matrix: include: - rust: stable - os: osx - - rust: 1.19.0 + - rust: 1.20.0 script: cargo build - rust: beta diff --git a/README.md b/README.md index aa31c01..25ad628 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,11 @@ Create an HTTP response: ```rust extern crate http; -use http::Response; -use http::status; +use http::{Response, StatusCode}; fn main() { let response = Response::builder() - .status(status::MOVED_PERMANENTLY) + .status(StatusCode::MOVED_PERMANENTLY) .header("Location", "https://www.rust-lang.org/install.html") .body(()) .unwrap(); diff --git a/src/lib.rs b/src/lib.rs index ad076c7..286de17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,14 +53,13 @@ //! to edit the request/response: //! //! ``` -//! use http::Response; +//! use http::{Response, StatusCode}; //! use http::header::{CONTENT_TYPE, HeaderValue}; -//! use http::status; //! //! fn add_server_headers(response: &mut Response) { //! response.headers_mut() //! .insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); -//! *response.status_mut() = status::OK; +//! *response.status_mut() = StatusCode::OK; //! } //! ``` //! diff --git a/src/method.rs b/src/method.rs index ca1a4da..020827c 100644 --- a/src/method.rs +++ b/src/method.rs @@ -5,17 +5,14 @@ //! crate as `http::Method` and is intended for import through that location //! primarily. //! -//! This module also contains constants for a number of common HTTP methods such -//! as GET, POST, etc. -//! //! # Examples //! //! ``` -//! use http::{method, Method}; +//! use http::Method; //! -//! assert_eq!(method::GET, Method::from_bytes(b"GET").unwrap()); -//! assert!(method::GET.is_idempotent()); -//! assert_eq!(method::POST.as_str(), "POST"); +//! assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); +//! assert!(Method::GET.is_idempotent()); +//! assert_eq!(Method::POST.as_str(), "POST"); //! ``` use HttpTryFrom; @@ -27,6 +24,9 @@ use std::error::Error; /// The Request Method (VERB) /// +/// This type also contains constants for a number of common HTTP methods such +/// as GET, POST, etc. +/// /// Currently includes 8 variants representing the 8 methods defined in /// [RFC 7230](https://tools.ietf.org/html/rfc7231#section-4.1), plus PATCH, /// and an Extension variant for all extensions. @@ -34,11 +34,11 @@ use std::error::Error; /// # Examples /// /// ``` -/// use http::{method, Method}; +/// use http::Method; /// -/// assert_eq!(method::GET, Method::from_bytes(b"GET").unwrap()); -/// assert!(method::GET.is_idempotent()); -/// assert_eq!(method::POST.as_str(), "POST"); +/// assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); +/// assert!(Method::GET.is_idempotent()); +/// assert_eq!(Method::POST.as_str(), "POST"); /// ``` #[derive(Clone, PartialEq, Eq, Hash)] pub struct Method(Inner); @@ -110,34 +110,35 @@ const METHOD_CHARS: [u8; 256] = [ b'\0', b'\0', b'\0', b'\0', b'\0', b'\0' // 25x ]; -/// GET -pub const GET: Method = Method(Get); - -/// POST -pub const POST: Method = Method(Post); - -/// PUT -pub const PUT: Method = Method(Put); - -/// DELETE -pub const DELETE: Method = Method(Delete); - -/// HEAD -pub const HEAD: Method = Method(Head); - -/// OPTIONS -pub const OPTIONS: Method = Method(Options); - -/// CONNECT -pub const CONNECT: Method = Method(Connect); - -/// PATCH -pub const PATCH: Method = Method(Patch); - -/// TRACE -pub const TRACE: Method = Method(Trace); impl Method { + /// GET + pub const GET: Method = Method(Get); + + /// POST + pub const POST: Method = Method(Post); + + /// PUT + pub const PUT: Method = Method(Put); + + /// DELETE + pub const DELETE: Method = Method(Delete); + + /// HEAD + pub const HEAD: Method = Method(Head); + + /// OPTIONS + pub const OPTIONS: Method = Method(Options); + + /// CONNECT + pub const CONNECT: Method = Method(Connect); + + /// PATCH + pub const PATCH: Method = Method(Patch); + + /// TRACE + pub const TRACE: Method = Method(Trace); + /// Converts a slice of bytes to an HTTP method. pub fn from_bytes(src: &[u8]) -> Result { match src.len() { @@ -302,7 +303,7 @@ impl fmt::Display for Method { impl Default for Method { #[inline] fn default() -> Method { - GET + Method::GET } } diff --git a/src/request.rs b/src/request.rs index 9a2bb8d..3c0a4c3 100644 --- a/src/request.rs +++ b/src/request.rs @@ -35,13 +35,12 @@ //! Inspecting a request to see what was sent. //! //! ``` -//! use http::{Request, Response}; -//! use http::status::NOT_FOUND; +//! use http::{Request, Response, StatusCode}; //! //! fn respond_to(req: Request<()>) -> http::Result> { //! if req.uri() != "/awesome-url" { //! return Response::builder() -//! .status(NOT_FOUND) +//! .status(StatusCode::NOT_FOUND) //! .body(()) //! } //! @@ -98,13 +97,12 @@ use version::Version; /// Inspecting a request to see what was sent. /// /// ``` -/// use http::{Request, Response}; -/// use http::status::NOT_FOUND; +/// use http::{Request, Response, StatusCode}; /// /// fn respond_to(req: Request<()>) -> http::Result> { /// if req.uri() != "/awesome-url" { /// return Response::builder() -/// .status(NOT_FOUND) +/// .status(StatusCode::NOT_FOUND) /// .body(()) /// } /// @@ -228,7 +226,7 @@ impl Request { /// # use http::*; /// let request = Request::new("hello world"); /// - /// assert_eq!(*request.method(), method::GET); + /// assert_eq!(*request.method(), Method::GET); /// assert_eq!(*request.body(), "hello world"); /// ``` #[inline] @@ -247,7 +245,7 @@ impl Request { /// # use http::*; /// let request = Request::new("hello world"); /// let (mut parts, body) = request.into_parts(); - /// parts.method = method::POST; + /// parts.method = Method::POST; /// /// let request = Request::from_parts(parts, body); /// ``` @@ -266,7 +264,7 @@ impl Request { /// ``` /// # use http::*; /// let request: Request<()> = Request::default(); - /// assert_eq!(*request.method(), method::GET); + /// assert_eq!(*request.method(), Method::GET); /// ``` #[inline] pub fn method(&self) -> &Method { @@ -280,8 +278,8 @@ impl Request { /// ``` /// # use http::*; /// let mut request: Request<()> = Request::default(); - /// *request.method_mut() = method::PUT; - /// assert_eq!(*request.method(), method::PUT); + /// *request.method_mut() = Method::PUT; + /// assert_eq!(*request.method(), Method::PUT); /// ``` #[inline] pub fn method_mut(&mut self) -> &mut Method { @@ -324,7 +322,7 @@ impl Request { /// ``` /// # use http::*; /// let request: Request<()> = Request::default(); - /// assert_eq!(request.version(), version::HTTP_11); + /// assert_eq!(request.version(), Version::HTTP_11); /// ``` #[inline] pub fn version(&self) -> Version { @@ -338,8 +336,8 @@ impl Request { /// ``` /// # use http::*; /// let mut request: Request<()> = Request::default(); - /// *request.version_mut() = version::HTTP_2; - /// assert_eq!(request.version(), version::HTTP_2); + /// *request.version_mut() = Version::HTTP_2; + /// assert_eq!(request.version(), Version::HTTP_2); /// ``` #[inline] pub fn version_mut(&mut self) -> &mut Version { @@ -444,7 +442,7 @@ impl Request { /// # use http::*; /// let request = Request::new(()); /// let (parts, body) = request.into_parts(); - /// assert_eq!(parts.method, method::GET); + /// assert_eq!(parts.method, Method::GET); /// ``` #[inline] pub fn into_parts(self) -> (Parts, T) { @@ -586,10 +584,9 @@ impl Builder { /// /// ``` /// # use http::*; - /// use http::version::HTTP_2; /// /// let req = Request::builder() - /// .version(HTTP_2) + /// .version(Version::HTTP_2) /// .body(()) /// .unwrap(); /// ``` diff --git a/src/response.rs b/src/response.rs index 1a884e8..29ca208 100644 --- a/src/response.rs +++ b/src/response.rs @@ -10,13 +10,12 @@ //! Creating a `Response` to return //! //! ``` -//! use http::{Request, Response}; -//! use http::status::OK; +//! use http::{Request, Response, StatusCode}; //! //! fn respond_to(req: Request<()>) -> http::Result> { //! let mut response = Response::builder(); //! response.header("Foo", "Bar") -//! .status(OK); +//! .status(StatusCode::OK); //! //! if req.headers().contains_key("Another-Header") { //! response.header("Another-Header", "Ack"); @@ -29,12 +28,11 @@ //! A simple 404 handler //! //! ``` -//! use http::{Request, Response}; -//! use http::status::NOT_FOUND; +//! use http::{Request, Response, StatusCode}; //! //! fn not_found(_req: Request<()>) -> http::Result> { //! Response::builder() -//! .status(NOT_FOUND) +//! .status(StatusCode::NOT_FOUND) //! .body(()) //! } //! ``` @@ -43,7 +41,6 @@ //! //! ```no_run //! use http::{Request, Response}; -//! use http::status::NOT_FOUND; //! //! fn get(url: &str) -> http::Result> { //! // ... @@ -88,13 +85,12 @@ use version::Version; /// Creating a `Response` to return /// /// ``` -/// use http::{Request, Response}; -/// use http::status::OK; +/// use http::{Request, Response, StatusCode}; /// /// fn respond_to(req: Request<()>) -> http::Result> { /// let mut response = Response::builder(); /// response.header("Foo", "Bar") -/// .status(OK); +/// .status(StatusCode::OK); /// /// if req.headers().contains_key("Another-Header") { /// response.header("Another-Header", "Ack"); @@ -107,12 +103,11 @@ use version::Version; /// A simple 404 handler /// /// ``` -/// use http::{Request, Response}; -/// use http::status::NOT_FOUND; +/// use http::{Request, Response, StatusCode}; /// /// fn not_found(_req: Request<()>) -> http::Result> { /// Response::builder() -/// .status(NOT_FOUND) +/// .status(StatusCode::NOT_FOUND) /// .body(()) /// } /// ``` @@ -121,7 +116,6 @@ use version::Version; /// /// ```no_run /// use http::{Request, Response}; -/// use http::status::NOT_FOUND; /// /// fn get(url: &str) -> http::Result> { /// // ... @@ -250,7 +244,7 @@ impl Response { /// # use http::*; /// let response = Response::new("hello world"); /// - /// assert_eq!(response.status(), status::OK); + /// assert_eq!(response.status(), StatusCode::OK); /// assert_eq!(*response.body(), "hello world"); /// ``` #[inline] @@ -270,10 +264,10 @@ impl Response { /// let response = Response::new("hello world"); /// let (mut parts, body) = response.into_parts(); /// - /// parts.status = status::BAD_REQUEST; + /// parts.status = StatusCode::BAD_REQUEST; /// let response = Response::from_parts(parts, body); /// - /// assert_eq!(response.status(), status::BAD_REQUEST); + /// assert_eq!(response.status(), StatusCode::BAD_REQUEST); /// assert_eq!(*response.body(), "hello world"); /// ``` #[inline] @@ -291,7 +285,7 @@ impl Response { /// ``` /// # use http::*; /// let response: Response<()> = Response::default(); - /// assert_eq!(response.status(), status::OK); + /// assert_eq!(response.status(), StatusCode::OK); /// ``` #[inline] pub fn status(&self) -> StatusCode { @@ -305,8 +299,8 @@ impl Response { /// ``` /// # use http::*; /// let mut response: Response<()> = Response::default(); - /// *response.status_mut() = status::CREATED; - /// assert_eq!(response.status(), status::CREATED); + /// *response.status_mut() = StatusCode::CREATED; + /// assert_eq!(response.status(), StatusCode::CREATED); /// ``` #[inline] pub fn status_mut(&mut self) -> &mut StatusCode { @@ -320,7 +314,7 @@ impl Response { /// ``` /// # use http::*; /// let response: Response<()> = Response::default(); - /// assert_eq!(response.version(), version::HTTP_11); + /// assert_eq!(response.version(), Version::HTTP_11); /// ``` #[inline] pub fn version(&self) -> Version { @@ -334,8 +328,8 @@ impl Response { /// ``` /// # use http::*; /// let mut response: Response<()> = Response::default(); - /// *response.version_mut() = version::HTTP_2; - /// assert_eq!(response.version(), version::HTTP_2); + /// *response.version_mut() = Version::HTTP_2; + /// assert_eq!(response.version(), Version::HTTP_2); /// ``` #[inline] pub fn version_mut(&mut self) -> &mut Version { @@ -439,7 +433,7 @@ impl Response { /// # use http::*; /// let response: Response<()> = Response::default(); /// let (parts, body) = response.into_parts(); - /// assert_eq!(parts.status, status::OK); + /// assert_eq!(parts.status, StatusCode::OK); /// ``` #[inline] pub fn into_parts(self) -> (Parts, T) { @@ -550,10 +544,9 @@ impl Builder { /// /// ``` /// # use http::*; - /// use http::version::HTTP_2; /// /// let response = Response::builder() - /// .version(HTTP_2) + /// .version(Version::HTTP_2) /// .body(()) /// .unwrap(); /// ``` diff --git a/src/status.rs b/src/status.rs index 9964f77..73fe1e9 100644 --- a/src/status.rs +++ b/src/status.rs @@ -9,11 +9,11 @@ //! # Examples //! //! ``` -//! use http::{status, StatusCode}; +//! use http::StatusCode; //! -//! assert_eq!(StatusCode::from_u16(200).unwrap(), status::OK); -//! assert_eq!(status::NOT_FOUND.as_u16(), 404); -//! assert!(status::OK.is_success()); +//! assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); +//! assert_eq!(StatusCode::NOT_FOUND.as_u16(), 404); +//! assert!(StatusCode::OK.is_success()); //! ``` use std::fmt; @@ -38,11 +38,11 @@ use HttpTryFrom; /// # Examples /// /// ``` -/// use http::{status, StatusCode}; +/// use http::StatusCode; /// -/// assert_eq!(StatusCode::from_u16(200).unwrap(), status::OK); -/// assert_eq!(status::NOT_FOUND.as_u16(), 404); -/// assert!(status::OK.is_success()); +/// assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); +/// assert_eq!(StatusCode::NOT_FOUND.as_u16(), 404); +/// assert!(StatusCode::OK.is_success()); /// ``` #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct StatusCode(u16); @@ -65,10 +65,10 @@ impl StatusCode { /// # Example /// /// ``` - /// use http::{status, StatusCode}; + /// use http::StatusCode; /// /// let ok = StatusCode::from_u16(200).unwrap(); - /// assert_eq!(ok, status::OK); + /// assert_eq!(ok, StatusCode::OK); /// /// let err = StatusCode::from_u16(99); /// assert!(err.is_err()); @@ -105,7 +105,7 @@ impl StatusCode { /// # Example /// /// ``` - /// let status = http::status::OK; + /// let status = http::StatusCode::OK; /// assert_eq!(status.as_u16(), 200); /// ``` #[inline] @@ -153,8 +153,8 @@ impl fmt::Debug for StatusCode { /// Formats the status code, *including* the canonical reason. /// /// ```rust -/// # use http::status; -/// assert_eq!(format!("{}", status::OK), "200 OK"); +/// # use http::StatusCode; +/// assert_eq!(format!("{}", StatusCode::OK), "200 OK"); /// ``` impl fmt::Display for StatusCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -166,7 +166,7 @@ impl fmt::Display for StatusCode { impl Default for StatusCode { #[inline] fn default() -> StatusCode { - OK + StatusCode::OK } } @@ -227,12 +227,12 @@ macro_rules! status_codes { ($num:expr, $konst:ident, $phrase:expr); )+ ) => { + impl StatusCode { $( $(#[$docs])* pub const $konst: StatusCode = StatusCode($num); )+ - impl StatusCode { /// Get the standardised `reason-phrase` for this status code. /// /// This is mostly here for servers writing responses, but could potentially have application diff --git a/src/version.rs b/src/version.rs index dfbe8d0..3229dc3 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,17 +1,19 @@ //! HTTP version //! -//! This module contains a definition of the `Version` type and various -//! constants associated with it as well. The `Version` type is intended to be -//! accessed through the root of the crate (`http::Version`) rather than this -//! module. +//! This module contains a definition of the `Version` type. The `Version` +//! type is intended to be accessed through the root of the crate +//! (`http::Version`) rather than this module. +//! +//! The `Version` type contains constants that represent the various versions +//! of the HTTP protocol. //! //! # Examples //! //! ``` -//! use http::{version, Version}; +//! use http::Version; //! -//! let http11 = version::HTTP_11; -//! let http2 = version::HTTP_2; +//! let http11 = Version::HTTP_11; +//! let http2 = Version::HTTP_2; //! assert!(http11 != http2); //! //! println!("{:?}", http2); @@ -23,17 +25,19 @@ use std::fmt; #[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)] pub struct Version(Http); -/// `HTTP/0.9` -pub const HTTP_09: Version = Version(Http::Http09); +impl Version { + /// `HTTP/0.9` + pub const HTTP_09: Version = Version(Http::Http09); -/// `HTTP/1.0` -pub const HTTP_10: Version = Version(Http::Http10); + /// `HTTP/1.0` + pub const HTTP_10: Version = Version(Http::Http10); -/// `HTTP/1.1` -pub const HTTP_11: Version = Version(Http::Http11); + /// `HTTP/1.1` + pub const HTTP_11: Version = Version(Http::Http11); -/// `HTTP/2.0` -pub const HTTP_2: Version = Version(Http::H2); + /// `HTTP/2.0` + pub const HTTP_2: Version = Version(Http::H2); +} #[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)] enum Http { @@ -46,7 +50,7 @@ enum Http { impl Default for Version { #[inline] fn default() -> Version { - HTTP_11 + Version::HTTP_11 } }