1#![allow(missing_docs)]
2
3#[cfg(all(feature = "images_no_default_features", unix, not(target_os = "macos")))]
4use crate::image::ImageError;
5use std::{fmt, num};
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[cfg(all(target_os = "macos", not(feature = "preview-macos-un")))]
10pub use crate::macos::{ApplicationError, MacOsError, NotificationError};
11
12#[cfg(all(target_os = "macos", feature = "preview-macos-un"))]
13pub use crate::macos::MacOsError;
14
15#[derive(Debug)]
17pub struct Error {
18 kind: ErrorKind,
19}
20
21#[derive(Debug)]
23#[non_exhaustive]
24pub enum ErrorKind {
25 Msg(String),
27
28 #[cfg(all(feature = "dbus", unix, not(target_os = "macos")))]
29 Dbus(dbus::Error),
30
31 #[cfg(all(feature = "zbus", unix, not(target_os = "macos")))]
32 Zbus(zbus::Error),
33
34 #[cfg(target_os = "macos")]
35 MacNotificationSys(mac_notification_sys::error::Error),
36
37 #[cfg(all(target_os = "macos", feature = "preview-macos-un"))]
38 MacUserNotifications(mac_usernotifications::Error),
39
40 Parse(num::ParseIntError),
41
42 SpecVersion(String),
43
44 Conversion(String),
45
46 #[cfg(all(feature = "images_no_default_features", unix, not(target_os = "macos")))]
47 Image(ImageError),
48
49 ImplementationMissing,
50}
51
52impl fmt::Display for Error {
53 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54 match self.kind {
55 #[cfg(all(feature = "dbus", unix, not(target_os = "macos")))]
56 ErrorKind::Dbus(ref e) => write!(f, "{}", e),
57
58 #[cfg(all(feature = "zbus", unix, not(target_os = "macos")))]
59 ErrorKind::Zbus(ref e) => write!(f, "{}", e),
60
61 #[cfg(target_os = "macos")]
62 ErrorKind::MacNotificationSys(ref e) => write!(f, "{e}"),
63
64 #[cfg(all(target_os = "macos", feature = "preview-macos-un"))]
65 ErrorKind::MacUserNotifications(ref e) => write!(f, "{e}"),
66
67 ErrorKind::Parse(ref e) => write!(f, "Parsing Error: {e}"),
68 ErrorKind::Conversion(ref e) => write!(f, "Conversion Error: {e}"),
69 ErrorKind::SpecVersion(ref e) | ErrorKind::Msg(ref e) => write!(f, "{e}"),
70 #[cfg(all(feature = "images_no_default_features", unix, not(target_os = "macos")))]
71 ErrorKind::Image(ref e) => write!(f, "{}", e),
72 ErrorKind::ImplementationMissing => write!(
73 f,
74 r#"No Dbus implementation available, please compile with either feature ="z" or feature="d""#
75 ),
76 }
77 }
78}
79
80impl std::error::Error for Error {}
81
82impl From<&str> for Error {
83 fn from(e: &str) -> Error {
84 Error {
85 kind: ErrorKind::Msg(e.into()),
86 }
87 }
88}
89
90#[cfg(all(feature = "dbus", unix, not(target_os = "macos")))]
91impl From<dbus::Error> for Error {
92 fn from(e: dbus::Error) -> Error {
93 Error {
94 kind: ErrorKind::Dbus(e),
95 }
96 }
97}
98
99#[cfg(all(feature = "zbus", unix, not(target_os = "macos")))]
100impl From<zbus::Error> for Error {
101 fn from(e: zbus::Error) -> Error {
102 Error {
103 kind: ErrorKind::Zbus(e),
104 }
105 }
106}
107
108#[cfg(target_os = "macos")]
109impl From<mac_notification_sys::error::Error> for Error {
110 fn from(e: mac_notification_sys::error::Error) -> Error {
111 Error {
112 kind: ErrorKind::MacNotificationSys(e),
113 }
114 }
115}
116
117#[cfg(all(feature = "images_no_default_features", unix, not(target_os = "macos")))]
118impl From<ImageError> for Error {
119 fn from(e: ImageError) -> Error {
120 Error {
121 kind: ErrorKind::Image(e),
122 }
123 }
124}
125
126#[cfg(all(target_os = "macos", feature = "preview-macos-un"))]
127impl From<mac_usernotifications::Error> for Error {
128 fn from(e: mac_usernotifications::Error) -> Error {
129 Error {
130 kind: ErrorKind::MacUserNotifications(e),
131 }
132 }
133}
134
135impl From<num::ParseIntError> for Error {
136 fn from(e: num::ParseIntError) -> Error {
137 Error {
138 kind: ErrorKind::Parse(e),
139 }
140 }
141}
142
143impl From<ErrorKind> for Error {
144 fn from(kind: ErrorKind) -> Error {
145 Error { kind }
146 }
147}
148
149#[macro_export]
151#[doc(hidden)]
152macro_rules! bail {
153 ($e:expr) => {
154 return Err($e.into());
155 };
156 ($fmt:expr, $($arg:tt)+) => {
157 return Err(format!($fmt, $($arg)+).into());
158 };
159}
160
161#[macro_export(local_inner_macros)]
167#[doc(hidden)]
168macro_rules! ensure {
169 ($cond:expr, $e:expr) => {
170 if !($cond) {
171 bail!($e);
172 }
173 };
174 ($cond:expr, $fmt:expr, $($arg:tt)*) => {
175 if !($cond) {
176 bail!($fmt, $($arg)*);
177 }
178 };
179}