From 00852fb08f0cffc17c1d41ad40aa4ce1e13e5620 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 5 Dec 2019 23:08:15 +0900 Subject: [PATCH 1/3] init http request Signed-off-by: Yoshua Wuyts --- src/request.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/request.rs b/src/request.rs index c2d6ba4f..0c9bf9c5 100644 --- a/src/request.rs +++ b/src/request.rs @@ -12,6 +12,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 +35,17 @@ 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: impl Into, + url: Url, + ) -> Result> { + Ok(Self { + method: method.into(), url, headers: Headers::new(), body_reader: Box::new(io::empty()), length: Some(0), - } + }) } /// Get the HTTP method From f7beeffbcf4af51b208930e7cffc99d7c3096976 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 5 Dec 2019 23:21:14 +0900 Subject: [PATCH 2/3] response fallible start Signed-off-by: Yoshua Wuyts --- src/response.rs | 23 +++++++++++++++++++---- src/status_code.rs | 8 ++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) 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; From 10c599cd280d6bbd0927f8c421eef0694a32a913 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 5 Dec 2019 23:35:17 +0900 Subject: [PATCH 3/3] cargo fmt Signed-off-by: Yoshua Wuyts --- src/method.rs | 8 ++++++++ src/request.rs | 13 +++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) 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 0c9bf9c5..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}; @@ -35,12 +36,16 @@ pin_project_lite::pin_project! { impl Request { /// Create a new request. - pub fn new( - method: impl Into, + pub fn new( + method: M, url: Url, - ) -> Result> { + ) -> Result> + where + M: TryInto, + >::Error: Sync + Send + std::error::Error + 'static, + { Ok(Self { - method: method.into(), + method: method.try_into()?, url, headers: Headers::new(), body_reader: Box::new(io::empty()),