feat: implement all error values

This commit is contained in:
2026-10-03 12:58:08 +02:00
parent a231712057
commit 37787a5594
4 changed files with 49 additions and 27 deletions

View File

@@ -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,
}
}
}

View File

@@ -26,7 +26,7 @@ where
)
} == 0
{
return Err(LibnotifyError::InitFail);
return Err(LibnotifyError::InitError);
}
Ok(())

View File

@@ -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<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, Box<dyn std::error::Error>>
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, LibnotifyError>
where
T: Into<Option<&'a str>>,
Y: Into<Option<&'a str>>,
@@ -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<dyn std::error::Error>>
) -> 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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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) };

View File

@@ -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<Self, std::ffi::NulError> {
pub fn set_app_name(mut self, app_name: &str) -> Result<Self, LibnotifyError> {
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<Self, std::ffi::NulError> {
pub fn set_app_icon(mut self, app_icon: &str) -> Result<Self, LibnotifyError> {
self.notification.set_app_icon(app_icon)?;
Ok(self)
}
/// Sets category for this specific notification
pub fn set_category(mut self, category: &str) -> Result<Self, std::ffi::NulError> {
pub fn set_category(mut self, category: &str) -> Result<Self, LibnotifyError> {
self.notification.set_category(category)?;
Ok(self)
}