Notification Struct

This commit is contained in:
Candifloss 2024-09-16 16:49:08 +05:30
parent ce4e7a8a43
commit b9513b903c

View File

@ -3,32 +3,44 @@ use std::collections::HashMap;
use zbus::{message::Body, Connection, Result};
use zvariant::OwnedValue;
fn print_notif(msg_body: Body) -> Result<()> {
// Deserialize the message body to the expected tuple
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout): (
String,
u32,
String,
String,
String,
Vec<String>,
HashMap<String, OwnedValue>,
i32,
) = msg_body.deserialize()?;
// Print the notification details
println!("App Name: {app_name}");
println!("Replace ID: {replace_id}");
println!("Icon: {icon}");
println!("Summary: {summary}");
println!("Body: {body}");
println!("Actions: {actions:?}");
struct Notification {
app_name: String,
replace_id: u32,
icon: String,
summary: String,
body: String,
actions: Vec<String>,
hints: HashMap<String, OwnedValue>,
expir_timeout: i32,
}
fn to_notif(msg_body: Body) -> Result<Notification> {
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
msg_body.deserialize()?;
Ok(Notification {
app_name,
replace_id,
icon,
summary,
body,
actions,
hints,
expir_timeout,
})
}
fn print_notif(notif: Notification) {
println!("App Name: {0}", notif.app_name);
println!("Replace ID: {0}", notif.replace_id);
println!("Icon: {0}", notif.icon);
println!("Summary: {0}", notif.summary);
println!("Body: {0}", notif.body);
println!("Actions: {0:?}", notif.actions);
println!("Hints:");
for (key, value) in hints {
for (key, value) in notif.hints {
println!(" {key}: {value:?}");
}
println!("Expiration Timeout: {expir_timeout} seconds\n");
Ok(())
println!("Expiration Timeout: {0} seconds\n", notif.expir_timeout);
}
#[tokio::main]
@ -51,14 +63,16 @@ async fn main() -> Result<()> {
println!("Request received: {member}\n\tName: SNot\n\tVendor: candifloss.cc\n\tVersion: 0.1.0\n\tSpec_version: 0.1.0");
}
"Notify" => {
println!("Member is: {member}");
// Handle notif
// Handle new received notif
println!("New notification:");
let msg_body = msg.body();
// get the app_name, summary, body, etc. from the msg_body and print it
let _ = print_notif(msg_body);
// Done. Respond with a notification ID
let notification_id: u32 = 1; // This could be incremented or generated
connection.reply(&msg, &notification_id).await?;
// get the app_name, summary, body, etc. from the msg_body
let notif = to_notif(msg_body);
// Print the notif
let _ = print_notif(notif.expect("Failed to parse notification"));
// Done. Respond to the client with a notification ID
let notification_id: u32 = 1; // This could be incremented or generated. Do it l8r
connection.reply(&msg, &notification_id).await?; // The client will stop when it gets the response
}
_ => {
println!("Unhandled method: {member}");