diff --git a/src/lib.rs b/src/lib.rs index 50d56f6..b7e25d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,105 @@ #[cfg(target_os = "macos")] extern crate mac_notification_sys; +#[cfg(target_os = "macos")] +use mac_notification_sys::error::{ApplicationError, NorificationError}; + #[cfg(target_os = "linux")] extern crate notify_rust; +#[cfg(target_os = "linux")] +use notify_rust::error::Error as LError; + #[cfg(target_os = "windows")] extern crate winrt; +#[cfg(target_os = "windows")] +use winrt::Error as WError; + +use std::{ + error::Error as StdError, + fmt::{self, Display, Formatter}, +}; + trait Platform { fn setup() -> Self; - fn notify(msg_title: &str, msg_body: &str); + fn notify(msg_title: &str, msg_body: &str) -> Result<(), Error>; fn teardown(self); } +#[derive(Debug)] +enum Error { + #[cfg(target_os = "linux")] + Linux(LError), + #[cfg(target_os = "macos")] + MacOs(MacOsError), + #[cfg(target_os = "windows")] + Windows(WError), +} + +impl StdError for Error {} + +#[cfg(target_os = "macos")] +enum MacOsError { + AppErr(ApplicationError), + NotErr(NorificationError), +} + +impl Display for Error { + fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { + match self { + #[cfg(target_os = "linux")] + Error::Linux(e) => write!(fmt, "{}", e), + #[cfg(target_os = "macos")] + Error::MacOs => write!(fmt, "MacOs Error"), + #[cfg(target_os = "windows")] + Error::Windows => write!(fmt, "Windows Error"), + } + } +} + +#[cfg(target_os = "macos")] +impl From for MacOsError { + fn from(err: ApplicationError) -> Self { + MacOsError::AppErr(err) + } +} + +#[cfg(target_os = "macos")] +impl From for Error { + fn from(err: ApplicationError) -> Self { + Error::MacOs(err.into()) + } +} + +#[cfg(target_os = "macos")] +impl From for MacOsError { + fn from(err: NotificationError) -> Self { + MacOsError::NotErr(err) + } +} + +#[cfg(target_os = "macos")] +impl From for Error { + fn from(err: NotificationError) -> Self { + Error::MacOs(err.into()) + } +} + +#[cfg(target_os = "linux")] +impl From for Error { + fn from(err: LError) -> Self { + Error::Linux(err) + } +} + +#[cfg(target_os = "windows")] +impl From for Error { + fn from(err: WError) -> Self { + Error::Windows(err) + } +} + #[cfg(target_os = "windows")] struct Windows(winrt::RuntimeContext); @@ -22,54 +109,32 @@ impl Platform for Windows { Windows(winrt::RuntimeContext::init()) } - fn notify(msg_title: &str, msg_body: &str) { + fn notify(msg_title: &str, msg_body: &str) -> Result<(), Error> { use winrt::windows::data::xml::dom::*; use winrt::windows::ui::notifications::*; use winrt::*; let toast_xml = - ToastNotificationManager::get_template_content(ToastTemplateType::ToastText02) - .unwrap() - .unwrap(); - let toast_text_elements = toast_xml - .get_elements_by_tag_name(&FastHString::new("text")) - .unwrap() - .unwrap(); - - toast_text_elements - .item(0) - .unwrap() - .unwrap() - .append_child( - &*toast_xml - .create_text_node(&FastHString::from(msg_title)) - .unwrap() - .unwrap() - .query_interface::() - .unwrap(), - ) - .unwrap(); - toast_text_elements - .item(1) - .unwrap() - .unwrap() - .append_child( - &*toast_xml - .create_text_node(&FastHString::from(msg_body)) - .unwrap() - .unwrap() - .query_interface::() - .unwrap(), - ) - .unwrap(); - - let toast = ToastNotification::create_toast_notification(&*toast_xml).unwrap(); + ToastNotificationManager::get_template_content(ToastTemplateType::ToastText02)??; + let toast_text_elements = + toast_xml.get_elements_by_tag_name(&FastHString::new("text"))??; + + toast_text_elements.item(0)??.append_child( + &*toast_xml + .create_text_node(&FastHString::from(msg_title))?? + .query_interface::()?, + )?; + toast_text_elements.item(1)??.append_child( + &*toast_xml + .create_text_node(&FastHString::from(msg_body))?? + .query_interface::()?, + )?; + + let toast = ToastNotification::create_toast_notification(&*toast_xml)?; ToastNotificationManager::create_toast_notifier_with_id(&FastHString::new( "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe", - )) - .unwrap() - .unwrap() - .show(&*toast) - .unwrap(); + ))?? + .show(&*toast)?; + Ok(()) } fn teardown(self) { @@ -86,10 +151,10 @@ impl Platform for MacOs { MacOs } - fn notify(msg_title: &str, msg_body: &str) { - let bundle = mac_notification_sys::get_bundle_identifier("Script Editor").unwrap(); - mac_notification_sys::set_application(&bundle).unwrap(); - mac_notification_sys::send_notification(msg_title, &None, msg_body, &None).unwrap(); + fn notify(msg_title: &str, msg_body: &str) -> Result<(), Error> { + let bundle = mac_notification_sys::get_bundle_identifier("Script Editor")?; + mac_notification_sys::set_application(&bundle)?; + mac_notification_sys::send_notification(msg_title, &None, msg_body, &None)?; } fn teardown(self) {} @@ -104,12 +169,12 @@ impl Platform for Linux { Linux } - fn notify(msg_title: &str, msg_body: &str) { + fn notify(msg_title: &str, msg_body: &str) -> Result<(), Error> { notify_rust::Notification::new() .summary(msg_title) .body(msg_body) - .show() - .unwrap(); + .show()?; + Ok(()) } fn teardown(self) {} -- 2.51.2 From 7f6b37f7fe0a86c9e30e30eafae7b90fa1a58107 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 17 Apr 2019 23:46:57 +0200 Subject: [PATCH 2/2] Fix typo --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b7e25d9..966be31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ extern crate mac_notification_sys; #[cfg(target_os = "macos")] -use mac_notification_sys::error::{ApplicationError, NorificationError}; +use mac_notification_sys::error::{ApplicationError, NotificationError}; #[cfg(target_os = "linux")] extern crate notify_rust; @@ -42,7 +42,7 @@ impl StdError for Error {} #[cfg(target_os = "macos")] enum MacOsError { AppErr(ApplicationError), - NotErr(NorificationError), + NotErr(NotificationError), } impl Display for Error {