Skip to content

Commit 728052e

Browse files
committed
Remove all ParseError instances.
1 parent b262d5d commit 728052e

File tree

7 files changed

+31
-68
lines changed

7 files changed

+31
-68
lines changed

src/headers/header_value.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::{self, Display};
22
use std::str::FromStr;
33

4-
use crate::headers::ParseError;
4+
use crate::{Error, ErrorKind};
55
use crate::{Cookie, Mime};
66

77
/// A header value.
@@ -16,9 +16,9 @@ impl HeaderValue {
1616
/// # Error
1717
///
1818
/// This function will error if the string is not a valid ASCII.
19-
pub fn from_ascii(bytes: &[u8]) -> Result<Self, ParseError> {
19+
pub fn from_ascii(bytes: &[u8]) -> Result<Self, Error> {
2020
if !bytes.is_ascii() {
21-
return Err(ParseError::new());
21+
return Err(Error::from(ErrorKind::InvalidData));
2222
}
2323

2424
// This is permitted because ASCII is valid UTF-8, and we just checked that.
@@ -71,14 +71,14 @@ impl From<&Mime> for HeaderValue {
7171
}
7272

7373
impl FromStr for HeaderValue {
74-
type Err = ParseError;
74+
type Err = Error;
7575

7676
/// Create a new `HeaderValue`.
7777
///
7878
/// This checks it's valid ASCII.
7979
fn from_str(s: &str) -> Result<Self, Self::Err> {
8080
if !s.is_ascii() {
81-
return Err(ParseError::new());
81+
return Err(Error::from(ErrorKind::InvalidData));
8282
}
8383
Ok(Self {
8484
inner: String::from(s),

src/headers/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ mod into_iter;
1515
mod iter;
1616
mod iter_mut;
1717
mod names;
18-
mod parse_error;
1918
mod to_header_values;
2019
mod values;
2120

@@ -26,7 +25,6 @@ pub use into_iter::IntoIter;
2625
pub use iter::Iter;
2726
pub use iter_mut::IterMut;
2827
pub use names::Names;
29-
pub use parse_error::ParseError;
3028
pub use to_header_values::ToHeaderValues;
3129
pub use values::Values;
3230

src/headers/parse_error.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/method.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::error::Error;
21
use std::fmt::{self, Display};
32
use std::str::FromStr;
43

4+
use crate::{Error, ErrorKind};
5+
56
/// HTTP request methods.
67
///
78
/// [Read more](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
@@ -67,22 +68,8 @@ impl Display for Method {
6768
}
6869
}
6970

70-
/// An error returned when failing to convert into a status code.
71-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
72-
pub struct ParseError {
73-
_private: (),
74-
}
75-
76-
impl Error for ParseError {}
77-
78-
impl Display for ParseError {
79-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80-
"Error parsing a string into a status code".fmt(f)
81-
}
82-
}
83-
8471
impl FromStr for Method {
85-
type Err = ParseError;
72+
type Err = crate::Error;
8673

8774
fn from_str(s: &str) -> Result<Self, Self::Err> {
8875
match s {
@@ -95,7 +82,7 @@ impl FromStr for Method {
9582
"OPTIONS" => Ok(Self::Options),
9683
"TRACE" => Ok(Self::Trace),
9784
"PATCH" => Ok(Self::Patch),
98-
_ => Err(ParseError { _private: () }),
85+
_ => Err(Error::from(ErrorKind::InvalidData)),
9986
}
10087
}
10188
}

src/mime/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::io;
1313
use std::option;
1414
use std::str::FromStr;
1515

16-
use crate::headers::{HeaderValue, ParseError, ToHeaderValues};
17-
use crate::Error;
16+
use crate::headers::{HeaderValue, ToHeaderValues};
17+
use crate::{Error, ErrorKind};
1818
use crate::StatusCode;
1919

2020
use infer::Infer;
@@ -130,14 +130,14 @@ impl Debug for Mime {
130130
}
131131

132132
impl FromStr for Mime {
133-
type Err = ParseError;
133+
type Err = crate::Error;
134134

135135
/// Create a new `HeaderName`.
136136
///
137137
/// This checks it's valid ASCII, and lowercases it.
138138
fn from_str(s: &str) -> Result<Self, Self::Err> {
139139
if !s.is_ascii() {
140-
return Err(ParseError::new());
140+
return Err(Error::from(ErrorKind::InvalidData));
141141
}
142142
Ok(Self {
143143
essence: s.to_ascii_lowercase(),

src/request.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Request {
6868
/// # Examples
6969
///
7070
/// ```
71-
/// # fn main() -> Result<(), http_types::url::ParseError> {
71+
/// # fn main() -> Result<(), http_types::Error> {
7272
/// #
7373
/// use http_types::{Url, Method, Request, Response, StatusCode};
7474
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
@@ -85,7 +85,7 @@ impl Request {
8585
/// # Examples
8686
///
8787
/// ```
88-
/// # fn main() -> Result<(), http_types::url::ParseError> {
88+
/// # fn main() -> Result<(), http_types::Error> {
8989
/// #
9090
/// use http_types::{Url, Method, Request, Response, StatusCode};
9191
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
@@ -289,7 +289,7 @@ impl Request {
289289
/// ```
290290
/// use http_types::{Url, Method, Request, Version};
291291
///
292-
/// # fn main() -> Result<(), http_types::url::ParseError> {
292+
/// # fn main() -> Result<(), http_types::Error> {
293293
/// #
294294
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
295295
/// assert_eq!(req.version(), None);
@@ -310,7 +310,7 @@ impl Request {
310310
/// ```
311311
/// use http_types::{Url, Method, Request, Version};
312312
///
313-
/// # fn main() -> Result<(), http_types::url::ParseError> {
313+
/// # fn main() -> Result<(), http_types::Error> {
314314
/// #
315315
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
316316
/// req.set_version(Some(Version::Http2_0));
@@ -326,7 +326,7 @@ impl Request {
326326
/// # Examples
327327
///
328328
/// ```
329-
/// # fn main() -> Result<(), http_types::url::ParseError> {
329+
/// # fn main() -> Result<(), http_types::Error> {
330330
/// #
331331
/// use http_types::{Cookie, Url, Method, Request, Version};
332332
///
@@ -336,7 +336,7 @@ impl Request {
336336
/// #
337337
/// # Ok(()) }
338338
/// ```
339-
pub fn cookies(&self) -> Result<Vec<Cookie<'_>>, cookie::ParseError> {
339+
pub fn cookies(&self) -> Result<Vec<Cookie<'_>>, crate::Error> {
340340
match self.header(&headers::COOKIE) {
341341
None => Ok(vec![]),
342342
Some(h) => h.iter().try_fold(vec![], |mut acc, h| {
@@ -352,7 +352,7 @@ impl Request {
352352
/// # Examples
353353
///
354354
/// ```
355-
/// # fn main() -> Result<(), http_types::url::ParseError> {
355+
/// # fn main() -> Result<(), http_types::Error> {
356356
/// #
357357
/// use http_types::{Cookie, Url, Method, Request, Version};
358358
///
@@ -362,7 +362,7 @@ impl Request {
362362
/// #
363363
/// # Ok(()) }
364364
/// ```
365-
pub fn cookie(&self, name: &str) -> Result<Option<Cookie<'_>>, cookie::ParseError> {
365+
pub fn cookie(&self, name: &str) -> Result<Option<Cookie<'_>>, crate::Error> {
366366
let cookies = self.cookies()?;
367367
let cookie = cookies.into_iter().filter(|c| c.name() == name).next();
368368
Ok(cookie)
@@ -375,7 +375,7 @@ impl Request {
375375
/// # Examples
376376
///
377377
/// ```
378-
/// # fn main() -> Result<(), http_types::url::ParseError> {
378+
/// # fn main() -> Result<(), http_types::Error> {
379379
/// #
380380
/// use http_types::{Cookie, Url, Method, Request, Version};
381381
///

src/response.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pin_project_lite::pin_project! {
1919
/// # Examples
2020
///
2121
/// ```
22-
/// # fn main() -> Result<(), http_types::url::ParseError> {
22+
/// # fn main() -> Result<(), http_types::Error> {
2323
/// #
2424
/// use http_types::{Response, StatusCode};
2525
///
@@ -126,7 +126,7 @@ impl Response {
126126
/// # Examples
127127
///
128128
/// ```
129-
/// # fn main() -> Result<(), http_types::url::ParseError> {
129+
/// # fn main() -> Result<(), http_types::Error> {
130130
/// #
131131
/// use http_types::{Response, StatusCode};
132132
///
@@ -248,7 +248,7 @@ impl Response {
248248
/// # Examples
249249
///
250250
/// ```
251-
/// # fn main() -> Result<(), http_types::url::ParseError> {
251+
/// # fn main() -> Result<(), http_types::Error> {
252252
/// #
253253
/// use http_types::{Response, StatusCode, Version};
254254
///
@@ -269,7 +269,7 @@ impl Response {
269269
/// # Examples
270270
///
271271
/// ```
272-
/// # fn main() -> Result<(), http_types::url::ParseError> {
272+
/// # fn main() -> Result<(), http_types::Error> {
273273
/// #
274274
/// use http_types::{Response, StatusCode, Version};
275275
///
@@ -292,7 +292,7 @@ impl Response {
292292
/// # Examples
293293
///
294294
/// ```
295-
/// # fn main() -> Result<(), http_types::url::ParseError> {
295+
/// # fn main() -> Result<(), http_types::Error> {
296296
/// #
297297
/// use http_types::{Cookie, Response, StatusCode, Version};
298298
///
@@ -302,7 +302,7 @@ impl Response {
302302
/// #
303303
/// # Ok(()) }
304304
/// ```
305-
pub fn cookies(&self) -> Result<Vec<Cookie<'_>>, cookie::ParseError> {
305+
pub fn cookies(&self) -> Result<Vec<Cookie<'_>>, crate::Error> {
306306
match self.header(&headers::SET_COOKIE) {
307307
None => Ok(vec![]),
308308
Some(h) => h.iter().try_fold(vec![], |mut acc, h| {
@@ -318,7 +318,7 @@ impl Response {
318318
/// # Examples
319319
///
320320
/// ```
321-
/// # fn main() -> Result<(), http_types::url::ParseError> {
321+
/// # fn main() -> Result<(), http_types::Error> {
322322
/// #
323323
/// use http_types::{Cookie, Response, StatusCode, Version};
324324
///
@@ -328,7 +328,7 @@ impl Response {
328328
/// #
329329
/// # Ok(()) }
330330
/// ```
331-
pub fn cookie(&self, name: &str) -> Result<Option<Cookie<'_>>, cookie::ParseError> {
331+
pub fn cookie(&self, name: &str) -> Result<Option<Cookie<'_>>, crate::Error> {
332332
let cookies = self.cookies()?;
333333
let cookie = cookies.into_iter().filter(|c| c.name() == name).next();
334334
Ok(cookie)
@@ -341,7 +341,7 @@ impl Response {
341341
/// # Examples
342342
///
343343
/// ```
344-
/// # fn main() -> Result<(), http_types::url::ParseError> {
344+
/// # fn main() -> Result<(), http_types::Error> {
345345
/// #
346346
/// use http_types::{Cookie, Response, StatusCode, Version};
347347
///

0 commit comments

Comments
 (0)