mac_notification_sys/
error.rs

1//! Custom errors for mac-notification-sys.
2
3use std::error;
4use std::fmt;
5
6/// Custom Result type for mac-notification-sys.
7pub type NotificationResult<T> = Result<T, Error>;
8
9mod application {
10    use super::*;
11    /// Errors that can occur setting the Bundle Identifier.
12    #[derive(Debug)]
13    pub enum ApplicationError {
14        /// The application name is already set.
15        AlreadySet(String),
16
17        /// The application name could not be set.
18        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    /// Errors that can occur while interacting with the NSUserNotificationCenter.
43    #[derive(Debug)]
44    pub enum NotificationError {
45        /// Notifications can not be scheduled in the past.
46        ScheduleInThePast,
47
48        /// Scheduling a notification caused an error.
49        UnableToSchedule,
50
51        /// Delivering a notification caused an error.
52        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/// Our local error Type
73#[derive(Debug)]
74pub enum Error {
75    /// Application related Error
76    Application(ApplicationError),
77    /// Notification related Error
78    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/// Just the usual bail macro
105#[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/// Exits a function early with an `Error` if the condition is not satisfied.
117///
118/// Similar to `assert!`, `ensure!` takes a condition and exits the function
119/// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`,
120/// it does not panic.
121#[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}