Skip to content

Commit ea22d43

Browse files
committed
Add header insert/append docs
Adds docs for inserting / appending to: - Request - Response - Trailers
1 parent 8509362 commit ea22d43

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

src/request.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ impl Request {
213213
}
214214

215215
/// 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+
/// ```
216229
pub fn insert_header(
217230
&mut self,
218231
name: impl TryInto<HeaderName>,
@@ -225,6 +238,19 @@ impl Request {
225238
///
226239
/// Unlike `insert` this function will not override the contents of a header, but insert a
227240
/// 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+
/// ```
228254
pub fn append_header(
229255
&mut self,
230256
name: impl TryInto<HeaderName>,

src/response.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ pin_project_lite::pin_project! {
2121
/// ```
2222
/// # fn main() -> Result<(), http_types::url::ParseError> {
2323
/// #
24-
/// use http_types::{Url, Method, Request};
24+
/// use http_types::{Response, StatusCode};
2525
///
26-
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
27-
/// req.set_body("Hello, Nori!");
26+
/// let mut res = Response::new(StatusCode::Ok);
27+
/// res.set_body("Hello, Nori!");
2828
/// #
2929
/// # Ok(()) }
3030
/// ```
@@ -75,6 +75,19 @@ impl Response {
7575
}
7676

7777
/// 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+
/// ```
7891
pub fn insert_header(
7992
&mut self,
8093
name: impl TryInto<HeaderName>,
@@ -87,6 +100,19 @@ impl Response {
87100
///
88101
/// Unlike `insert` this function will not override the contents of a header, but insert a
89102
/// 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+
/// ```
90116
pub fn append_header(
91117
&mut self,
92118
name: impl TryInto<HeaderName>,
@@ -102,10 +128,10 @@ impl Response {
102128
/// ```
103129
/// # fn main() -> Result<(), http_types::url::ParseError> {
104130
/// #
105-
/// use http_types::{Url, Method, Request};
131+
/// use http_types::{Response, StatusCode};
106132
///
107-
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
108-
/// req.set_body("Hello, Nori!");
133+
/// let mut res = Response::new(StatusCode::Ok);
134+
/// res.set_body("Hello, Nori!");
109135
/// #
110136
/// # Ok(()) }
111137
/// ```

src/trailers.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ impl Trailers {
7070
}
7171

7272
/// 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+
/// ```
7386
pub fn insert(
7487
&mut self,
7588
name: impl TryInto<HeaderName>,
@@ -82,6 +95,19 @@ impl Trailers {
8295
///
8396
/// Unlike `insert` this function will not override the contents of a header, but insert a
8497
/// header if there aren't any. Or else append to the existing list of headers.
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+
/// ```
85111
pub fn append(&mut self, name: impl TryInto<HeaderName>, values: impl ToHeaderValues) -> io::Result<()> {
86112
self.headers.append(name, values)
87113
}

0 commit comments

Comments
 (0)