5 Commits

Author SHA1 Message Date
Szymon
21a09f9722 Merge pull request #3 from szymon-jozef/docs/everything
docs/everything
2026-10-02 15:30:25 +02:00
173d04c39c chore: version bump 2026-10-02 15:29:43 +02:00
6be65a249e docs: write super cool documentation 2026-10-02 15:29:17 +02:00
Szymon
2140751d2f Merge pull request #2 from szymon-jozef/refactor/split-modules
refactor: split modules into separate dirs
2026-10-01 21:35:48 +02:00
b4c8bceb4c refactor: split modules into separate dirs 2026-10-01 21:34:47 +02:00
14 changed files with 97 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "libnotify-rs" name = "libnotify-rs"
version = "0.2.0" version = "0.2.1"
edition = "2024" edition = "2024"
authors = [ "szymon_jozef"] authors = [ "szymon_jozef"]
description = "Rust bindings for libnotify" description = "Rust bindings for libnotify"

View File

@@ -11,8 +11,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
None, None,
&ctx, &ctx,
)? )?
.set_urgency(libnotify_rs::notification::Urgency::Critical) .set_urgency(libnotify_rs::api::notification::Urgency::Critical)
.set_timeout(libnotify_rs::notification::Timeout::Never) .set_timeout(libnotify_rs::api::notification::Timeout::Never)
.set_category("Coffee")?; .set_category("Coffee")?;
notification_builder.show()?; notification_builder.show()?;

View File

@@ -1,7 +1,7 @@
use glib::MainLoop; use glib::MainLoop;
use libnotify_rs::{ use libnotify_rs::{
functions::{init, uninit}, api::functions::{init, uninit},
notification::Notification, api::notification::Notification,
}; };
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {

View File

@@ -1,4 +1,4 @@
use libnotify_rs::functions::{self, uninit}; use libnotify_rs::api::functions::{self, uninit};
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
functions::init("Morbius")?; functions::init("Morbius")?;

View File

@@ -1,6 +1,6 @@
use libnotify_rs::{ use libnotify_rs::api::{
functions::{init, uninit}, functions::{init, uninit},
notification::Notification, notification::{self, Notification},
}; };
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -11,8 +11,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
None, None,
)?; )?;
notify.set_timeout(libnotify_rs::notification::Timeout::Custom(1000 * 10)); // 10 secs notify.set_timeout(notification::Timeout::Custom(1000 * 10)); // 10 secs
notify.set_urgency(libnotify_rs::notification::Urgency::Critical); // coffee is very important notify.set_urgency(notification::Urgency::Critical); // coffee is very important
notify.set_category("Coffee")?; notify.set_category("Coffee")?;
notify.show()?; notify.show()?;

View File

