docs: add doctests to builder

This commit is contained in:
2026-10-04 15:27:48 +02:00
parent f76226242d
commit 71c1dc7bad

View File

@@ -19,6 +19,29 @@ impl<'a> NotificationBuilder<'a> {
/// # Errors /// # Errors
/// ///
/// This function can return LibnotifyError::{NewNotificationError, NulError} /// This function can return LibnotifyError::{NewNotificationError, NulError}
///
/// # Example
/// ```
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
///
/// // Builder is recommended way to interact with libnotify notifications
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
///
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?;
///
/// // you don't need to uninitialize libnotify, cause context already does that!
///
/// Ok(())
/// }
///
/// ```
pub fn new<T, Y>( pub fn new<T, Y>(
summary: &str, summary: &str,
body: T, body: T,
@@ -41,6 +64,17 @@ impl<'a> NotificationBuilder<'a> {
/// ///
/// # Errors /// # Errors
/// This function can return LibnotifyError::{NulError} /// This function can return LibnotifyError::{NulError}
///
/// # Example
/// ```ignore(Small example)
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .set_app_name("Other app name");
/// ```
pub fn set_app_name<T: Into<Option<&'a str>>>( pub fn set_app_name<T: Into<Option<&'a str>>>(
mut self, mut self,
app_name: T, app_name: T,
@@ -56,6 +90,17 @@ impl<'a> NotificationBuilder<'a> {
/// ///
/// # Errors /// # Errors
/// This function can return LibnotifyError::{NulError} /// This function can return LibnotifyError::{NulError}
///
/// # Example
/// ```ignore(Small example)
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .set_app_icon("STOCK-HOME")?;
/// ```
pub fn set_app_icon<T: Into<Option<&'a str>>>( pub fn set_app_icon<T: Into<Option<&'a str>>>(
mut self, mut self,
app_icon: T, app_icon: T,
@@ -91,6 +136,25 @@ impl<'a> NotificationBuilder<'a> {
} }
/// Set timeout until close /// Set timeout until close
///
/// # Example
/// ```
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
///
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .set_timeout(libnotify_rs::api::notification::Timeout::Never);
///
/// Ok(())
/// }
/// ```
pub fn set_timeout<T: Into<Option<Timeout>>>(mut self, timeout: T) -> Self { pub fn set_timeout<T: Into<Option<Timeout>>>(mut self, timeout: T) -> Self {
if let Some(timeout) = timeout.into() { if let Some(timeout) = timeout.into() {
self.notification.set_timeout(timeout); self.notification.set_timeout(timeout);
@@ -100,6 +164,25 @@ impl<'a> NotificationBuilder<'a> {
} }
/// Set notification urgency level /// Set notification urgency level
///
/// # Example
/// ```
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
///
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .set_urgency(libnotify_rs::api::notification::Urgency::Critical);
///
/// Ok(())
/// }
/// ```
pub fn set_urgency<T: Into<Option<Urgency>>>(mut self, urgency: T) -> Self { pub fn set_urgency<T: Into<Option<Urgency>>>(mut self, urgency: T) -> Self {
if let Some(urgency) = urgency.into() { if let Some(urgency) = urgency.into() {
self.notification.set_urgency(urgency); self.notification.set_urgency(urgency);
@@ -112,6 +195,28 @@ impl<'a> NotificationBuilder<'a> {
/// ///
/// # Errors /// # Errors
/// This function can return LibnotifyError::NulError /// This function can return LibnotifyError::NulError
///
/// # Example
/// ```
///
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
///
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .add_action("default", "super cool action", |action| {
/// println!("Action: {} was run!", action);
/// });
///
/// Ok(())
/// }
/// ```
pub fn add_action<F>( pub fn add_action<F>(
mut self, mut self,
action: &str, action: &str,
@@ -131,6 +236,31 @@ impl<'a> NotificationBuilder<'a> {
/// ///
/// # Errors /// # Errors
/// This function can return LibnotifyError::{GerrorError, AllocationError} /// This function can return LibnotifyError::{GerrorError, AllocationError}
///
/// # Example
/// ```no_run(No notification daemon on CI)
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
///
/// /// Builder is recommended way to interact with libnotify notifications
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
///
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .set_urgency(libnotify_rs::api::notification::Urgency::Critical)
/// .set_timeout(libnotify_rs::api::notification::Timeout::Never)
/// .set_category("Coffee")?;
///
/// notification_builder.show()?;
///
/// Ok(())
/// }
/// ```
pub fn show(&self) -> Result<(), LibnotifyError> { pub fn show(&self) -> Result<(), LibnotifyError> {
self.notification.show()?; self.notification.show()?;
Ok(()) Ok(())
@@ -142,6 +272,31 @@ impl<'a> NotificationBuilder<'a> {
/// ///
/// # Errors /// # Errors
/// This function can return LibnotifyError::{GerrorError, AllocationError} /// This function can return LibnotifyError::{GerrorError, AllocationError}
///
/// # Example
/// ```no_run(No notification daemon on CI)
/// use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
///
/// /// Builder is recommended way to interact with libnotify notifications
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
///
/// let notification_builder = NotificationBuilder::new(
/// "Remember about your coffee!",
/// "Only idiots forget about coffee!",
/// None,
/// &ctx,
/// )?
/// .set_urgency(libnotify_rs::api::notification::Urgency::Critical)
/// .set_timeout(libnotify_rs::api::notification::Timeout::Never)
/// .set_category("Coffee")?;
///
/// notification_builder.show()?;
/// notification_builder.close()?;
/// Ok(())
/// }
/// ```
pub fn close(&self) -> Result<(), LibnotifyError> { pub fn close(&self) -> Result<(), LibnotifyError> {
self.notification.close()?; self.notification.close()?;
Ok(()) Ok(())