feat: add libnotify error

This commit is contained in:
2026-10-03 12:43:34 +02:00
parent 21a09f9722
commit a231712057
5 changed files with 56 additions and 13 deletions

View File

@@ -1,3 +1,5 @@
use crate::api::errors::LibnotifyError;
use super::utils::c_str_to_rs_str_and_free;
use super::libnotify::*;
@@ -10,7 +12,7 @@ use super::libnotify::*;
/// Initialized libnotify. This must be called before any other functions.
///
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
pub fn init<'a, T>(app_name: T) -> Result<(), Box<dyn std::error::Error>>
pub fn init<'a, T>(app_name: T) -> Result<(), LibnotifyError>
where
T: Into<Option<&'a str>>,
{
@@ -24,7 +26,7 @@ where
)
} == 0
{
return Err("Libnotify init failed".into());
return Err(LibnotifyError::InitFail);
}
Ok(())
@@ -161,7 +163,7 @@ pub fn get_server_info() -> Option<ServerInfo> {
*/
/// Sets the application name
pub fn set_app_name(app_name: &str) -> Result<(), std::ffi::NulError> {
pub fn set_app_name(app_name: &str) -> Result<(), LibnotifyError> {
let app_name_c = std::ffi::CString::new(app_name)?;
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
@@ -172,7 +174,7 @@ pub fn set_app_name(app_name: &str) -> Result<(), std::ffi::NulError> {
/// Available since: 0.8.4
///
/// app_icon is icon name or path to an image
pub fn set_app_icon(app_icon: &str) -> Result<(), std::ffi::NulError> {
pub fn set_app_icon(app_icon: &str) -> Result<(), LibnotifyError> {
let app_icon_c = std::ffi::CString::new(app_icon)?;
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
@@ -195,11 +197,10 @@ mod tests {
#[test]
fn test_bad_init() {
assert!(
init("Mor\0ius")
.unwrap_err()
.is::<std::ffi::c_str::NulError>()
);
assert!(matches!(
init("Mor\0ius").unwrap_err(),
LibnotifyError::NulError(_)
));
}
#[test]