-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy patherror.rs
More file actions
170 lines (140 loc) · 4.38 KB
/
Copy patherror.rs
File metadata and controls
170 lines (140 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! error system used around the library.
use std::{error, fmt, io, num, str, sync::mpsc};
/// Enum representing different parsing errors encountered by the library.
#[derive(Debug, PartialEq)]
pub enum ParseErr {
/// Error related to invalid UTF-8 character sequences encountered
/// during string processing or conversion operations.
Utf8(str::Utf8Error),
/// Failure in parsing integer values from strings using standard
/// number formats, such as those conforming to base 10 conventions.
Int(num::ParseIntError),
/// Issue encountered when processing status line from HTTP response message.
StatusErr,
/// Issue encountered when processing headers from HTTP response message.
HeadersErr,
/// Issue arising while processing URIs that contain invalid
/// characters or do not follow the URI specification.
UriErr,
/// Error indicating that provided string, vector, or other element
/// does not contain any values that could be parsed.
Empty,
}
impl error::Error for ParseErr {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
use self::ParseErr::*;
match self {
Utf8(e) => Some(e),
Int(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for ParseErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ParseErr::*;
let err = match self {
Utf8(_) => "Invalid character sequence",
Int(_) => "Cannot parse number",
StatusErr => "Status line contains invalid values",
HeadersErr => "Headers contain invalid values",
UriErr => "URI contains invalid characters",
Empty => "Nothing to parse",
};
write!(f, "ParseErr: {}", err)
}
}
impl From<num::ParseIntError> for ParseErr {
fn from(e: num::ParseIntError) -> Self {
ParseErr::Int(e)
}
}
impl From<str::Utf8Error> for ParseErr {
fn from(e: str::Utf8Error) -> Self {
ParseErr::Utf8(e)
}
}
/// Enum representing various errors encountered by the library.
#[derive(Debug)]
pub enum Error {
/// IO error that occurred during file operations,
/// network connections, or any other type of I/O operation.
IO(io::Error),
/// Error encountered while parsing data using the library's functions.
Parse(ParseErr),
/// Timeout error, indicating that an operation timed out
/// after waiting for the specified duration.
Timeout,
/// Error encountered while using TLS/SSL cryptographic protocols,
/// such as establishing secure connections with servers.
Tls,
/// Thread-related communication error, signifying an issue
/// that occurred during inter-thread communication.
Thread,
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
use self::Error::*;
match self {
IO(e) => Some(e),
Parse(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
let err = match self {
IO(e) => &format!("IO Error - {}", e),
Parse(e) => return e.fmt(f),
Timeout => "Timeout error",
Tls => "TLS error",
Thread => "Thread communication error",
};
write!(f, "Error: {}", err)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::IO(e)
}
}
impl From<ParseErr> for Error {
fn from(e: ParseErr) -> Self {
Error::Parse(e)
}
}
impl From<str::Utf8Error> for Error {
fn from(e: str::Utf8Error) -> Self {
Error::Parse(ParseErr::Utf8(e))
}
}
impl From<mpsc::RecvTimeoutError> for Error {
fn from(_e: mpsc::RecvTimeoutError) -> Self {
Error::Timeout
}
}
#[cfg(feature = "rust-tls")]
impl From<rustls::Error> for Error {
fn from(_e: rustls::Error) -> Self {
Error::Tls
}
}
#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
fn from(_e: native_tls::Error) -> Self {
Error::Tls
}
}
#[cfg(feature = "native-tls")]
impl<T> From<native_tls::HandshakeError<T>> for Error {
fn from(_e: native_tls::HandshakeError<T>) -> Self {
Error::Tls
}
}
impl<T> From<mpsc::SendError<T>> for Error {
fn from(_e: mpsc::SendError<T>) -> Self {
Error::Thread
}
}