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
use serde::de::value::Error as DeserializeError;
use std::{error::Error as StdError, fmt, io, result};
use url::ParseError;
pub type Lib3hProtocolResult<T> = result::Result<T, Lib3hProtocolError>;
#[derive(Debug)]
pub struct Lib3hProtocolError(Box<ErrorKind>);
impl Lib3hProtocolError {
pub fn new(kind: ErrorKind) -> Self {
Lib3hProtocolError(Box::new(kind))
}
pub fn kind(&self) -> &ErrorKind {
&self.0
}
pub fn into_kind(self) -> ErrorKind {
*self.0
}
}
#[derive(Debug)]
pub enum ErrorKind {
Io(io::Error),
TransportError(String),
DeserializeError(DeserializeError),
Lib3hError(String, Option<backtrace::Backtrace>),
UrlError(ParseError),
Other(String),
#[doc(hidden)]
__Nonexhaustive,
}
impl StdError for Lib3hProtocolError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self.0 {
ErrorKind::Io(ref err) => Some(err),
ErrorKind::DeserializeError(ref err) => Some(err),
ErrorKind::Lib3hError(ref _s, ref _bt) => None,
ErrorKind::UrlError(ref err) => Some(err),
ErrorKind::Other(ref _s) | ErrorKind::TransportError(ref _s) => None,
_ => unreachable!(),
}
}
}
impl fmt::Display for Lib3hProtocolError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
ErrorKind::Io(ref err) => err.fmt(f),
ErrorKind::TransportError(ref s) => write!(f, "TransportError: '{}'.", s),
ErrorKind::DeserializeError(ref err) => err.fmt(f),
ErrorKind::Lib3hError(ref s, ref _bt) => write!(f, "Li3hError encountered: '{}'.", s),
ErrorKind::UrlError(ref s) => write!(f, "Url ParseError: '{}'.", s),
ErrorKind::Other(ref s) => write!(f, "Unknown error encountered: '{}'.", s),
_ => unreachable!(),
}
}
}
impl From<io::Error> for Lib3hProtocolError {
fn from(err: io::Error) -> Self {
Lib3hProtocolError::new(ErrorKind::Io(err))
}
}
impl From<DeserializeError> for Lib3hProtocolError {
fn from(err: DeserializeError) -> Self {
Lib3hProtocolError::new(ErrorKind::DeserializeError(err))
}
}
impl From<ParseError> for Lib3hProtocolError {
fn from(err: ParseError) -> Self {
Lib3hProtocolError::new(ErrorKind::UrlError(err))
}
}