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
use std::{convert, error::Error, fmt, io};
use std::fmt::{Display, Formatter};
use std::io::ErrorKind;
#[derive(Debug)]
pub enum ParserErrorKind {
IoError(io::Error),
IoNotEnoughBytes(),
EofError(io::Error),
RemoteIoError(String),
EofExpected,
ParseError(String),
UnknownAttr(String),
TruncatedMsg(String),
Deprecated(String),
Unsupported(String),
FilterError(String),
}
impl Error for ParserErrorKind {}
#[derive(Debug)]
pub struct ParserError {
pub error: ParserErrorKind,
pub bytes: Option<Vec<u8>>,
}
impl Display for ParserError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.error.to_string())
}
}
impl Error for ParserError {}
impl fmt::Display for ParserErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let message = match self {
ParserErrorKind::IoError(e) => e.to_string(),
ParserErrorKind::EofError(e) => e.to_string(),
ParserErrorKind::ParseError(s) => s.to_owned(),
ParserErrorKind::TruncatedMsg(s) => s.to_owned(),
ParserErrorKind::Deprecated(s) => s.to_owned(),
ParserErrorKind::UnknownAttr(s) => s.to_owned(),
ParserErrorKind::Unsupported(s) => s.to_owned(),
ParserErrorKind::EofExpected => "reach end of file".to_string(),
ParserErrorKind::RemoteIoError(e) => e.to_string(),
ParserErrorKind::FilterError(e) => e.to_owned(),
ParserErrorKind::IoNotEnoughBytes() => "Not enough bytes to read".to_string(),
};
write!(f, "Error: {}", message)
}
}
impl convert::From<reqwest::Error> for ParserErrorKind {
fn from(error: reqwest::Error) -> Self {
ParserErrorKind::RemoteIoError(error.to_string())
}
}
impl convert::From<ParserErrorKind> for ParserError {
fn from(error: ParserErrorKind) -> Self {
ParserError{error, bytes: None}
}
}
impl convert::From<io::Error> for ParserErrorKind {
fn from(io_error: io::Error) -> Self {
match io_error.kind() {
ErrorKind::UnexpectedEof => { ParserErrorKind::EofError(io_error)}
_ => ParserErrorKind::IoError(io_error)
}
}
}