mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
feat: add notifications constructor
This commit is contained in:
85
src/lib.rs
85
src/lib.rs
@@ -1,5 +1,58 @@
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
|
||||
pub enum Urgency {
|
||||
Low,
|
||||
Normal,
|
||||
Critical,
|
||||
}
|
||||
|
||||
pub enum ClosedReason {
|
||||
Unset,
|
||||
Expired,
|
||||
ApiReqest,
|
||||
Undefined,
|
||||
}
|
||||
|
||||
pub struct Notification {
|
||||
inner: *mut NotifyNotification,
|
||||
}
|
||||
|
||||
impl<'a> Notification {
|
||||
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, Box<dyn std::error::Error>>
|
||||
where
|
||||
T: Into<Option<&'a str>>,
|
||||
Y: Into<Option<&'a str>>,
|
||||
{
|
||||
let summary = std::ffi::CString::new(summary)?;
|
||||
|
||||
let body = body
|
||||
.into()
|
||||
.map(|body| std::ffi::CString::new(body))
|
||||
.transpose()?;
|
||||
|
||||
let icon = icon
|
||||
.into()
|
||||
.map(|icon| std::ffi::CString::new(icon))
|
||||
.transpose()?;
|
||||
|
||||
let inner = unsafe {
|
||||
notify_notification_new(
|
||||
summary.as_ptr(),
|
||||
body.as_ref().map_or(std::ptr::null(), |body| body.as_ptr()),
|
||||
icon.as_ref().map_or(std::ptr::null(), |icon| icon.as_ptr()),
|
||||
)
|
||||
};
|
||||
|
||||
if inner.is_null() {
|
||||
return Err("notify_notification_new returned null".into());
|
||||
}
|
||||
|
||||
Ok(Self { inner })
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -7,8 +60,30 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
fn new_test() {
|
||||
assert!(
|
||||
!Notification::new("Test", "Test", "Test")
|
||||
.unwrap()
|
||||
.inner
|
||||
.is_null()
|
||||
);
|
||||
assert!(
|
||||
!Notification::new("Test", None, None)
|
||||
.unwrap()
|
||||
.inner
|
||||
.is_null()
|
||||
);
|
||||
assert!(
|
||||
!Notification::new("Test", None, "Test")
|
||||
.unwrap()
|
||||
.inner
|
||||
.is_null()
|
||||
);
|
||||
assert!(
|
||||
!Notification::new("Test", "Test", None)
|
||||
.unwrap()
|
||||
.inner
|
||||
.is_null()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user