conditional inclusion of actions in json

This commit is contained in:
Candifloss 2024-09-21 22:41:57 +05:30
parent 62eb2bff11
commit d2562a7d57
3 changed files with 14 additions and 6 deletions

View File

@ -46,17 +46,25 @@ impl Notification {
// The notification as a json object // The notification as a json object
pub fn json(&self) -> Value { pub fn json(&self) -> Value {
json!({ // Initialize
let mut notif_json: Value = json!({
"app_name": &self.app_name(), "app_name": &self.app_name(),
"replace_id": &self.replace_id(), "replace_id": &self.replace_id(),
"icon": &self.icon(), "icon": &self.icon(),
"summary": &self.summary(), "summary": &self.summary(),
"body": &self.body(), "body": &self.body(),
"actions": &self.actions_json(),
"hints": &self.hints_json(), "hints": &self.hints_json(),
"expiration_timeout": self.expir_timeout(), "expiration_timeout": self.expir_timeout(),
"urgency": self.urgency(), "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
} }
} }

0
src/formats/rson.rs Normal file
View File

View File

@ -1,10 +1,10 @@
use serde::Serialize; // use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use zbus::{message::Body, Result}; use zbus::{message::Body, Result};
use zvariant::OwnedValue; use zvariant::OwnedValue;
// A notificaion object // A notificaion object
#[derive(Serialize)] // To help with json // #[derive(Serialize)] // To help with json
pub struct Notification { pub struct Notification {
// The application that sent the notification // The application that sent the notification
app_name: String, app_name: String,