diff options
| author | Teddy Wing | 2021-03-14 03:32:18 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2021-03-14 03:32:18 +0100 | 
| commit | dca32c260c0ecd85e01a7bbef07192bc4c8a2290 (patch) | |
| tree | 339271d9b93b428ddfc277db57a9f5522a12c06f | |
| parent | 4abb14e1d3ea8fe2499e0c9f428ecc998f685465 (diff) | |
| download | mutt-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.
| -rw-r--r-- | Cargo.lock | 56 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/main.rs | 24 | 
3 files changed, 78 insertions, 3 deletions
| @@ -91,17 +91,73 @@ version = "0.0.1"  dependencies = [   "anyhow",   "mailparse", + "thiserror",   "whatlang",   "xdg",  ]  [[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]]  name = "quoted_printable"  version = "0.4.2"  source = "registry+https://github.com/rust-lang/crates.io-index"  checksum = "47b080c5db639b292ac79cbd34be0cfc5d36694768d8341109634d90b86930e2"  [[package]] +name = "syn" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd9bc7ccc2688b3344c2f48b9b546648b25ce0b20fc717ee7fa7981a8ca9717" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "thiserror" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]]  name = "whatlang"  version = "0.11.1"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -6,5 +6,6 @@ edition = "2018"  [dependencies]  anyhow = "1.0.38"  mailparse = "0.13.2" +thiserror = "1.0.24"  whatlang = "0.11.1"  xdg = "2.2.0" 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)?; | 
