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
use std::convert::From;
use std::fmt;
use std::io;
use std::iter;
#[derive(Debug)]
pub enum DecodeError {
IncompleteData {
expected_size: usize,
actual_size: usize,
},
InvalidHeader {
line: String,
position: usize,
},
InvalidChecksum,
IoError(io::Error),
}
#[derive(Debug)]
pub enum EncodeError {
PartNumberMissing,
PartBeginOffsetMissing,
PartEndOffsetMissing,
PartOffsetsInvalidRange,
IoError(io::Error),
}
impl From<io::Error> for DecodeError {
fn from(error: io::Error) -> DecodeError {
DecodeError::IoError(error)
}
}
impl From<io::Error> for EncodeError {
fn from(error: io::Error) -> EncodeError {
EncodeError::IoError(error)
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DecodeError::IncompleteData {
ref expected_size,
ref actual_size,
} => write!(
f,
"Incomplete data: expected size {}, actual size {}",
expected_size, actual_size
),
DecodeError::InvalidHeader { ref line, position } => write!(
f,
"Invalid header: \n{}\n{}^",
line,
iter::repeat(" ").take(position).collect::<String>()
),
DecodeError::InvalidChecksum => write!(f, "Invalid checksum"),
DecodeError::IoError(ref err) => write!(f, "I/O error {}", err),
}
}
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
EncodeError::PartNumberMissing => {
write!(f, "Multiple parts, but no part number specified.")
}
EncodeError::PartBeginOffsetMissing => {
write!(f, "Multiple parts, but no begin offset specified.")
}
EncodeError::PartEndOffsetMissing => {
write!(f, "Multiple parts, but no end offset specified.")
}
EncodeError::PartOffsetsInvalidRange => {
write!(f, "Multiple parts, begin offset larger than end offset")
}
EncodeError::IoError(ref err) => write!(f, "I/O error {}", err),
}
}
}