Added options j/p

This commit is contained in:
Candifloss 2024-09-22 11:16:51 +05:30
parent 04db8a6454
commit 3b92de541a
3 changed files with 46 additions and 3 deletions

View File

@ -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::<Map<_, _>>() // Collect into a rson_rs::value::Value::Map
}
}
}

View File

@ -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<String, String> {
#[tokio::main]
async fn main() -> Result<()> {
let args: Vec<String> = 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, &notification_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", &notif.plain()); // Print the plain version
println!("JSON!\n{}\n", &notif.json()); // Print the plain version
if op_format == "j" {
println!("JSON!\n{}\n", &notif.json()); // Print the json version
} else {
println!("New notification!\n{}\n", &notif.plain()); // Print the plain version
}
}
"CloseNotification" => {
// Client sent a close signal. Extract notification ID of the notif to be closed from the message body

View File

@ -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<String, OwnedValue> {