Skip to content

Commit b162385

Browse files
authored
Merge pull request #61 from yoshuawuyts/try-from-headername
Try from headername
2 parents c9b612f + 657e714 commit b162385

File tree

6 files changed

+122
-32
lines changed

6 files changed

+122
-32
lines changed

src/headers/header_name.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ impl FromStr for HeaderName {
6565
}
6666
}
6767

68+
impl<'a> std::convert::TryFrom<&'a str> for HeaderName {
69+
type Error = ParseError;
70+
71+
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
72+
Self::from_str(value)
73+
}
74+
}
75+
6876
#[cfg(test)]
6977
mod tests {
7078
use super::*;

src/headers/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use async_std::io;
44

55
use std::collections::HashMap;
6+
use std::convert::TryInto;
67
use std::iter::IntoIterator;
78

89
mod constants;
@@ -44,9 +45,12 @@ impl Headers {
4445
/// Insert a header into the headers.
4546
pub fn insert(
4647
&mut self,
47-
name: HeaderName,
48+
name: impl TryInto<HeaderName>,
4849
values: impl ToHeaderValues,
4950
) -> io::Result<Option<Vec<HeaderValue>>> {
51+
let name = name.try_into().map_err(|_| {
52+
io::Error::new(io::ErrorKind::Other, "Could not convert into header name")
53+
})?;
5054
let values: Vec<HeaderValue> = values.to_header_values()?.collect();
5155
Ok(self.headers.insert(name, values))
5256
}
@@ -55,7 +59,14 @@ impl Headers {
5559
///
5660
/// Unlike `insert` this function will not override the contents of a header, but insert a
5761
/// header if there aren't any. Or else append to the existing list of headers.
58-
pub fn append(&mut self, name: HeaderName, values: impl ToHeaderValues) -> io::Result<()> {
62+
pub fn append(
63+
&mut self,
64+
name: impl TryInto<HeaderName>,
65+
values: impl ToHeaderValues,
66+
) -> io::Result<()> {
67+
let name = name.try_into().map_err(|_| {
68+
io::Error::new(io::ErrorKind::Other, "Could not convert into header name")
69+
})?;
5970
match self.get_mut(&name) {
6071
Some(headers) => {
6172
let mut values: Vec<HeaderValue> = values.to_header_values()?.collect();

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ pub use method::Method;
128128
pub use request::Request;
129129
pub use response::Response;
130130
pub use status_code::StatusCode;
131-
pub use trailers::Trailers;
132131
pub use version::Version;
133132

133+
#[doc(inline)]
134+
pub use trailers::Trailers;
135+
134136
#[doc(inline)]
135137
pub use mime::Mime;
136138

src/request.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

4+
use std::convert::TryInto;
45
use std::mem;
56
use std::pin::Pin;
67
use std::task::{Context, Poll};
@@ -62,19 +63,6 @@ impl Request {
6263
self.method = method;
6364
}
6465

65-
/// Set the headers.
66-
pub fn set_headers<'a, T: IntoIterator<Item = (&'a HeaderName, &'a Vec<HeaderValue>)>>(
67-
&mut self,
68-
headers: T,
69-
) {
70-
self.headers = Headers {
71-
headers: headers
72-
.into_iter()
73-
.map(|(name, values)| (name.clone(), values.clone()))
74-
.collect(),
75-
};
76-
}
77-
7866
/// Get a reference to the url.
7967
///
8068
/// # Examples
@@ -225,9 +213,22 @@ impl Request {
225213
}
226214

227215
/// Set an HTTP header.
216+
///
217+
/// # Examples
218+
///
219+
/// ```
220+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
221+
/// #
222+
/// use http_types::{Url, Method, Request};
223+
///
224+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
225+
/// req.insert_header("Content-Type", "text/plain")?;
226+
/// #
227+
/// # Ok(()) }
228+
/// ```
228229
pub fn insert_header(
229230
&mut self,
230-
name: HeaderName,
231+
name: impl TryInto<HeaderName>,
231232
values: impl ToHeaderValues,
232233
) -> io::Result<Option<Vec<HeaderValue>>> {
233234
self.headers.insert(name, values)
@@ -237,9 +238,22 @@ impl Request {
237238
///
238239
/// Unlike `insert` this function will not override the contents of a header, but insert a
239240
/// header if there aren't any. Or else append to the existing list of headers.
241+
///
242+
/// # Examples
243+
///
244+
/// ```
245+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
246+
/// #
247+
/// use http_types::{Url, Method, Request};
248+
///
249+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
250+
/// req.append_header("Content-Type", "text/plain")?;
251+
/// #
252+
/// # Ok(()) }
253+
/// ```
240254
pub fn append_header(
241255
&mut self,
242-
name: HeaderName,
256+
name: impl TryInto<HeaderName>,
243257
values: impl ToHeaderValues,
244258
) -> io::Result<()> {
245259
self.headers.append(name, values)

src/response.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

4+
use std::convert::TryInto;
45
use std::mem;
56
use std::pin::Pin;
67
use std::task::{Context, Poll};
@@ -20,10 +21,10 @@ pin_project_lite::pin_project! {
2021
/// ```
2122
/// # fn main() -> Result<(), http_types::url::ParseError> {
2223
/// #
23-
/// use http_types::{Url, Method, Request};
24+
/// use http_types::{Response, StatusCode};
2425
///
25-
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
26-
/// req.set_body("Hello, Nori!");
26+
/// let mut res = Response::new(StatusCode::Ok);
27+
/// res.set_body("Hello, Nori!");
2728
/// #
2829
/// # Ok(()) }
2930
/// ```
@@ -74,9 +75,22 @@ impl Response {
7475
}
7576

7677
/// Set an HTTP header.
78+
///
79+
/// # Examples
80+
///
81+
/// ```
82+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
83+
/// #
84+
/// use http_types::{Url, Method, Request};
85+
///
86+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
87+
/// req.insert_header("Content-Type", "text/plain")?;
88+
/// #
89+
/// # Ok(()) }
90+
/// ```
7791
pub fn insert_header(
7892
&mut self,
79-
name: HeaderName,
93+
name: impl TryInto<HeaderName>,
8094
values: impl ToHeaderValues,
8195
) -> io::Result<Option<Vec<HeaderValue>>> {
8296
self.headers.insert(name, values)
@@ -86,9 +100,22 @@ impl Response {
86100
///
87101
/// Unlike `insert` this function will not override the contents of a header, but insert a
88102
/// header if there aren't any. Or else append to the existing list of headers.
103+
///
104+
/// # Examples
105+
///
106+
/// ```
107+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
108+
/// #
109+
/// use http_types::{Response, StatusCode};
110+
///
111+
/// let mut res = Response::new(StatusCode::Ok);
112+
/// res.append_header("Content-Type", "text/plain")?;
113+
/// #
114+
/// # Ok(()) }
115+
/// ```
89116
pub fn append_header(
90117
&mut self,
91-
name: HeaderName,
118+
name: impl TryInto<HeaderName>,
92119
values: impl ToHeaderValues,
93120
) -> io::Result<()> {
94121
self.headers.append(name, values)
@@ -101,10 +128,10 @@ impl Response {
101128
/// ```
102129
/// # fn main() -> Result<(), http_types::url::ParseError> {
103130
/// #
104-
/// use http_types::{Url, Method, Request};
131+
/// use http_types::{Response, StatusCode};
105132
///
106-
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
107-
/// req.set_body("Hello, Nori!");
133+
/// let mut res = Response::new(StatusCode::Ok);
134+
/// res.set_body("Hello, Nori!");
108135
/// #
109136
/// # Ok(()) }
110137
/// ```

src/trailers.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@
3131
//!
3232
//! let sender = req.send_trailers();
3333
//! let mut trailers = Trailers::new();
34-
//! trailers.insert(
35-
//! HeaderName::from_str("Content-Type")?,
36-
//! "text/plain",
37-
//! );
34+
//! trailers.insert("Content-Type", "text/plain")?;
3835
//!
3936
//! task::spawn(async move {
4037
//! let _trailers = req.recv_trailers().await;
@@ -54,6 +51,7 @@ use crate::headers::{
5451
};
5552
use async_std::sync::Sender;
5653

54+
use std::convert::TryInto;
5755
use std::io;
5856
use std::ops::{Deref, DerefMut};
5957

@@ -72,9 +70,22 @@ impl Trailers {
7270
}
7371

7472
/// Insert a header into the headers.
73+
///
74+
/// # Examples
75+
///
76+
/// ```
77+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
78+
/// #
79+
/// use http_types::Trailers;
80+
///
81+
/// let mut trailers = Trailers::new();
82+
/// trailers.insert("Content-Type", "text/plain")?;
83+
/// #
84+
/// # Ok(()) }
85+
/// ```
7586
pub fn insert(
7687
&mut self,
77-
name: HeaderName,
88+
name: impl TryInto<HeaderName>,
7889
values: impl ToHeaderValues,
7990
) -> io::Result<Option<Vec<HeaderValue>>> {
8091
self.headers.insert(name, values)
@@ -84,7 +95,24 @@ impl Trailers {
8495
///
8596
/// Unlike `insert` this function will not override the contents of a header, but insert a
8697
/// header if there aren't any. Or else append to the existing list of headers.
87-
pub fn append(&mut self, name: HeaderName, values: impl ToHeaderValues) -> io::Result<()> {
98+
///
99+
/// # Examples
100+
///
101+
/// ```
102+
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
103+
/// #
104+
/// use http_types::Trailers;
105+
///
106+
/// let mut trailers = Trailers::new();
107+
/// trailers.append("Content-Type", "text/plain")?;
108+
/// #
109+
/// # Ok(()) }
110+
/// ```
111+
pub fn append(
112+
&mut self,
113+
name: impl TryInto<HeaderName>,
114+
values: impl ToHeaderValues,
115+
) -> io::Result<()> {
88116
self.headers.append(name, values)
89117
}
90118

0 commit comments

Comments
 (0)