aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2021-03-14 03:32:18 +0100
committerTeddy Wing2021-03-14 03:32:18 +0100
commitdca32c260c0ecd85e01a7bbef07192bc4c8a2290 (patch)
tree339271d9b93b428ddfc277db57a9f5522a12c06f /src
parent4abb14e1d3ea8fe2499e0c9f428ecc998f685465 (diff)
downloadmutt-ottolangy-dca32c260c0ecd85e01a7bbef07192bc4c8a2290.tar.bz2
Wrap errors in a custom `OttolangyError` type
Give us a way to match on the type of error so we can return an appropriate exit code.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index 44031b0..2775155 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,6 +15,7 @@
use anyhow::{anyhow, Context, Error};
use mailparse;
+use thiserror;
use whatlang::{self, Lang};
use xdg;
@@ -38,6 +39,23 @@ const ATTRIBUTION_EN: &'static str =
"#;
+#[derive(thiserror::Error, Debug)]
+enum OttolangyError {
+ #[error("unable to parse email body: {0}")]
+ ParseMail(#[from] mailparse::MailParseError),
+
+ #[error("unable to parse email body")]
+ ParseMailUnknown,
+
+ #[error(transparent)]
+ Xdg(#[from] xdg::BaseDirectoriesError),
+
+ #[error(transparent)]
+ Io(#[from] std::io::Error),
+}
+
+
+// TODO: exit codes
fn main() {
match run() {
Ok(_) => (),
@@ -76,7 +94,7 @@ fn run() -> Result<(), Error> {
///
/// Given an email as input, parses it and extracts the body. For multipart
/// emails, the body is extracted from the text part.
-fn get_email_body(email: &[u8]) -> Result<String, Error> {
+fn get_email_body(email: &[u8]) -> Result<String, OttolangyError> {
let email = mailparse::parse_mail(&email)?;
if email.subparts.is_empty() {
@@ -95,13 +113,13 @@ fn get_email_body(email: &[u8]) -> Result<String, Error> {
}
}
- Err(anyhow!("unable to parse email body"))
+ Err(OttolangyError::ParseMailUnknown)
}
/// Write the attribution config to a file.
///
/// Store the file in the XDG data directory.
-fn write_attribution(config: &str) -> Result<(), Error> {
+fn write_attribution(config: &str) -> Result<(), OttolangyError> {
let xdg_dirs = xdg::BaseDirectories::with_prefix(PROGRAM_NAME)?;
let muttrc_path = xdg_dirs.place_data_file(MUTTRC_FILENAME)?;