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
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::io::ErrorKind::InvalidInput;
use std::result::Result as StdResult;
use std::string;
use byteorder;
pub type Result<T> = StdResult<T, Error>;
#[derive(Debug)]
pub enum Error {
IoError(io::Error),
InvalidTypeId(u8),
HeterogeneousList,
NoRootCompound,
InvalidUtf8,
IncompleteNbtValue,
TagMismatch(u8, u8),
UnexpectedField(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Error::IoError(ref e) => e.fmt(f),
other => write!(f, "{}", other.description()),
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::IoError(ref e) => e.description(),
Error::InvalidTypeId(_) => "invalid NBT tag byte",
Error::HeterogeneousList => "values in NBT Lists must be homogeneous",
Error::NoRootCompound => "the root value must be Compound-like (tag = 0x0a)",
Error::InvalidUtf8 => "a string is not valid UTF-8",
Error::IncompleteNbtValue => "data does not represent a complete NbtValue",
Error::TagMismatch(_, _) => "encountered one NBT tag but expected another",
Error::UnexpectedField(_) => "encountered an unexpected field",
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
Error::IoError(ref e) => e.cause(),
_ => None
}
}
}
impl PartialEq<Error> for Error {
fn eq(&self, other: &Error) -> bool {
use Error::{IoError, InvalidTypeId, HeterogeneousList, NoRootCompound,
InvalidUtf8, IncompleteNbtValue, TagMismatch, UnexpectedField};
match (self, other) {
(&IoError(_), &IoError(_)) => true,
(&InvalidTypeId(a), &InvalidTypeId(b)) => a == b,
(&HeterogeneousList, &HeterogeneousList) => true,
(&NoRootCompound, &NoRootCompound) => true,
(&InvalidUtf8, &InvalidUtf8) => true,
(&IncompleteNbtValue, &IncompleteNbtValue) => true,
(&TagMismatch(a, b), &TagMismatch(c, d)) => a == c && b == d,
(&UnexpectedField(ref a), &UnexpectedField(ref b)) => a == b,
_ => false
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::IoError(e)
}
}
impl From<string::FromUtf8Error> for Error {
fn from(_: string::FromUtf8Error) -> Error {
Error::InvalidUtf8
}
}
impl From<byteorder::Error> for Error {
fn from(err: byteorder::Error) -> Error {
match err {
byteorder::Error::Io(e) => Error::IoError(e),
byteorder::Error::UnexpectedEOF => Error::IncompleteNbtValue
}
}
}
impl From<Error> for io::Error {
fn from(e: Error) -> io::Error {
match e {
Error::IoError(e) => e,
Error::InvalidTypeId(id) =>
io::Error::new(InvalidInput, &format!("invalid NBT tag byte: {}", id)[..]),
Error::TagMismatch(a, b) =>
io::Error::new(InvalidInput, &format!("encountered NBT tag {} \
but expected {}", a, b)[..]),
Error::UnexpectedField(f) =>
io::Error::new(InvalidInput, &format!("encountered unexpected field \
with name {}", f)[..]),
other => io::Error::new(InvalidInput, other.description()),
}
}
}