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 '{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 #[derive(Debug)]
43 pub enum NotificationError {
44 ScheduleInThePast,
46
47 UnableToSchedule,
49
50 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#[derive(Debug)]
73pub enum Error {
74 Application(ApplicationError),
76 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#[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#[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}