From 3b92de541a5b6ec963439be0cf657493b0efecba Mon Sep 17 00:00:00 2001 From: candifloss Date: Sun, 22 Sep 2024 11:16:51 +0530 Subject: [PATCH] Added options j/p --- src/formats/rson.rs | 21 +++++++++++++++++++++ src/main.rs | 21 ++++++++++++++++++--- src/notification.rs | 7 +++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/formats/rson.rs b/src/formats/rson.rs index e69de29..cebe5d2 100644 --- a/src/formats/rson.rs +++ b/src/formats/rson.rs @@ -0,0 +1,21 @@ +// This module deals with converting the notification object into rson format, which can be used instead of json if preferred +use crate::notification::Notification; +use rson_rs::ser::to_string; +use rson_rs::value::Value; +use rson_rs::value::Value::Map; + +impl Notification { + pub fn actions_rson(&self) -> Value { + if self.actions().is_empty() { + Value::from_str("{actions: Null}") + } else { + // Wrap the map into a rson_rs::value::Value + self.actions() + .iter() + .map(|(id, label)| { + (id.clone(), Value::from_str(label)) // Create key-value pairs: id -> label + }) + .collect::>() // Collect into a rson_rs::value::Value::Map + } + } +} diff --git a/src/main.rs b/src/main.rs index f53fbcd..b25b0df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ pub mod formats { pub mod json; pub mod plain; + //pub mod rson; } mod notification; use notification::{to_notif, Notification}; use std::collections::HashMap; +use std::env; use futures_util::stream::TryStreamExt; use zbus::{message::Body, Connection, Result}; @@ -29,6 +31,13 @@ fn server_properties() -> HashMap { #[tokio::main] async fn main() -> Result<()> { + let args: Vec = env::args().collect(); + let op_format: String = if args.is_empty() || args[1] == "j" { + "j".to_string() // Default value, json format + } else { + "p".to_string() // Plain format + }; + let connection = Connection::session().await?; connection .request_name(NOTIF_INTERFACE) // Requesting dbus for this service name. Any other services/procs using this name should be stopped/disabled before this @@ -36,6 +45,9 @@ async fn main() -> Result<()> { let mut stream = zbus::MessageStream::from(&connection); // Convert connection to a MessageStream, yields Message items + // Notification id, restarts with each session + let mut notification_id: u32 = 0; + // Iterate on the message stream while let Some(msg) = stream.try_next().await? { // Check the method calls in the received message's header @@ -81,7 +93,7 @@ async fn main() -> Result<()> { } "Notify" => { // New notification received. Now, respond to the client with a notification ID - let notification_id: u32 = 1; // This could be incremented or generated. DO IT LATER + notification_id += 1; // This could be incremented or generated. connection.reply(&msg, ¬ification_id).await?; // The client waits for this response in order to disconnect // Get the body of the message @@ -90,8 +102,11 @@ async fn main() -> Result<()> { // Convert the msg body to a Notification object let notif: Notification = to_notif(&msg_body)?; // Handle the notif - println!("New notification!\n{}\n", ¬if.plain()); // Print the plain version - println!("JSON!\n{}\n", ¬if.json()); // Print the plain version + if op_format == "j" { + println!("JSON!\n{}\n", ¬if.json()); // Print the json version + } else { + println!("New notification!\n{}\n", ¬if.plain()); // Print the plain version + } } "CloseNotification" => { // Client sent a close signal. Extract notification ID of the notif to be closed from the message body diff --git a/src/notification.rs b/src/notification.rs index 0153e7e..299e42d 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -65,6 +65,13 @@ impl Notification { } actions } + /* + pub fn actions(&self) -> Vec<(String, String)> { + self.actions + .chunks(2) + .map(|chunk| (chunk[0].clone(), chunk[1].clone())) + .collect() + } */ // Hints pub fn hints(&self) -> &HashMap {