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::fmt::{self, Write};
use self::super::parse;
use std::error::Error;
use lazysort::Sorted;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HrxError {
NoBoundary,
Parse(parse::ParseError),
BodyContainsBoundary(Vec<ErroneousBodyPath>),
DuplicateEntry(String),
FileAsDirectory(String, String),
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErroneousBodyPath {
RootComment,
EntryComment(String),
EntryData(String),
}
impl From<parse::ParseError> for HrxError {
fn from(pe: parse::ParseError) -> HrxError {
HrxError::Parse(pe)
}
}
impl From<ErroneousBodyPath> for HrxError {
fn from(bcb: ErroneousBodyPath) -> HrxError {
HrxError::BodyContainsBoundary(vec![bcb])
}
}
impl<V: Into<Vec<ErroneousBodyPath>>> From<V> for HrxError {
fn from(paths: V) -> HrxError {
HrxError::BodyContainsBoundary(paths.into())
}
}
impl fmt::Display for HrxError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
&HrxError::NoBoundary => fmt.write_str("No boundary found")?,
&HrxError::Parse(ref pe) => {
write!(fmt, "Parse failed at {}:{} [position {}]: expected ", pe.line, pe.column, pe.offset)?;
for (i, x) in pe.expected.iter().sorted().enumerate() {
if i != 0 {
fmt.write_str(", ")?;
if i == pe.expected.len() - 1 {
fmt.write_str("or ")?;
}
}
if x.len() == 1 {
write!(fmt, "{:?}", x)?;
} else {
fmt.write_str(x)?;
}
}
fmt.write_str(".")?;
}
&HrxError::BodyContainsBoundary(ref paths) => {
fn first_char(fmt: &mut fmt::Formatter, c: char, first: bool, last: bool) -> fmt::Result {
if first {
write!(fmt, "{}", c.to_uppercase())?
} else {
fmt.write_str(", ")?;
if last {
fmt.write_str("and ")?;
}
fmt.write_char(c)?;
}
Ok(())
}
if !paths.is_empty() {
for (i, path) in paths.iter().enumerate() {
let first = i == 0;
let last = i == paths.len();
match path {
ErroneousBodyPath::RootComment => {
first_char(fmt, 'r', first, last)?;
fmt.write_str("oot archive comment")?
}
ErroneousBodyPath::EntryComment(ref pp) => {
first_char(fmt, 'c', first, last)?;
write!(fmt, "omment for \"{}\" entry", pp)?
}
ErroneousBodyPath::EntryData(ref pp) => {
first_char(fmt, 'd', first, last)?;
write!(fmt, "ata of \"{}\" entry", pp)?
}
}
}
fmt.write_str(" contain")?;
if paths.len() != 1 {
fmt.write_char('s')?;
}
fmt.write_str(" the archive boundary, making resulting archive not redeserialisable.")?;
} else {
fmt.write_str("No paths specified.")?;
}
}
&HrxError::DuplicateEntry(ref path) => {
fmt.write_str("Duplicate entry: ")?;
fmt.write_str(&path)?;
}
&HrxError::FileAsDirectory(ref file, ref who) => {
fmt.write_str(&who)?;
fmt.write_str(" attempted to use the file at ")?;
fmt.write_str(&file)?;
fmt.write_str(" as a directrory.")?;
}
}
Ok(())
}
}
impl Error for HrxError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
&HrxError::Parse(ref pe) => Some(pe),
_ => None,
}
}
}