From 37787a559440b6e8fad65d916bd1f8bdf9b7d124 Mon Sep 17 00:00:00 2001 From: szymon Date: Sat, 3 Oct 2026 12:58:08 +0200 Subject: [PATCH] feat: implement all error values --- src/api/errors.rs | 33 +++++++++++++++++++++++++++------ src/api/functions.rs | 2 +- src/api/notification.rs | 30 ++++++++++++++---------------- src/wrappers/builder.rs | 11 +++++++---- 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/api/errors.rs b/src/api/errors.rs index 98aabc1..416748b 100644 --- a/src/api/errors.rs +++ b/src/api/errors.rs @@ -1,22 +1,40 @@ /// Enum mapping every possible error that can happen in this lib, with a nice enum. #[derive(Debug)] pub enum LibnotifyError { - /// [libnotify init](https://gnome.pages.gitlab.gnome.org/libnotify/func.init.html) failed - InitFail, + /// [Libnotify init](https://gnome.pages.gitlab.gnome.org/libnotify/func.init.html) failed + InitError, /// `&str` contains a null terminator. Read more at [std documentation](https://doc.rust-lang.org/std/ffi/struct.NulError.html) NulError(std::ffi::NulError), + /// [Libnotify notification constructor](https://gnome.pages.gitlab.gnome.org/libnotify/ctor.Notification.new.html) failed allocating + NewNotificationError, + /// Custom libnotify error with explanation. + GerrorError(String), + /// Data failed allocating. Could indicate that system doesn't have enough memory + AllocationError, } impl std::fmt::Display for LibnotifyError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::InitFail => { + LibnotifyError::InitError => { write!(f, "Libnotify failed to init") } - Self::NulError(_) => { + LibnotifyError::NulError(_) => { write!(f, "Argument contains a null terminator") } + + LibnotifyError::NewNotificationError => { + write!(f, "Notification constructor failed") + } + + LibnotifyError::GerrorError(e) => { + write!(f, "Libnotify returned an error: {}", e) + } + + LibnotifyError::AllocationError => { + write!(f, "Libnotify failed allocating memory") + } } } } @@ -24,8 +42,11 @@ impl std::fmt::Display for LibnotifyError { impl std::error::Error for LibnotifyError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { - Self::InitFail => None, - Self::NulError(e) => Some(e), + LibnotifyError::InitError => None, + LibnotifyError::NulError(nul_error) => Some(nul_error), + LibnotifyError::NewNotificationError => None, + LibnotifyError::GerrorError(_) => None, + LibnotifyError::AllocationError => None, } } } diff --git a/src/api/functions.rs b/src/api/functions.rs index fcf7ffc..c314b74 100644 --- a/src/api/functions.rs +++ b/src/api/functions.rs @@ -26,7 +26,7 @@ where ) } == 0 { - return Err(LibnotifyError::InitFail); + return Err(LibnotifyError::InitError); } Ok(()) diff --git a/src/api/notification.rs b/src/api/notification.rs index ace6194..e5859a1 100644 --- a/src/api/notification.rs +++ b/src/api/notification.rs @@ -1,3 +1,5 @@ +use crate::api::errors::LibnotifyError; + use super::libnotify::*; #[derive(Debug, Clone, Copy)] @@ -67,7 +69,7 @@ impl<'a> Notification { /// the position of the nul byte. /// /// On libnotify fail it will return Err(&str.into). May change in the future. - pub fn new(summary: &str, body: T, icon: Y) -> Result> + pub fn new(summary: &str, body: T, icon: Y) -> Result where T: Into>, Y: Into>, @@ -87,7 +89,7 @@ impl<'a> Notification { }; if inner.is_null() { - return Err("notify_notification_new returned null".into()); + return Err(LibnotifyError::NewNotificationError); } Ok(Self { inner }) @@ -107,7 +109,7 @@ impl<'a> Notification { action: &str, label: &str, callback: F, - ) -> Result<(), Box> + ) -> Result<(), LibnotifyError> where F: Fn(&str) + 'static, { @@ -144,19 +146,19 @@ impl<'a> Notification { } /// Synchronously tells the notification server to hide the notification on the screen - pub fn close(&self) -> Result<(), Box> { + pub fn close(&self) -> Result<(), LibnotifyError> { let mut gerror = std::ptr::null_mut() as *mut GError; if unsafe { notify_notification_close(self.inner, &mut gerror) } == 0 { if gerror.is_null() { - return Err("Unknown error".into()); + return Err(LibnotifyError::AllocationError); } let e = unsafe { *gerror }.message; if e.is_null() { unsafe { g_error_free(gerror) }; - return Err("Unknown error".into()); + return Err(LibnotifyError::AllocationError); } let e_str = unsafe { std::ffi::CStr::from_ptr(e) } @@ -165,7 +167,7 @@ impl<'a> Notification { unsafe { g_error_free(gerror) }; - return Err(e_str.into()); + return Err(LibnotifyError::GerrorError(e_str)); } Ok(()) @@ -191,7 +193,7 @@ impl<'a> Notification { /// `set_app_icon()`. /// /// Available since: 0.8.4 - pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), std::ffi::NulError> { + pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), LibnotifyError> { let app_icon_c = std::ffi::CString::new(app_icon)?; unsafe { notify_notification_set_app_icon(self.inner, app_icon_c.as_ptr()) }; @@ -203,7 +205,7 @@ impl<'a> Notification { /// If this function is not called, the application name will be set from the value used in init() or overridden with set_app_name(). /// /// Available since: 0.7.3 - pub fn set_app_name(&mut self, app_name: &str) -> Result<(), std::ffi::NulError> { + pub fn set_app_name(&mut self, app_name: &str) -> Result<(), LibnotifyError> { let app_name_c = std::ffi::CString::new(app_name)?; unsafe { notify_notification_set_app_name(self.inner, app_name_c.as_ptr()) }; @@ -213,7 +215,7 @@ impl<'a> Notification { /// Sets the category of this notification. /// /// This can be used by the notification server to filter or display the data in a certain way - pub fn set_category(&mut self, category: &str) -> Result<(), std::ffi::NulError> { + pub fn set_category(&mut self, category: &str) -> Result<(), LibnotifyError> { let category_c = std::ffi::CString::new(category)?; unsafe { notify_notification_set_category(self.inner, category_c.as_ptr()) }; @@ -223,11 +225,7 @@ impl<'a> Notification { /// Sets a hint for key with value value /// /// Available since: 0.6 - pub fn set_hint( - &mut self, - key: &str, - value: HintValue, - ) -> Result<(), Box> { + pub fn set_hint(&mut self, key: &str, value: HintValue) -> Result<(), LibnotifyError> { let key_c = std::ffi::CString::new(key)?; let value_c = match value { @@ -242,7 +240,7 @@ impl<'a> Notification { }; if value_c.is_null() { - return Err("Couldn't create hint value".into()); + return Err(LibnotifyError::AllocationError); } unsafe { notify_notification_set_hint(self.inner, key_c.as_ptr(), value_c) }; diff --git a/src/wrappers/builder.rs b/src/wrappers/builder.rs index b691328..afd4a14 100644 --- a/src/wrappers/builder.rs +++ b/src/wrappers/builder.rs @@ -1,5 +1,8 @@ use crate::{ - api::notification::{HintValue, Notification, Timeout, Urgency}, + api::{ + errors::LibnotifyError, + notification::{HintValue, Notification, Timeout, Urgency}, + }, wrappers::context::LibnotifyContext, }; @@ -31,19 +34,19 @@ impl<'a> NotificationBuilder<'a> { } /// Sets app name for this specific notification. - pub fn set_app_name(mut self, app_name: &str) -> Result { + pub fn set_app_name(mut self, app_name: &str) -> Result { self.notification.set_app_name(app_name)?; Ok(self) } /// Sets app icon fot this specific notification. - pub fn set_app_icon(mut self, app_icon: &str) -> Result { + pub fn set_app_icon(mut self, app_icon: &str) -> Result { self.notification.set_app_icon(app_icon)?; Ok(self) } /// Sets category for this specific notification - pub fn set_category(mut self, category: &str) -> Result { + pub fn set_category(mut self, category: &str) -> Result { self.notification.set_category(category)?; Ok(self) }