Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unwrap calls #10

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 115 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,105 @@
#[cfg(target_os = "macos")]
extern crate mac_notification_sys;

#[cfg(target_os = "macos")]
use mac_notification_sys::error::{ApplicationError, NotificationError};

#[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(NotificationError),
}

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<ApplicationError> for MacOsError {
fn from(err: ApplicationError) -> Self {
MacOsError::AppErr(err)
}
}

#[cfg(target_os = "macos")]
impl From<ApplicationError> for Error {
fn from(err: ApplicationError) -> Self {
Error::MacOs(err.into())
}
}

#[cfg(target_os = "macos")]
impl From<NotificationError> for MacOsError {
fn from(err: NotificationError) -> Self {
MacOsError::NotErr(err)
}
}

#[cfg(target_os = "macos")]
impl From<NotificationError> for Error {
fn from(err: NotificationError) -> Self {
Error::MacOs(err.into())
}
}

#[cfg(target_os = "linux")]
impl From<LError> for Error {
fn from(err: LError) -> Self {
Error::Linux(err)
}
}

#[cfg(target_os = "windows")]
impl From<WError> for Error {
fn from(err: WError) -> Self {
Error::Windows(err)
}
}

#[cfg(target_os = "windows")]
struct Windows(winrt::RuntimeContext);

Expand All @@ -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::<IXmlNode>()
.unwrap(),
)
.unwrap();
toast_text_elements
.item(1)
.unwrap()
.unwrap()
.append_child(
&*toast_xml
.create_text_node(&FastHString::from(msg_body))
.unwrap()
.unwrap()
.query_interface::<IXmlNode>()
.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::<IXmlNode>()?,
)?;
toast_text_elements.item(1)??.append_child(
&*toast_xml
.create_text_node(&FastHString::from(msg_body))??
.query_interface::<IXmlNode>()?,
)?;

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) {
Expand All @@ -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) {}
Expand All @@ -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) {}
Expand Down