Skip to content

Commit 19329c4

Browse files
committed
Switch to thiserror to avoid duplicate error messages
1 parent b7ac377 commit 19329c4

File tree

2 files changed

+14
-53
lines changed

2 files changed

+14
-53
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mailparse = "0.13.4"
1919
rfc2047-decoder = "0.1.2"
2020
serde = { version = "1.0.126", features = ["derive"], optional = true }
2121
tar = "0.4.35"
22+
thiserror = "1.0.30"
2223
xz = { version = "0.1.0", optional = true }
2324
zip = { version = "0.6.0", default-features = false, features = ["bzip2", "deflate", "time"] }
2425

src/error.rs

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,31 @@
1-
use std::{error, fmt, io};
1+
use std::io;
22

33
use mailparse::MailParseError;
4+
use thiserror::Error;
45
use zip::result::ZipError;
56

67
/// The error type
7-
#[derive(Debug)]
8+
#[derive(Error, Debug)]
89
pub enum Error {
910
/// I/O error
10-
Io(io::Error),
11+
#[error(transparent)]
12+
Io(#[from] io::Error),
1113
/// mail parse error
12-
MailParse(MailParseError),
14+
#[error(transparent)]
15+
MailParse(#[from] MailParseError),
1316
/// Zip parse error
14-
Zip(ZipError),
17+
#[error(transparent)]
18+
Zip(#[from] ZipError),
1519
/// Metadata field not found
20+
#[error("metadata field {0} not found")]
1621
FieldNotFound(&'static str),
1722
/// Unknown distribution type
23+
#[error("unknown distribution type")]
1824
UnknownDistributionType,
1925
/// Metadata file not found
26+
#[error("metadata file not found")]
2027
MetadataNotFound,
2128
/// Multiple metadata files found
29+
#[error("found multiple metadata files: {0:?}")]
2230
MultipleMetadataFiles(Vec<String>),
2331
}
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

Comments
 (0)