|
1 | | -use std::{error, fmt, io}; |
| 1 | +use std::io; |
2 | 2 |
|
3 | 3 | use mailparse::MailParseError; |
| 4 | +use thiserror::Error; |
4 | 5 | use zip::result::ZipError; |
5 | 6 |
|
6 | 7 | /// The error type |
7 | | -#[derive(Debug)] |
| 8 | +#[derive(Error, Debug)] |
8 | 9 | pub enum Error { |
9 | 10 | /// I/O error |
10 | | - Io(io::Error), |
| 11 | + #[error(transparent)] |
| 12 | + Io(#[from] io::Error), |
11 | 13 | /// mail parse error |
12 | | - MailParse(MailParseError), |
| 14 | + #[error(transparent)] |
| 15 | + MailParse(#[from] MailParseError), |
13 | 16 | /// Zip parse error |
14 | | - Zip(ZipError), |
| 17 | + #[error(transparent)] |
| 18 | + Zip(#[from] ZipError), |
15 | 19 | /// Metadata field not found |
| 20 | + #[error("metadata field {0} not found")] |
16 | 21 | FieldNotFound(&'static str), |
17 | 22 | /// Unknown distribution type |
| 23 | + #[error("unknown distribution type")] |
18 | 24 | UnknownDistributionType, |
19 | 25 | /// Metadata file not found |
| 26 | + #[error("metadata file not found")] |
20 | 27 | MetadataNotFound, |
21 | 28 | /// Multiple metadata files found |
| 29 | + #[error("found multiple metadata files: {0:?}")] |
22 | 30 | MultipleMetadataFiles(Vec<String>), |
23 | 31 | } |
24 | | - |
25 | | -impl fmt::Display for Error { |
26 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
27 | | - match self { |
28 | | - Error::Io(err) => err.fmt(f), |
29 | | - Error::MailParse(err) => err.fmt(f), |
30 | | - Error::Zip(err) => err.fmt(f), |
31 | | - Error::FieldNotFound(key) => write!(f, "metadata field {} not found", key), |
32 | | - Error::UnknownDistributionType => write!(f, "unknown distribution type"), |
33 | | - Error::MetadataNotFound => write!(f, "metadata file not found"), |
34 | | - Error::MultipleMetadataFiles(files) => { |
35 | | - write!(f, "found multiple metadata files: {:?}", files) |
36 | | - } |
37 | | - } |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -impl error::Error for Error { |
42 | | - fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
43 | | - match self { |
44 | | - Error::Io(err) => Some(err), |
45 | | - Error::MailParse(err) => Some(err), |
46 | | - Error::Zip(err) => Some(err), |
47 | | - Error::FieldNotFound(_) |
48 | | - | Error::UnknownDistributionType |
49 | | - | Error::MetadataNotFound |
50 | | - | Error::MultipleMetadataFiles(_) => None, |
51 | | - } |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -impl From<io::Error> for Error { |
56 | | - fn from(err: io::Error) -> Self { |
57 | | - Self::Io(err) |
58 | | - } |
59 | | -} |
60 | | - |
61 | | -impl From<MailParseError> for Error { |
62 | | - fn from(err: MailParseError) -> Self { |
63 | | - Self::MailParse(err) |
64 | | - } |
65 | | -} |
66 | | - |
67 | | -impl From<ZipError> for Error { |
68 | | - fn from(err: ZipError) -> Self { |
69 | | - Self::Zip(err) |
70 | | - } |
71 | | -} |
0 commit comments