Skip to main content

NotificationBuilder

Struct NotificationBuilder 

Source
pub struct NotificationBuilder<'a> { /* private fields */ }
Expand description

Higher level abstraction of Notification. Allow for builder pattern notification construction. Requires LibnotifyContext

Implementations§

Source§

impl<'a> NotificationBuilder<'a>

Source

pub fn new<T, Y>( summary: &str, body: T, icon: Y, context: &'a LibnotifyContext, ) -> Result<Self, Box<dyn Error>>
where T: Into<Option<&'a str>>, Y: Into<Option<&'a str>>,

Create new NotificationBuilder, with given context.

§Errors

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(())
}
Source

pub fn set_app_name<T: Into<Option<&'a str>>>( self, app_name: T, ) -> Result<Self, LibnotifyError>

Sets app name for this specific notification.

§Errors

This function can return LibnotifyError::{NulError}

§Example
ⓘ
let notification_builder = NotificationBuilder::new(
    "Remember about your coffee!",
    "Only idiots forget about coffee!",
    None,
    &ctx,
)?
.set_app_name("Other app name");
Source

pub fn set_app_icon<T: Into<Option<&'a str>>>( self, app_icon: T, ) -> Result<Self, LibnotifyError>

Sets app icon fot this specific notification.

§Errors

This function can return LibnotifyError::{NulError}

§Example
ⓘ
let notification_builder = NotificationBuilder::new(
    "Remember about your coffee!",
    "Only idiots forget about coffee!",
    None,
    &ctx,
)?
.set_app_icon("STOCK-HOME")?;
Source

pub fn set_category<T: Into<Option<&'a str>>>( self, category: T, ) -> Result<Self, LibnotifyError>

Sets category for this specific notification

§Errors

This function can return LibnotifyError::{NulError}

Source

pub fn set_hint( self, key: &str, value: HintValue, ) -> Result<Self, LibnotifyError>

Set hint for this specific notification

§Errors

This function can return LibnotifyError::{NulError, AllocationError}

Source

pub fn set_timeout<T: Into<Option<Timeout>>>(self, timeout: T) -> Self

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(())
}
Source

pub fn set_urgency<T: Into<Option<Urgency>>>(self, urgency: T) -> Self

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(())
}
Source

pub fn add_action<F>( self, action: &str, label: &str, callback: F, ) -> Result<Self, LibnotifyError>
where F: Fn(&str) + 'static,

Add callback

§Errors

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(())
}
Source

pub fn show(&self) -> Result<(), LibnotifyError>

Show notification

Uses &self borrow, so you can use this object later, after showing.

§Errors

This function can return LibnotifyError::{GerrorError, AllocationError}

§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,
    )?
    .set_urgency(libnotify_rs::api::notification::Urgency::Critical)
    .set_timeout(libnotify_rs::api::notification::Timeout::Never)
    .set_category("Coffee")?;

    notification_builder.show()?;

    Ok(())
}
Source

pub fn close(&self) -> Result<(), LibnotifyError>

Close notification synchronously.

Uses &self borrow, so you can use this object later, after closing.

§Errors

This function can return LibnotifyError::{GerrorError, AllocationError}

§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,
    )?
    .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(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.