diff --git a/src/method.rs b/src/method.rs index 6ebac2cd..86f36ea0 100644 --- a/src/method.rs +++ b/src/method.rs @@ -1,3 +1,4 @@ +use std::convert::TryFrom; use std::error::Error; use std::fmt::{self, Display}; use std::str::FromStr; @@ -86,3 +87,10 @@ impl FromStr for Method { } } } + +impl<'a> TryFrom<&'a str> for Method { + type Error = ParseError; + fn try_from(s: &'a str) -> Result { + s.parse() + } +} diff --git a/src/request.rs b/src/request.rs index c2d6ba4f..64cd5eb6 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,6 +1,7 @@ use async_std::io::{self, BufRead, Read}; use std::borrow::Borrow; +use std::convert::TryInto; use std::fmt::{self, Debug}; use std::pin::Pin; use std::task::{Context, Poll}; @@ -12,6 +13,17 @@ type BodyReader = dyn BufRead + Unpin + Send + 'static; pin_project_lite::pin_project! { /// An HTTP request. + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # + /// use http_types::{Url, Method, Request}; + /// + /// let url = Url::parse("https://example.com")?; + /// let mut req = Request::new(Method::Get, url)?; + /// # + /// # Ok(()) } + /// ``` pub struct Request { method: Method, url: Url, @@ -24,14 +36,21 @@ pin_project_lite::pin_project! { impl Request { /// Create a new request. - pub fn new(method: Method, url: Url) -> Self { - Self { - method, + pub fn new( + method: M, + url: Url, + ) -> Result> + where + M: TryInto, + >::Error: Sync + Send + std::error::Error + 'static, + { + Ok(Self { + method: method.try_into()?, url, headers: Headers::new(), body_reader: Box::new(io::empty()), length: Some(0), - } + }) } /// Get the HTTP method diff --git a/src/response.rs b/src/response.rs index 00a71068..a9650f99 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,6 +1,7 @@ use async_std::io::{self, BufRead, Read}; use std::borrow::Borrow; +use std::convert::TryInto; use std::fmt::{self, Debug}; use std::pin::Pin; use std::task::{Context, Poll}; @@ -12,6 +13,16 @@ type BodyReader = dyn BufRead + Unpin + Send + 'static; pin_project_lite::pin_project! { /// An HTTP response. + /// + /// ``` + /// # fn main() -> Result<(), Box> { + /// # + /// use http_types::{Response, StatusCode}; + /// + /// let mut req = Response::new(200)?; + /// # + /// # Ok(()) } + /// ``` pub struct Response { #[pin] body_reader: Box, @@ -23,13 +34,17 @@ pin_project_lite::pin_project! { impl Response { /// Create a new response. - pub fn new(status: StatusCode) -> Self { - Self { - status, + pub fn new(status: S) -> Result> + where + S: TryInto, + >::Error: Sync + Send + std::error::Error + 'static, + { + Ok(Self { + status: status.try_into()?, headers: Headers::new(), body_reader: Box::new(io::empty()), length: Some(0), - } + }) } /// Get the status diff --git a/src/status_code.rs b/src/status_code.rs index 7a48436b..f76a3900 100644 --- a/src/status_code.rs +++ b/src/status_code.rs @@ -466,6 +466,14 @@ impl Into for StatusCode { } } +impl std::convert::TryFrom for StatusCode { + type Error = Box; + + fn try_from(value: i32) -> Result { + Self::try_from(value as u16) + } +} + impl std::convert::TryFrom for StatusCode { type Error = Box;