From b067ba86892e8b3acee3fa11487e2f7cfad9e4a8 Mon Sep 17 00:00:00 2001 From: lelgenio Date: Fri, 4 Mar 2022 09:46:14 -0300 Subject: [PATCH] better error handling and add README.md --- README.md | 13 +++++++++++++ src/main.rs | 55 +++++++++++++++++++---------------------------------- 2 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee22506 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Maildir Notification Daemon + +Waits for new files on provided dirs, parse them as email and send a notification with author and subject. + +```sh +maildir-notify-daemon ~/.local/share/mail/*/*/new/ +``` + +## Try it without sending emails + +```sh +cp ~/.local/share/mail/personal/INBOX/cur/[ANY_MAIL_FILE] ~/.local/share/mail/personal/INBOX/new/ +``` diff --git a/src/main.rs b/src/main.rs index cd0f63a..fc04a6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,55 +15,39 @@ fn main() { Hotwatch::new_with_custom_delay(std::time::Duration::from_secs(0)) .expect("hotwatch failed to initialize!"); - std::env::args().for_each(|arg| { - hotwatch.watch(arg, handle_event).ok(); - }); + for arg in std::env::args() { + if let Err(e) = hotwatch.watch(&arg, handle_event) { + eprintln!("Failed to watch arg {} with error: {}", &arg, e); + } + } hotwatch.run(); } fn handle_event(event: hotwatch::Event) -> hotwatch::blocking::Flow { if let hotwatch::Event::Create(newfile) = event { - _handle_event(newfile) + if let Err(e) = _handle_event(newfile) { + eprintln!("{}", e); + }; }; Flow::Continue } -fn _handle_event(newfile: PathBuf) { - let raw_content = match std::fs::read(&newfile) { - Ok(c) => c, - Err(e) => { - eprintln!( - "Cannot read file '{}': {:?}", - newfile.to_string_lossy(), - e - ); - return; - } - }; +fn _handle_event(newfile: PathBuf) -> Result<(), String> { + let raw_content = std::fs::read(&newfile).map_err(|e| { + format!("Cannot read file '{}': {:?}", newfile.to_string_lossy(), e) + })?; - let mail_content = match mailparse::parse_mail(&raw_content) { - Ok(c) => c, - Err(e) => { - eprintln!( - "Cannot parse file '{}': {:?}", - newfile.to_string_lossy(), - e - ); - return; - } - }; + let mail_content = mailparse::parse_mail(&raw_content).map_err(|e| { + format!("Cannot parse file '{}': {:?}", newfile.to_string_lossy(), e) + })?; let headers = mail_content.get_headers(); - let from = match headers.get_first_value("From") { - Some(f) => f, - None => { - eprintln!("Cannot parse file '{}'", newfile.to_string_lossy(),); - return; - } - }; + let from = headers.get_first_value("From").ok_or_else(|| { + format!("Cannot parse file '{}'", newfile.to_string_lossy()) + })?; let subject = headers.get_first_value("Subject").unwrap_or_else(|| { mail_content @@ -81,5 +65,6 @@ fn _handle_event(newfile: PathBuf) { .body(&format!("Subject: {}", subject)) .icon("mail-unread-symbolic") .show() - .ok(); + .map(|_| ()) + .map_err(|e| format!("Could not send notification: {}", e)) }