Skip to main content

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 '{e}' can only be set once.")
26                }
27                ApplicationError::CouldNotSet(e) => write!(
28                    f,
29                    "Could not set application '{e}', using default \"com.apple.Terminal\"",
30                ),
31            }
32        }
33    }
34
35    impl error::Error for ApplicationError {}
36}
37
38mod notification {
39    use super::*;
40
41    /// Errors that can occur while interacting with the NSUserNotificationCenter.
42    #[derive(Debug)]
43    pub enum NotificationError {
44        /// Notifications can not be scheduled in the past.
45        ScheduleInThePast,
46
47        /// Scheduling a notification caused an error.
48        UnableToSchedule,
49
50        /// Delivering a notification caused an error.
51        UnableToDeliver,
52    }
53    impl fmt::Display for NotificationError {
54        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55            match self {
56                NotificationError::ScheduleInThePast => {
57                    write!(f, "Can not schedule notification in the past")
58                }
59                NotificationError::UnableToSchedule => write!(f, "Could not schedule notification"),
60                NotificationError::UnableToDeliver => write!(f, "Could not deliver notification"),
61            }
62        }
63    }
64
65    impl error::Error for NotificationError {}
66}
67
68pub use self::application::ApplicationError;
69pub use self::notification::NotificationError;
70
71/// Our local error Type
72#[derive(Debug)]
73pub enum Error {
74    /// Application related Error
75    Application(ApplicationError),
76    /// Notification related Error
77    Notification(NotificationError),
78}
79
80impl fmt::Display for Error {
81    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82        match self {
83            Error::Application(e) => write!(f, "{e}"),
84            Error::Notification(e) => write!(f, "{e}"),
85        }
86    }
87}
88
89impl error::Error for Error {}
90
91impl From<ApplicationError> for Error {
92    fn from(e: ApplicationError) -> Error {
93        Error::Application(e)
94    }
95}
96
97impl From<NotificationError> for Error {
98    fn from(e: NotificationError) -> Error {
99        Error::Notification(e)
100    }
101}
102
103/// Just the usual bail macro
104#[macro_export]
105#[doc(hidden)]
106macro_rules! bail {
107    ($e:expr_2021) => {
108        return Err($e.into());
109    };
110    ($fmt:expr_2021, $($arg:tt)+) => {
111        return Err(format!($fmt, $($arg)+).into());
112    };
113}
114
115/// Exits a function early with an `Error` if the condition is not satisfied.
116///
117/// Similar to `assert!`, `ensure!` takes a condition and exits the function
118/// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`,
119/// it does not panic.
120#[macro_export(local_inner_macros)]
121#[doc(hidden)]
122macro_rules! ensure {
123    ($cond:expr_2021, $e:expr_2021) => {
124        if ($cond) != true {
125            bail!($e);
126        }
127    };
128    ($cond:expr_2021, $fmt:expr_2021, $($arg:tt)*) => {
129        if !($cond) {
130            bail!($fmt, $($arg)*);
131        }
132    };
133}