barkust 0.2.1

A Rust client library and CLI for the Bark push notification service
Documentation
use crate::{Level, Notification};

/// A builder for constructing [`Notification`] values.
#[derive(Debug, Default, Clone)]
pub struct NotificationBuilder {
    device_key: Option<String>,
    device_keys: Option<Vec<String>>,
    title: Option<String>,
    subtitle: Option<String>,
    body: Option<String>,
    markdown: Option<String>,
    image: Option<String>,
    sound: Option<String>,
    badge: Option<i32>,
    icon: Option<String>,
    group: Option<String>,
    url: Option<String>,
    copy: Option<String>,
    action: Option<String>,
    is_archive: Option<i32>,
    is_url_encode: Option<i32>,
    auto_copy: Option<i32>,
    level: Option<Level>,
    volume: Option<i32>,
    call: Option<i32>,
    id: Option<String>,
    delete: Option<i32>,
    ttl: Option<i32>,
}

impl NotificationBuilder {
    /// Create a new, empty builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the target device key.
    pub fn device_key(mut self, key: impl Into<String>) -> Self {
        self.device_key = Some(key.into());
        self
    }

    /// Set multiple target device keys for batch push.
    pub fn device_keys(mut self, keys: Vec<String>) -> Self {
        self.device_keys = Some(keys);
        self
    }

    /// Set the notification title.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set the notification body.
    pub fn body(mut self, body: impl Into<String>) -> Self {
        self.body = Some(body.into());
        self
    }

    /// Set the notification subtitle.
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    /// Set the markdown-formatted content. Overrides `body` on the client.
    pub fn markdown(mut self, markdown: impl Into<String>) -> Self {
        self.markdown = Some(markdown.into());
        self
    }

    /// Set the image URL to display in the notification.
    pub fn image(mut self, image: impl Into<String>) -> Self {
        self.image = Some(image.into());
        self
    }

    /// Set the alert sound name.
    pub fn sound(mut self, sound: impl Into<String>) -> Self {
        self.sound = Some(sound.into());
        self
    }

    /// Set the badge number.
    pub fn badge(mut self, badge: i32) -> Self {
        self.badge = Some(badge);
        self
    }

    /// Set the icon URL.
    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set the notification group name.
    pub fn group(mut self, group: impl Into<String>) -> Self {
        self.group = Some(group.into());
        self
    }

    /// Set the URL to open when the notification is tapped.
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.url = Some(url.into());
        self
    }

    /// Set the text copied to the clipboard when the notification is tapped.
    pub fn copy(mut self, copy: impl Into<String>) -> Self {
        self.copy = Some(copy.into());
        self
    }

    /// Set a custom action (iOS notification action identifier).
    pub fn action(mut self, action: impl Into<String>) -> Self {
        self.action = Some(action.into());
        self
    }

    /// Whether to archive the notification in Bark history.
    pub fn archive(mut self, archive: bool) -> Self {
        self.is_archive = Some(if archive { 1 } else { 0 });
        self
    }

    /// Whether the title and body should be URL-encoded by the server.
    pub fn url_encode(mut self, encode: bool) -> Self {
        self.is_url_encode = Some(if encode { 1 } else { 0 });
        self
    }

    /// Whether to automatically copy `copy` text when the notification arrives.
    pub fn auto_copy(mut self, auto_copy: bool) -> Self {
        self.auto_copy = Some(if auto_copy { 1 } else { 0 });
        self
    }

    /// Set the interruption level of the notification.
    pub fn level(mut self, level: Level) -> Self {
        self.level = Some(level);
        self
    }

    /// Set the volume for the alert sound (0-10).
    pub fn volume(mut self, volume: i32) -> Self {
        self.volume = Some(volume);
        self
    }

    /// Whether to call the user after delivering the notification.
    pub fn call(mut self, call: bool) -> Self {
        self.call = Some(if call { 1 } else { 0 });
        self
    }

    /// Set a custom identifier to update or delete an existing notification.
    pub fn id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Pass `true` together with `id` to delete the notification.
    pub fn delete(mut self, delete: bool) -> Self {
        self.delete = Some(if delete { 1 } else { 0 });
        self
    }

    /// Set archive retention time in seconds.
    pub fn ttl(mut self, ttl: i32) -> Self {
        self.ttl = Some(ttl);
        self
    }

    /// Build the [`Notification`].
    pub fn build(self) -> Notification {
        Notification {
            device_key: self.device_key,
            device_keys: self.device_keys,
            title: self.title,
            subtitle: self.subtitle,
            body: self.body,
            markdown: self.markdown,
            image: self.image,
            sound: self.sound,
            badge: self.badge,
            icon: self.icon,
            group: self.group,
            url: self.url,
            copy: self.copy,
            action: self.action,
            is_archive: self.is_archive,
            is_url_encode: self.is_url_encode,
            auto_copy: self.auto_copy,
            level: self.level,
            volume: self.volume,
            call: self.call,
            id: self.id,
            delete: self.delete,
            ttl: self.ttl,
        }
    }
}