1use std::{error, fmt, io, num, str, sync::mpsc};
4
5#[derive(Debug, PartialEq)]
7pub enum ParseErr {
8 Utf8(str::Utf8Error),
11
12 Int(num::ParseIntError),
15
16 StatusErr,
18
19 HeadersErr,
21
22 UriErr,
25
26 Empty,
29}
30
31impl error::Error for ParseErr {
32 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
33 use self::ParseErr::*;
34
35 match self {
36 Utf8(e) => Some(e),
37 Int(e) => Some(e),
38 _ => None,
39 }
40 }
41}
42
43impl fmt::Display for ParseErr {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 use self::ParseErr::*;
46
47 let err = match self {
48 Utf8(_) => "Invalid character sequence",
49 Int(_) => "Cannot parse number",
50 StatusErr => "Status line contains invalid values",
51 HeadersErr => "Headers contain invalid values",
52 UriErr => "URI contains invalid characters",
53 Empty => "Nothing to parse",
54 };
55 write!(f, "ParseErr: {}", err)
56 }
57}
58
59impl From<num::ParseIntError> for ParseErr {
60 fn from(e: num::ParseIntError) -> Self {
61 ParseErr::Int(e)
62 }
63}
64
65impl From<str::Utf8Error> for ParseErr {
66 fn from(e: str::Utf8Error) -> Self {
67 ParseErr::Utf8(e)
68 }
69}
70
71#[derive(Debug)]
73pub enum Error {
74 IO(io::Error),
77
78 Parse(ParseErr),
80
81 Timeout,
84
85 Tls,
88
89 Thread,
92}
93
94impl error::Error for Error {
95 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
96 use self::Error::*;
97
98 match self {
99 IO(e) => Some(e),
100 Parse(e) => Some(e),
101 _ => None,
102 }
103 }
104}
105
106impl fmt::Display for Error {
107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108 use self::Error::*;
109
110 let err = match self {
111 IO(e) => &format!("IO Error - {}", e),
112 Parse(e) => return e.fmt(f),
113 Timeout => "Timeout error",
114 Tls => "TLS error",
115 Thread => "Thread communication error",
116 };
117 write!(f, "Error: {}", err)
118 }
119}
120
121impl From<io::Error> for Error {
122 fn from(e: io::Error) -> Self {
123 Error::IO(e)
124 }
125}
126
127impl From<ParseErr> for Error {
128 fn from(e: ParseErr) -> Self {
129 Error::Parse(e)
130 }
131}
132
133impl From<str::Utf8Error> for Error {
134 fn from(e: str::Utf8Error) -> Self {
135 Error::Parse(ParseErr::Utf8(e))
136 }
137}
138
139impl From<mpsc::RecvTimeoutError> for Error {
140 fn from(_e: mpsc::RecvTimeoutError) -> Self {
141 Error::Timeout
142 }
143}
144
145#[cfg(feature = "rust-tls")]
146impl From<rustls::Error> for Error {
147 fn from(_e: rustls::Error) -> Self {
148 Error::Tls
149 }
150}
151
152#[cfg(feature = "native-tls")]
153impl From<native_tls::Error> for Error {
154 fn from(_e: native_tls::Error) -> Self {
155 Error::Tls
156 }
157}
158
159#[cfg(feature = "native-tls")]
160impl<T> From<native_tls::HandshakeError<T>> for Error {
161 fn from(_e: native_tls::HandshakeError<T>) -> Self {
162 Error::Tls
163 }
164}
165
166impl<T> From<mpsc::SendError<T>> for Error {
167 fn from(_e: mpsc::SendError<T>) -> Self {
168 Error::Thread
169 }
170}