feat: change init return value to bool, add is_inited and tests

This commit is contained in:
2026-09-29 01:39:42 +02:00
parent 971c833b3e
commit 368d82f8b9
2 changed files with 33 additions and 15 deletions

View File

@@ -3,8 +3,6 @@ name = "libnotify-rs"
version = "0.1.0"
edition = "2024"
[dependencies]
[build-dependencies]
pkg-config = "0.3.34"
bindgen = "0.73.2"

View File

@@ -5,25 +5,26 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
/// 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.
///
/// # Returns
/// True if successful, false if libnotify fails to initialize, error if string contains \0
pub fn init<'a, T>(app_name: T) -> Result<bool, Box<dyn std::error::Error>>
/// TRUE if successful, or FALSE on error
pub fn init<'a, T>(app_name: T) -> bool
where
T: Into<Option<&'a str>>,
{
let app_name = app_name
match app_name
.into()
.map(|app_name| std::ffi::CString::new(app_name))
.transpose()?;
.transpose()
{
Ok(app_name) => unsafe {
notify_init(
app_name
.as_ref()
.map_or(std::ptr::null(), |app_name| app_name.as_ptr()),
) != 0
},
let result = unsafe {
notify_init(
app_name
.as_ref()
.map_or(std::ptr::null(), |app_name| app_name.as_ptr()),
) != 0
};
Ok(result)
Err(_) => return false,
}
}
/// Uninitializes libnotify.
@@ -33,3 +34,22 @@ pub fn uninit() {
notify_uninit();
}
}
/// Gets whether or not libnotify is initialized.
pub fn is_initted() -> bool {
unsafe { notify_is_initted() != 0 }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_initializing() {
assert!(!is_initted());
assert!(init("Morbius"));
assert!(is_initted());
uninit();
assert!(!is_initted());
}
}