notify_rust/
error.rs

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