mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
feat: change init return value to bool, add is_inited and tests
This commit is contained in:
@@ -3,8 +3,6 @@ name = "libnotify-rs"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
pkg-config = "0.3.34"
|
pkg-config = "0.3.34"
|
||||||
bindgen = "0.73.2"
|
bindgen = "0.73.2"
|
||||||
|
|||||||
@@ -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.
|
/// 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
|
/// # Returns
|
||||||
/// True if successful, false if libnotify fails to initialize, error if string contains \0
|
/// TRUE if successful, or FALSE on error
|
||||||
pub fn init<'a, T>(app_name: T) -> Result<bool, Box<dyn std::error::Error>>
|
pub fn init<'a, T>(app_name: T) -> bool
|
||||||
where
|
where
|
||||||
T: Into<Option<&'a str>>,
|
T: Into<Option<&'a str>>,
|
||||||
{
|
{
|
||||||
let app_name = app_name
|
match app_name
|
||||||
.into()
|
.into()
|
||||||
.map(|app_name| std::ffi::CString::new(app_name))
|
.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 {
|
Err(_) => return false,
|
||||||
notify_init(
|
}
|
||||||
app_name
|
|
||||||
.as_ref()
|
|
||||||
.map_or(std::ptr::null(), |app_name| app_name.as_ptr()),
|
|
||||||
) != 0
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uninitializes libnotify.
|
/// Uninitializes libnotify.
|
||||||
@@ -33,3 +34,22 @@ pub fn uninit() {
|
|||||||
notify_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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user