From d2562a7d57979b7f8851dfdbc1aa7e3a74fffd6b Mon Sep 17 00:00:00 2001 From: candifloss Date: Sat, 21 Sep 2024 22:41:57 +0530 Subject: [PATCH] conditional inclusion of actions in json --- src/formats/json.rs | 16 ++++++++++++---- src/formats/rson.rs | 0 src/notification.rs | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 src/formats/rson.rs diff --git a/src/formats/json.rs b/src/formats/json.rs index 930ede0..c662594 100644 --- a/src/formats/json.rs +++ b/src/formats/json.rs @@ -46,17 +46,25 @@ impl Notification { // The notification as a json object pub fn json(&self) -> Value { - json!({ + // Initialize + let mut notif_json: Value = json!({ "app_name": &self.app_name(), "replace_id": &self.replace_id(), "icon": &self.icon(), "summary": &self.summary(), "body": &self.body(), - "actions": &self.actions_json(), "hints": &self.hints_json(), "expiration_timeout": self.expir_timeout(), "urgency": self.urgency(), - "default_action": self.default_action_json(), - }) + }); + + // Conditionally add the Actions fields + if let Value::Object(ref mut map) = notif_json { + if !self.actions().is_empty() { + map.insert("actions".to_string(), self.actions_json()); + map.insert("default_action".to_string(), self.default_action_json()); + } + } + notif_json } } diff --git a/src/formats/rson.rs b/src/formats/rson.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/notification.rs b/src/notification.rs index 6621b1d..0153e7e 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,10 +1,10 @@ -use serde::Serialize; +// use serde::Serialize; use std::collections::HashMap; use zbus::{message::Body, Result}; use zvariant::OwnedValue; // A notificaion object -#[derive(Serialize)] // To help with json +// #[derive(Serialize)] // To help with json pub struct Notification { // The application that sent the notification app_name: String,