1 | use std::borrow::Cow; |
2 | use std::error; |
3 | use std::fmt; |
4 | use std::io::{self, Error}; |
5 | |
6 | #[derive (Debug)] |
7 | pub struct TarError { |
8 | desc: Cow<'static, str>, |
9 | io: io::Error, |
10 | } |
11 | |
12 | impl TarError { |
13 | pub fn new(desc: impl Into<Cow<'static, str>>, err: Error) -> TarError { |
14 | TarError { |
15 | desc: desc.into(), |
16 | io: err, |
17 | } |
18 | } |
19 | } |
20 | |
21 | impl error::Error for TarError { |
22 | fn description(&self) -> &str { |
23 | &self.desc |
24 | } |
25 | |
26 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
27 | Some(&self.io) |
28 | } |
29 | } |
30 | |
31 | impl fmt::Display for TarError { |
32 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
33 | self.desc.fmt(f) |
34 | } |
35 | } |
36 | |
37 | impl From<TarError> for Error { |
38 | fn from(t: TarError) -> Error { |
39 | Error::new(t.io.kind(), error:t) |
40 | } |
41 | } |
42 | |