1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#[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")]
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) -> Result<(), ErrorRepr>;
}

#[derive(Debug)]
pub struct Error(ErrorRepr);

#[derive(Debug)]
enum ErrorRepr {
    #[cfg(target_os = "linux")]
    Linux(LError),
    #[cfg(target_os = "macos")]
    MacOs(MacOsError),
    #[cfg(target_os = "windows")]
    Windows(WError),
}

impl StdError for Error {}
impl StdError for ErrorRepr {}

#[cfg(target_os = "macos")]
#[derive(Debug)]
enum MacOsError {
    AppErr(ApplicationError),
    NotErr(NotificationError),
}

#[cfg(target_os = "macos")]
impl Display for MacOsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MacOsError::AppErr(e) => Display::fmt(e, f),
            MacOsError::NotErr(e) => Display::fmt(e, f),
        }
    }
}

impl Display for Error {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
        self.0.fmt(fmt)
    }
}

impl Display for ErrorRepr {
    fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
        match self {
            #[cfg(target_os = "linux")]
            ErrorRepr::Linux(e) => write!(fmt, "{}", e),
            #[cfg(target_os = "macos")]
            ErrorRepr::MacOs(e) => write!(fmt, "{}", e),
            #[cfg(target_os = "windows")]
            ErrorRepr::Windows(e) => write!(fmt, "{:?}", e),
        }
    }
}

#[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 ErrorRepr {
    fn from(err: ApplicationError) -> Self {
        ErrorRepr::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 ErrorRepr {
    fn from(err: NotificationError) -> Self {
        ErrorRepr::MacOs(err.into())
    }
}

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

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

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

#[cfg(target_os = "windows")]
impl Platform for Windows {
    fn setup() -> Self {
        Windows
    }

    fn notify(msg_title: &str, msg_body: &str) -> Result<(), ErrorRepr> {
        use winrt::windows::data::xml::dom::*;
        use winrt::windows::ui::notifications::*;
        use winrt::*;
        let toast_xml =
            ToastNotificationManager::get_template_content(ToastTemplateType::ToastText02)?.unwrap();
        let toast_text_elements =
            toast_xml.get_elements_by_tag_name(&FastHString::new("text"))?.unwrap();

        toast_text_elements.item(0)?.unwrap().append_child(
            &*toast_xml
                .create_text_node(&FastHString::from(msg_title))?.unwrap()
                .query_interface::<IXmlNode>().unwrap(),
        )?;
        toast_text_elements.item(1)?.unwrap().append_child(
            &*toast_xml
                .create_text_node(&FastHString::from(msg_body))?.unwrap()
                .query_interface::<IXmlNode>().unwrap(),
        )?;

        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()
            .show(&*toast)?;
        Ok(())
    }
}

#[cfg(target_os = "macos")]
struct MacOs;

#[cfg(target_os = "macos")]
impl Platform for MacOs {
    fn setup() -> Self {
        MacOs
    }

    fn notify(msg_title: &str, msg_body: &str) -> Result<(), ErrorRepr> {
        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();
        Ok(())
    }
}

#[cfg(target_os = "linux")]
struct Linux;

#[cfg(target_os = "linux")]
impl Platform for Linux {
    fn setup() -> Self {
        Linux
    }

    fn notify(msg_title: &str, msg_body: &str) -> Result<(), ErrorRepr> {
        notify_rust::Notification::new()
            .summary(msg_title)
            .body(msg_body)
            .show()?;
        Ok(())
    }
}

#[cfg(target_os = "windows")]
type CurrPlatform = Windows;
#[cfg(target_os = "macos")]
type CurrPlatform = MacOs;
#[cfg(target_os = "linux")]
type CurrPlatform = Linux;

pub fn notify(msg_title: &str, msg_body: &str) -> Result<(), Error> {
    CurrPlatform::setup();
    CurrPlatform::notify(msg_title, msg_body).map_err(Error)?;
    Ok(())
}