@@ -1,6 +1,6 @@
use crate::utils::c_str_to_rs_str_and_free; use super::utils::c_str_to_rs_str_and_free;
use crate::libnotify::*; use super::libnotify::*;
/* Initting /* Initting
* *
@@ -116,6 +116,7 @@ pub fn get_server_caps() -> Option<Vec<String>> {
if vec.is_empty() { None } else { Some(vec) } if vec.is_empty() { None } else { Some(vec) }
} }
/// Contains server information. Refer to [libnotify get_server_info docs](https://gnome.pages.gitlab.gnome.org/libnotify/func.get_server_info.html) to better understand specific fields
pub struct ServerInfo { pub struct ServerInfo {
/// A location to store the server name, or `None` /// A location to store the server name, or `None`
pub name: Option<String>, pub name: Option<String>,

6
src/api/mod.rs Normal file
View File

@@ -0,0 +1,6 @@
/// Same as [libnotify global functions](https://gnome.pages.gitlab.gnome.org/libnotify/index.html#functions)
pub mod functions;
mod libnotify;
/// Same as [libnotify notification](https://gnome.pages.gitlab.gnome.org/libnotify/class.Notification.html)
pub mod notification;
mod utils;

View File

@@ -1,13 +1,18 @@
use crate::libnotify::*; use super::libnotify::*;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
/// Notification urgency level. Refer to [libnotify docs](https://gnome.pages.gitlab.gnome.org/libnotify/enum.Urgency.html) for more info
pub enum Urgency { pub enum Urgency {
/// Low urgency. Used for unimportant notifications
Low, Low,
/// Normal urgency. Used for most standard notifications
Normal, Normal,
/// Critical urgency. Used for very important notifications
Critical, Critical,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
/// Reason why notification was closed. Refer to [libnotify docs](https://gnome.pages.gitlab.gnome.org/libnotify/enum.ClosedReason.html)
pub enum ClosedReason { pub enum ClosedReason {
/// Notification not closed /// Notification not closed
Unset, Unset,
@@ -29,24 +34,39 @@ pub enum ClosedReason {
/// Used in set hint. /// Used in set hint.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum HintValue { pub enum HintValue {
#[allow(missing_docs)]
String(String), String(String),
#[allow(missing_docs)]
Int32(i32), Int32(i32),
#[allow(missing_docs)]
Boolean(bool), Boolean(bool),
} }
/// The timeout in milliseconds /// Notification timeout, until close
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Timeout { pub enum Timeout {
#[allow(missing_docs)]
Default, Default,
#[allow(missing_docs)]
Never, Never,
/// Custom timeout in milliseconds
Custom(i32), Custom(i32),
} }
/// Represents one notification. Wraps most of [libnotify::notifaction](https://gnome.pages.gitlab.gnome.org/libnotify/class.Notification.html) functions.
pub struct Notification { pub struct Notification {
inner: *mut NotifyNotification, inner: *mut NotifyNotification,
} }
impl<'a> Notification { impl<'a> Notification {
/// Create new notification.
///
/// # Errors
/// This function will return an error if the supplied bytes contain an
/// internal 0 byte. The [`NulError`](https://doc.rust-lang.org/stable/alloc/ffi/c_str/struct.NulError.html) returned will contain the bytes as well as
/// 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, Box<dyn std::error::Error>>
where where
T: Into<Option<&'a str>>, T: Into<Option<&'a str>>,
@@ -73,6 +93,15 @@ impl<'a> Notification {
Ok(Self { inner }) Ok(Self { inner })
} }
/// Add callback to notification.
///
/// # Args
/// `action` – action identifier. `default` will work for most use cases. Other text will be
/// displayed as button names.
///
/// `label` – label for user
///
/// `callback` - any closure that accepts &str as an argument, where &str is action name.
pub fn add_action<F>( pub fn add_action<F>(
&mut self, &mut self,
action: &str, action: &str,

View File

@@ -7,7 +7,7 @@ pub unsafe fn c_str_to_rs_str_and_free(c_str: *mut std::os::raw::c_char) -> Opti
.to_string_lossy() .to_string_lossy()
.to_string(); .to_string();
unsafe { crate::libnotify::g_free(c_str as *mut std::ffi::c_void) }; unsafe { super::libnotify::g_free(c_str as *mut std::ffi::c_void) };
Some(normal_string) Some(normal_string)
} }

View File

@@ -1,11 +1,20 @@
#![cfg_attr(not(doctest), doc = include_str!("./../README.md"))] //! This crate exposes two modules:
//!
//! # Api
//! This module contains a basic wrapper for libnotify calls. It just translates data types, etc,
//! so usage is basically the same as libnotify.
//!
//! # Wrappers
//! This module is a higher level abstraction over api. Recommended to use over api.
//!
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![warn(missing_docs)]
pub mod builder; /// Basic libnotify rust wrapper
pub mod context; pub mod api;
pub mod functions; /// Higher level abstraction over api
mod libnotify; pub mod wrappers;
pub mod notification;
mod utils; pub use wrappers::{builder, context};

View File

@@ -1,14 +1,17 @@
use crate::{ use crate::{
context::LibnotifyContext, api::notification::{HintValue, Notification, Timeout, Urgency},
notification::{HintValue, Notification, Timeout, Urgency}, wrappers::context::LibnotifyContext,
}; };
/// Higher level abstraction of `Notification`. Allow for builder pattern notification construction.
/// Requires `LibnotifyContext`
pub struct NotificationBuilder<'a> { pub struct NotificationBuilder<'a> {
notification: Notification, notification: Notification,
_context: &'a LibnotifyContext, _context: &'a LibnotifyContext,
} }
impl<'a> NotificationBuilder<'a> { impl<'a> NotificationBuilder<'a> {
/// Create new NotificationBuilder, with given context.
pub fn new<T, Y>( pub fn new<T, Y>(
summary: &str, summary: &str,
body: T, body: T,
@@ -27,20 +30,25 @@ 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, std::ffi::NulError> {
self.notification.set_app_name(app_name)?; self.notification.set_app_name(app_name)?;
Ok(self) 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, std::ffi::NulError> {
self.notification.set_app_icon(app_icon)?; self.notification.set_app_icon(app_icon)?;
Ok(self) 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, std::ffi::NulError> {
self.notification.set_category(category)?; self.notification.set_category(category)?;
Ok(self) Ok(self)
} }
/// Set hint for this specific notification
pub fn set_hint( pub fn set_hint(
mut self, mut self,
key: &str, key: &str,
@@ -50,16 +58,19 @@ impl<'a> NotificationBuilder<'a> {
Ok(self) Ok(self)
} }
/// Set timeout until close
pub fn set_timeout(mut self, timeout: Timeout) -> Self { pub fn set_timeout(mut self, timeout: Timeout) -> Self {
self.notification.set_timeout(timeout); self.notification.set_timeout(timeout);
self self
} }
/// Set notification urgency level
pub fn set_urgency(mut self, urgency: Urgency) -> Self { pub fn set_urgency(mut self, urgency: Urgency) -> Self {
self.notification.set_urgency(urgency); self.notification.set_urgency(urgency);
self self
} }
/// Add callback
pub fn add_action<F>( pub fn add_action<F>(
mut self, mut self,
action: &str, action: &str,
@@ -73,11 +84,15 @@ impl<'a> NotificationBuilder<'a> {
Ok(self) Ok(self)
} }
/// Show notification
///
/// Uses `&self` borrow, so you can use this object later, after showing.
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> { pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
self.notification.show()?; self.notification.show()?;
Ok(()) Ok(())
} }
/// Close notification synchronously.
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> { pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
self.notification.close()?; self.notification.close()?;
Ok(()) Ok(())

View File

@@ -1,11 +1,17 @@
use crate::functions::{init, is_initted, set_app_icon, set_app_name, uninit}; use crate::api::functions::{init, is_initted, set_app_icon, set_app_name, uninit};
/// Notification context. Handles libnotify initializing and uninitializing.
pub struct LibnotifyContext {} pub struct LibnotifyContext {}
static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool = static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false); std::sync::atomic::AtomicBool::new(false);
impl LibnotifyContext { impl LibnotifyContext {
/// Try initializing libnotify
///
/// # Error
/// Returns an error, if libnotify cannot be initialized or if there already exists a context
/// object
pub fn new(app_name: &str) -> Result<LibnotifyContext, Box<dyn std::error::Error>> { pub fn new(app_name: &str) -> Result<LibnotifyContext, Box<dyn std::error::Error>> {
if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) { if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) {
return Err("You can create only one context at a time".into()); return Err("You can create only one context at a time".into());
@@ -18,11 +24,13 @@ impl LibnotifyContext {
Ok(Self {}) Ok(Self {})
} }
/// Sets global app name
pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> { pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> {
set_app_name(app_name)?; set_app_name(app_name)?;
Ok(()) Ok(())
} }
/// Sets global app icon
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> { pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> {
set_app_icon(app_icon)?; set_app_icon(app_icon)?;
Ok(()) Ok(())

4
src/wrappers/mod.rs Normal file
View File

@@ -0,0 +1,4 @@
/// Contains notification builder
pub mod builder;
/// Containx libnotify context
pub mod context;