Beginning of json module

This commit is contained in:
Candifloss 2024-09-20 12:12:41 +05:30
parent 7506134553
commit 59efd6e20e
5 changed files with 55 additions and 13 deletions

View File

@ -9,5 +9,6 @@ zbus = "4.4.0"
zvariant = "4.2.0"
tokio = { version = "1.40.0", features = ["full"] }
futures-util = "0.3.30"
serde = "1.0.210"
serde_json = "1.0.128"
# rson_rs = "0.2.1"

View File

@ -1,17 +1,54 @@
// This module deals with converting the notification object into Json format, which might be useful for data exchange
use crate::notification::Notification;
use serde_json::{json, Value};
use serde_json::{json, Value}; // json!() macro and Value type
impl Notification {
// Json format, useful for data exchange
pub fn json(&self) {
// Actions
let actions: Value = if self.actions().is_empty() {
"None".into()
// Actions, as json
pub fn actions_json(&self) -> Value {
if self.actions().is_empty() {
json!({}) // Empty JSON object if no actions
} else {
self.actions().into()
};
json!({"actions": actions})
serde_json::Value::Object(
// Wrap the map into a serde_json::Value
self.actions()
.iter()
.map(|(id, label)| {
(id.clone(), json!(label)) // Create key-value pairs: id -> label
})
.collect::<serde_json::Map<_, _>>(), // Collect into a serde_json::Map
)
}
}
// Hints, as json
pub fn hints_json(&self) -> Value {
if self.hints().is_empty() {
json!({}) // Empty JSON object if no hints
} else {
serde_json::Value::Object(
self.hints()
.iter()
.map(|(key, value)| {
(key.clone(), json!(value.to_string())) // Convert hint value to string
})
.collect::<serde_json::Map<_, _>>(), // Collect into a serde_json::Map
)
}
}
// The notification as a json object
pub fn json(&self) -> 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().map(|actn| actn.0),
})
}
}

View File

@ -1,3 +1,4 @@
// This module helps get the notification object in a plain text format, which could be printed to the console or a file
use crate::notification::Notification;
impl Notification {

View File

@ -1,6 +1,6 @@
pub mod formats {
pub mod plain;
pub mod json;
pub mod plain;
}
mod notification;
use notification::{to_notif, Notification};
@ -91,6 +91,7 @@ async fn main() -> Result<()> {
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
}
"CloseNotification" => {
// Client sent a close signal. Extract notification ID of the notif to be closed from the message body

View File

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