Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Into types #28

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/method.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use std::error::Error;
use std::fmt::{self, Display};
use std::str::FromStr;
Expand Down Expand Up @@ -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<Self, Self::Error> {
s.parse()
}
}
27 changes: 23 additions & 4 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,6 +13,17 @@ type BodyReader = dyn BufRead + Unpin + Send + 'static;

pin_project_lite::pin_project! {
/// An HTTP request.
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// #
/// 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,
Expand All @@ -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<M>(
method: M,
url: Url,
) -> Result<Self, Box<dyn std::error::Error + Send + Sync + 'static>>
where
M: TryInto<Method>,
<M as TryInto<Method>>::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
Expand Down
23 changes: 19 additions & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -12,6 +13,16 @@ type BodyReader = dyn BufRead + Unpin + Send + 'static;

pin_project_lite::pin_project! {
/// An HTTP response.
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// #
/// use http_types::{Response, StatusCode};
///
/// let mut req = Response::new(200)?;
/// #
/// # Ok(()) }
/// ```
pub struct Response {
#[pin]
body_reader: Box<BodyReader>,
Expand All @@ -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<S>(status: S) -> Result<Self, Box<dyn std::error::Error + Send + Sync + 'static>>
where
S: TryInto<StatusCode>,
<S as TryInto<StatusCode>>::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
Expand Down
8 changes: 8 additions & 0 deletions src/status_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ impl Into<u16> for StatusCode {
}
}

impl std::convert::TryFrom<i32> for StatusCode {
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

fn try_from(value: i32) -> Result<Self, Self::Error> {
Self::try_from(value as u16)
}
}

impl std::convert::TryFrom<u16> for StatusCode {
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

Expand Down