mac_notification_sys/
error.rs1use std::error;
4use std::fmt;
5
6pub type NotificationResult<T> = Result<T, Error>;
8
9mod application {
10    use super::*;
11    #[derive(Debug)]
13    pub enum ApplicationError {
14        AlreadySet(String),
16
17        CouldNotSet(String),
19    }
20
21    impl fmt::Display for ApplicationError {
22        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23            match self {
24                ApplicationError::AlreadySet(e) => {
25                    write!(f, "Application '{}' can only be set once.", e)
26                }
27                ApplicationError::CouldNotSet(e) => write!(
28                    f,
29                    "Could not set application '{}', using default \"com.apple.Terminal\"",
30                    e
31                ),
32            }
33        }
34    }
35
36    impl error::Error for ApplicationError {}
37}
38
39mod notification {
40    use super::*;
41
42    #[derive(Debug)]
44    pub enum NotificationError {
45        ScheduleInThePast,
47
48        UnableToSchedule,
50
51        UnableToDeliver,
53    }
54    impl fmt::Display for NotificationError {
55        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56            match self {
57                NotificationError::ScheduleInThePast => {
58                    write!(f, "Can not schedule notification in the past")
59                }
60                NotificationError::UnableToSchedule => write!(f, "Could not schedule notification"),
61                NotificationError::UnableToDeliver => write!(f, "Could not deliver notification"),
62            }
63        }
64    }
65
66    impl error::Error for NotificationError {}
67}
68
69pub use self::application::ApplicationError;
70pub use self::notification::NotificationError;
71
72#[derive(Debug)]
74pub enum Error {
75    Application(ApplicationError),
77    Notification(NotificationError),
79}
80
81impl fmt::Display for Error {
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        match self {
84            Error::Application(e) => write!(f, "{}", e),
85            Error::Notification(e) => write!(f, "{}", e),
86        }
87    }
88}
89
90impl error::Error for Error {}
91
92impl From<ApplicationError> for Error {
93    fn from(e: ApplicationError) -> Error {
94        Error::Application(e)
95    }
96}
97
98impl From<NotificationError> for Error {
99    fn from(e: NotificationError) -> Error {
100        Error::Notification(e)
101    }
102}
103
104#[macro_export]
106#[doc(hidden)]
107macro_rules! bail {
108    ($e:expr) => {
109        return Err($e.into());
110    };
111    ($fmt:expr, $($arg:tt)+) => {
112        return Err(format!($fmt, $($arg)+).into());
113    };
114}
115
116#[macro_export(local_inner_macros)]
122#[doc(hidden)]
123macro_rules! ensure {
124    ($cond:expr, $e:expr) => {
125        if ($cond) != true {
126            bail!($e);
127        }
128    };
129    ($cond:expr, $fmt:expr, $($arg:tt)*) => {
130        if !($cond) {
131            bail!($fmt, $($arg)*);
132        }
133    };
134}