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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use std::fmt::{self, Display};
use std::io;
use failure::{Fail, Context, Backtrace};
use internals::error::EncodingError;
use headers::error::{
BuildInValidationError,
HeaderTypeError, ComponentCreationError,
HeaderValidationError
};
use ::IRI;
#[derive(Debug, Fail)]
pub enum ResourceError {
#[fail(display = "{}", _0)]
Loading(ResourceLoadingError),
#[fail(display = "{}", _0)]
Encoding(EncodingError)
}
impl From<EncodingError> for ResourceError {
fn from(err: EncodingError) -> Self {
ResourceError::Encoding(err)
}
}
impl From<ResourceLoadingError> for ResourceError {
fn from(err: ResourceLoadingError) -> Self {
ResourceError::Loading(err)
}
}
#[derive(Copy, Clone, Debug, Fail, PartialEq, Eq, Hash)]
pub enum ResourceLoadingErrorKind {
#[fail(display = "resource not found")]
NotFound,
#[fail(display = "loading failed")]
LoadingFailed,
#[fail(display = "automatically detecting the media type failed")]
MediaTypeDetectionFailed
}
#[derive(Debug)]
pub struct ResourceLoadingError {
inner: Context<ResourceLoadingErrorKind>,
iri: Option<IRI>
}
impl Display for ResourceLoadingError {
fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.inner, fter)
}
}
impl Fail for ResourceLoadingError {
fn cause(&self) -> Option<&Fail> {
self.inner.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}
impl ResourceLoadingError {
pub fn kind(&self) -> ResourceLoadingErrorKind {
*self.inner.get_context()
}
pub fn source_iri(&self) -> Option<&IRI> {
self.iri.as_ref()
}
pub fn with_source_iri_or_else<F>(mut self, func: F) -> Self
where F: FnOnce() -> Option<IRI>
{
if self.iri.is_none() {
self.iri = func();
}
self
}
}
impl From<ResourceLoadingErrorKind> for ResourceLoadingError {
fn from(err: ResourceLoadingErrorKind) -> Self {
ResourceLoadingError::from((None, err))
}
}
impl From<Context<ResourceLoadingErrorKind>> for ResourceLoadingError {
fn from(inner: Context<ResourceLoadingErrorKind>) -> Self {
ResourceLoadingError::from((None, inner))
}
}
impl From<(IRI, ResourceLoadingErrorKind)> for ResourceLoadingError {
fn from((iri, error_kind): (IRI, ResourceLoadingErrorKind)) -> Self {
ResourceLoadingError::from((Some(iri), error_kind))
}
}
impl From<(IRI, Context<ResourceLoadingErrorKind>)> for ResourceLoadingError {
fn from((iri, inner): (IRI, Context<ResourceLoadingErrorKind>)) -> Self {
ResourceLoadingError::from((Some(iri), inner))
}
}
impl From<(Option<IRI>, ResourceLoadingErrorKind)> for ResourceLoadingError {
fn from((iri, error_kind): (Option<IRI>, ResourceLoadingErrorKind)) -> Self {
ResourceLoadingError::from((iri, Context::new(error_kind)))
}
}
impl From<(Option<IRI>, Context<ResourceLoadingErrorKind>)> for ResourceLoadingError {
fn from((iri, inner): (Option<IRI>, Context<ResourceLoadingErrorKind>)) -> Self {
ResourceLoadingError {
inner, iri
}
}
}
impl From<io::Error> for ResourceLoadingError {
fn from(err: io::Error) -> Self {
err.context(ResourceLoadingErrorKind::LoadingFailed).into()
}
}
#[derive(Debug, Fail)]
pub enum OtherValidationError {
#[fail(display = "Content-Type header given for non multipart mail")]
ContentTypeHeaderGiven,
#[fail(display = "Content-Transfer-Encoding header given")]
ContentTransferEncodingHeaderGiven,
#[fail(display = "found non multipart content type in multipart mail")]
SingleMultipartMixup,
#[fail(display = "inserting Content-Type for singlepart body is not allowed")]
InsertSinglepartContentTypeHeader,
#[fail(display = "multipart mail does not contain a content type header")]
MissingContentTypeHeader,
#[fail(display = "mail did not contain a From header")]
NoFrom
}
impl From<OtherValidationError> for HeaderValidationError {
fn from(oe: OtherValidationError) -> Self {
let err: ::failure::Error = oe.into();
HeaderValidationError::Custom(err)
}
}
impl From<OtherValidationError> for MailError {
fn from(oe: OtherValidationError) -> Self {
let val_err = HeaderValidationError::from(oe);
MailError::from(val_err)
}
}
#[derive(Debug, Fail)]
pub enum MailError {
#[fail(display = "{}", _0)]
Encoding(EncodingError),
#[fail(display = "{}", _0)]
Type(HeaderTypeError),
#[fail(display = "{}", _0)]
Component(ComponentCreationError),
#[fail(display = "{}", _0)]
Validation(HeaderValidationError),
#[fail(display = "{}", _0)]
ResourceLoading(ResourceLoadingError)
}
impl From<BuildInValidationError> for MailError {
fn from(err: BuildInValidationError) -> Self {
MailError::Validation(err.into())
}
}
impl From<HeaderTypeError> for MailError {
fn from(err: HeaderTypeError) -> Self {
MailError::Type(err)
}
}
impl From<EncodingError> for MailError {
fn from(err: EncodingError) -> Self {
MailError::Encoding(err)
}
}
impl From<HeaderValidationError> for MailError {
fn from(err: HeaderValidationError) -> Self {
MailError::Validation(err)
}
}
impl From<ResourceLoadingError> for MailError {
fn from(err: ResourceLoadingError) -> Self {
MailError::ResourceLoading(err)
}
}
impl From<ResourceError> for MailError {
fn from(err: ResourceError) -> Self {
match err {
ResourceError::Loading(err) => MailError::ResourceLoading(err),
ResourceError::Encoding(err) => MailError::Encoding(err)
}
}
}
impl From<ComponentCreationError> for MailError {
fn from(err: ComponentCreationError) -> Self {
MailError::Component(err)
}
}
#[derive(Copy, Clone, Debug, Fail)]
pub enum ResourceNotUnloadableError {
#[fail(display = "resource is in use, can't unload it")]
InUse,
#[fail(display = "resource has no source, can't unload it")]
NoSource
}