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
use std::error;
use std::fmt;
use std::io;
use nom::Err;
#[derive(Debug)]
pub enum Error {
IoError(io::Error),
NoFilename,
ParsingIncomplete,
ParsingError(Vec<u8>, ParsingErrorKind),
DecodeError,
}
impl From<Err<nom::error::Error<&[u8]>>> for Error {
fn from(error: Err<nom::error::Error<&[u8]>>) -> Self {
match error {
Err::Incomplete(_) => Error::ParsingIncomplete,
Err::Error(err) | Err::Failure(err) => Error::ParsingError(
err.input.to_vec(),
ParsingErrorKind::GenericParserError(err.code.description().to_string()),
),
}
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::IoError(error)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::IoError(ref x) => x.fmt(f),
Error::NoFilename => write!(f, "The plugin path has no filename part"),
Error::ParsingIncomplete => write!(f, "More input was expected by the plugin parser"),
Error::ParsingError(input, kind) => write!(
f,
"An error was encountered while parsing the plugin content {:02X?}: {}",
input, kind
),
Error::DecodeError => write!(
f,
"Plugin string content could not be decoded from Windows-1252"
),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::IoError(x) => Some(x),
_ => None,
}
}
}
#[derive(Debug)]
pub enum ParsingErrorKind {
UnexpectedRecordType(Vec<u8>),
SubrecordDataTooShort(usize),
GenericParserError(String),
}
impl fmt::Display for ParsingErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParsingErrorKind::UnexpectedRecordType(v) => {
write!(f, "Expected record type {:02X?}", v)
}
ParsingErrorKind::SubrecordDataTooShort(s) => write!(
f,
"Subrecord data field too short, expected at least {} bytes",
s
),
ParsingErrorKind::GenericParserError(e) => write!(f, "Error in parser: {}", e),
}
}
}