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
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::result;
use std::str;
use std::str::Utf8Error;
use protobuf::ProtobufError;
pub(crate) fn new_error(kind: ErrorKind) -> Error {
Error(Box::new(kind))
}
pub(crate) fn new_blob_error(kind: BlobError) -> Error {
Error(Box::new(ErrorKind::Blob(kind)))
}
pub(crate) fn new_protobuf_error(err: ProtobufError, location: &'static str) -> Error {
Error(Box::new(ErrorKind::Protobuf{ err, location }))
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error(Box<ErrorKind>);
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.0
}
pub fn into_kind(self) -> ErrorKind {
*self.0
}
}
#[derive(Debug)]
pub enum ErrorKind {
Io(io::Error),
Protobuf{err: ProtobufError, location: &'static str},
StringtableUtf8{err: Utf8Error, index: usize},
StringtableIndexOutOfBounds{index: usize},
Blob(BlobError),
#[doc(hidden)]
__Nonexhaustive,
}
#[derive(Debug)]
pub enum BlobError {
InvalidHeaderSize,
HeaderTooBig{
size: u64
},
MessageTooBig{
size: u64
},
Empty,
#[doc(hidden)]
__Nonexhaustive,
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
new_error(ErrorKind::Io(err))
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> io::Error {
io::Error::new(io::ErrorKind::Other, err)
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self.0 {
ErrorKind::Io(ref err) => err.description(),
ErrorKind::Protobuf{ref err, ..} => err.description(),
ErrorKind::StringtableUtf8{ref err, ..} => err.description(),
ErrorKind::StringtableIndexOutOfBounds{..} => "stringtable index out of bounds",
ErrorKind::Blob(BlobError::InvalidHeaderSize) => "blob header size could not be decoded",
ErrorKind::Blob(BlobError::HeaderTooBig{..}) => "blob header is too big",
ErrorKind::Blob(BlobError::MessageTooBig{..}) => "blob message is too big",
ErrorKind::Blob(BlobError::Empty) => "blob is missing fields 'raw' and 'zlib_data",
_ => unreachable!(),
}
}
fn cause(&self) -> Option<&StdError> {
match *self.0 {
ErrorKind::Io(ref err) => Some(err),
ErrorKind::Protobuf{ref err, ..} => Some(err),
ErrorKind::StringtableUtf8{ref err, ..} => Some(err),
ErrorKind::StringtableIndexOutOfBounds{..} => None,
ErrorKind::Blob(BlobError::InvalidHeaderSize) => None,
ErrorKind::Blob(BlobError::HeaderTooBig{..}) => None,
ErrorKind::Blob(BlobError::MessageTooBig{..}) => None,
ErrorKind::Blob(BlobError::Empty) => None,
_ => unreachable!(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.0 {
ErrorKind::Io(ref err) => err.fmt(f),
ErrorKind::Protobuf{ref err, location} => {
write!(f, "protobuf error at '{}': {}", location, err)
},
ErrorKind::StringtableUtf8{ref err, index} => {
write!(f, "invalid UTF-8 at string table index {}: {}", index, err)
}
ErrorKind::StringtableIndexOutOfBounds { index } => {
write!(f, "stringtable index out of bounds: {}", index)
},
ErrorKind::Blob(BlobError::InvalidHeaderSize) => {
write!(f, "blob header size could not be decoded")
},
ErrorKind::Blob(BlobError::HeaderTooBig { size }) => {
write!(f, "blob header is too big: {} bytes", size)
},
ErrorKind::Blob(BlobError::MessageTooBig { size }) => {
write!(f, "blob message is too big: {} bytes", size)
},
ErrorKind::Blob(BlobError::Empty) => {
write!(f, "blob is missing fields 'raw' and 'zlib_data'")
},
_ => unreachable!(),
}
}
}