aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2021-03-13 23:57:30 +0100
committerTeddy Wing2021-03-13 23:57:30 +0100
commit5455b0c072a7626107a5994a6e8ba32b64c2ea17 (patch)
tree7c4eec18e3efafe17fa856d28d3e113edae338fd
parent39e56152f825f3550f5fdf04b075b3fd594e97c9 (diff)
downloadmutt-ottolangy-5455b0c072a7626107a5994a6e8ba32b64c2ea17.tar.bz2
Add context to errors
Include the 'anyhow' crate to add additional context to errors.
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs21
3 files changed, 20 insertions, 9 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6512fcd..a997dcb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7,6 +7,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217"
[[package]]
+name = "anyhow"
+version = "1.0.38"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1"
+
+[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -83,6 +89,7 @@ dependencies = [
name = "ottolangy"
version = "0.0.1"
dependencies = [
+ "anyhow",
"mailparse",
"whatlang",
"xdg",
diff --git a/Cargo.toml b/Cargo.toml
index 6e47203..ad95cec 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.0.1"
edition = "2018"
[dependencies]
+anyhow = "1.0.38"
mailparse = "0.13.2"
whatlang = "0.11.1"
xdg = "2.2.0"
diff --git a/src/main.rs b/src/main.rs
index 0a7cec7..7e96024 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,8 @@
+use anyhow::{anyhow, Context, Error};
use mailparse;
use whatlang::{self, Lang};
use xdg;
-use std::error::Error;
use std::fs::File;
use std::io::{self, Read, Write};
@@ -32,16 +32,18 @@ fn main() {
/// Get an email from standard input and write a Mutt attribution config based
/// on the language.
-fn run() -> Result<(), Box<dyn Error>> {
+fn run() -> Result<(), Error> {
let mut email_input: Vec<u8> = Vec::new();
let mut stdin = io::stdin();
- stdin.read_to_end(&mut email_input)?;
+ stdin.read_to_end(&mut email_input)
+ .context("failed to read from stdin")?;
- let body = get_email_body(&email_input)?;
+ let body = get_email_body(&email_input)
+ .context("failed to parse email body")?;
let lang_info = whatlang::detect(&body)
- .ok_or("unable to detect language")?;
+ .ok_or(anyhow!("unable to detect language"))?;
let attribution_config = if lang_info.lang() == Lang::Fra {
ATTRIBUTION_FR
@@ -49,7 +51,8 @@ fn run() -> Result<(), Box<dyn Error>> {
ATTRIBUTION_EN
};
- write_attribution(&attribution_config)?;
+ write_attribution(&attribution_config)
+ .context("failed to write attribution config file")?;
Ok(())
}
@@ -58,7 +61,7 @@ fn run() -> Result<(), Box<dyn 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, Box<dyn Error>> {
+fn get_email_body(email: &[u8]) -> Result<String, Error> {
let email = mailparse::parse_mail(&email)?;
if email.subparts.is_empty() {
@@ -77,13 +80,13 @@ fn get_email_body(email: &[u8]) -> Result<String, Box<dyn Error>> {
}
}
- Err("unable to parse email body".into())
+ Err(anyhow!("unable to parse email body"))
}
/// Write the attribution config to a file.
///
/// Store the file in the XDG data directory.
-fn write_attribution(config: &str) -> Result<(), Box<dyn Error>> {
+fn write_attribution(config: &str) -> Result<(), Error> {
let xdg_dirs = xdg::BaseDirectories::with_prefix(PROGRAM_NAME)?;
let muttrc_path = xdg_dirs.place_data_file(MUTTRC_FILENAME)?;