Skip to main content

libnotify_rs/api/
errors.rs

1/// Enum mapping every possible error that can happen in this lib, with a nice enum.
2#[derive(Debug)]
3pub enum LibnotifyError {
4    /// [Libnotify init](https://gnome.pages.gitlab.gnome.org/libnotify/func.init.html) failed
5    InitError,
6    /// `&str` contains a null terminator. Read more at [std documentation](https://doc.rust-lang.org/std/ffi/struct.NulError.html)
7    NulError(std::ffi::NulError),
8    /// [Libnotify notification constructor](https://gnome.pages.gitlab.gnome.org/libnotify/ctor.Notification.new.html) failed allocating
9    NewNotificationError,
10    /// Custom libnotify error with explanation.
11    GerrorError(String),
12    /// Data failed allocating. Could indicate that system doesn't have enough memory
13    AllocationError,
14}
15
16impl std::fmt::Display for LibnotifyError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            LibnotifyError::InitError => {
20                write!(f, "Libnotify failed to init")
21            }
22
23            LibnotifyError::NulError(_) => {
24                write!(f, "Argument contains a null terminator")
25            }
26
27            LibnotifyError::NewNotificationError => {
28                write!(f, "Notification constructor failed")
29            }
30
31            LibnotifyError::GerrorError(e) => {
32                write!(f, "Libnotify returned an error: {}", e)
33            }
34
35            LibnotifyError::AllocationError => {
36                write!(f, "Libnotify failed allocating memory")
37            }
38        }
39    }
40}
41
42impl std::error::Error for LibnotifyError {
43    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44        match self {
45            LibnotifyError::InitError => None,
46            LibnotifyError::NulError(nul_error) => Some(nul_error),
47            LibnotifyError::NewNotificationError => None,
48            LibnotifyError::GerrorError(_) => None,
49            LibnotifyError::AllocationError => None,
50        }
51    }
52}
53
54impl From<std::ffi::NulError> for LibnotifyError {
55    fn from(value: std::ffi::NulError) -> Self {
56        Self::NulError(value)
57    }
58}