1use std::{error, fmt, io, num, str};
3
4#[derive(Debug, PartialEq)]
5pub enum ParseErr {
6 Utf8(str::Utf8Error),
7 Int(num::ParseIntError),
8 StatusErr,
9 HeadersErr,
10 UriErr,
11 Invalid,
12 Empty,
13}
14
15impl error::Error for ParseErr {
16 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
17 use self::ParseErr::*;
18
19 match self {
20 Utf8(e) => Some(e),
21 Int(e) => Some(e),
22 StatusErr | HeadersErr | UriErr | Invalid | Empty => None,
23 }
24 }
25}
26
27impl fmt::Display for ParseErr {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 use self::ParseErr::*;
30
31 let err = match self {
32 Utf8(_) => "invalid character",
33 Int(_) => "cannot parse number",
34 Invalid => "invalid value",
35 Empty => "nothing to parse",
36 StatusErr => "status line contains invalid values",
37 HeadersErr => "headers contain invalid values",
38 UriErr => "uri contains invalid characters",
39 };
40 write!(f, "ParseErr: {}", err)
41 }
42}
43
44impl From<num::ParseIntError> for ParseErr {
45 fn from(e: num::ParseIntError) -> Self {
46 ParseErr::Int(e)
47 }
48}
49
50impl From<str::Utf8Error> for ParseErr {
51 fn from(e: str::Utf8Error) -> Self {
52 ParseErr::Utf8(e)
53 }
54}
55
56#[derive(Debug)]
57pub enum Error {
58 IO(io::Error),
59 Parse(ParseErr),
60 Tls,
61}
62
63impl error::Error for Error {
64 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
65 use self::Error::*;
66
67 match self {
68 IO(e) => Some(e),
69 Parse(e) => Some(e),
70 Tls => None,
71 }
72 }
73}
74
75impl fmt::Display for Error {
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 use self::Error::*;
78
79 let err = match self {
80 IO(_) => "IO error",
81 Tls => "TLS error",
82 Parse(err) => return err.fmt(f),
83 };
84 write!(f, "Error: {}", err)
85 }
86}
87
88#[cfg(feature = "native-tls")]
89impl From<native_tls::Error> for Error {
90 fn from(_e: native_tls::Error) -> Self {
91 Error::Tls
92 }
93}
94
95#[cfg(feature = "native-tls")]
96impl<T> From<native_tls::HandshakeError<T>> for Error {
97 fn from(_e: native_tls::HandshakeError<T>) -> Self {
98 Error::Tls
99 }
100}
101
102impl From<io::Error> for Error {
103 fn from(e: io::Error) -> Self {
104 Error::IO(e)
105 }
106}
107
108impl From<ParseErr> for Error {
109 fn from(e: ParseErr) -> Self {
110 Error::Parse(e)
111 }
112}
113
114impl From<str::Utf8Error> for Error {
115 fn from(e: str::Utf8Error) -> Self {
116 Error::Parse(ParseErr::Utf8(e))
117 }
118}