begin json module

This commit is contained in:
Candifloss 2024-09-19 16:15:52 +05:30
parent 128ffb0db5
commit f507d0b687
5 changed files with 36 additions and 16 deletions

View File

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

13
src/formats/json.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::notification::Notification;
use serde_json::json;
impl Notification {
// Json format, useful for data exchange
pub fn json(&self) {
let actions = json!(
{}
);
}
}

View File

@ -10,7 +10,7 @@ impl Notification {
self.actions()
.iter()
.fold(String::new(), |mut acc, (id, label)| {
// the formatted string is appended to the accumulator string, acc
// The formatted string is appended to the accumulator string, acc
acc.push_str(&format!("\n\t{id}: {label}"));
acc
})
@ -21,7 +21,7 @@ impl Notification {
Some(actn) => actn.0,
};
// Hints
let hints = if self.hints().is_empty() {
let hints: String = if self.hints().is_empty() {
"None".to_string()
} else {
self.hints()
@ -33,7 +33,7 @@ impl Notification {
};
// Return the notification as a plain string
let plain_string = format!("App Name: {}\nReplace ID: {}\nIcon: {}\nSummary: {}\nBody: {}\nActions: {}\nHints: {}\nExpiration Timeout: {}\nUrgency: {}\nDefault Action: {}",
let plain_string: String = format!("App Name: {}\nReplace ID: {}\nIcon: {}\nSummary: {}\nBody: {}\nActions: {}\nHints: {}\nExpiration Timeout: {}\nUrgency: {}\nDefault Action: {}",
&self.app_name(),
&self.replace_id(),
&self.icon(),

View File

@ -1,16 +1,19 @@
mod formats;
pub mod formats {
pub mod plain;
pub mod json;
}
mod notification;
use notification::to_notif;
use notification::{to_notif, Notification};
use std::collections::HashMap;
use futures_util::stream::TryStreamExt;
use zbus::{Connection, Result};
use zbus::{message::Body, Connection, Result};
const SERVER_NAME: &str = "SNot"; // Server software name
const VENDOR: &str = "candifloss.cc"; // Server software vendor
const VERSION: &str = "0.1.0"; // Server software version
const SPEC_VERSION: &str = "0.42"; // DBus specification version
const NOTIF_INTERFACE: &str = "org.freedesktop.Notifications"; // DBus interface
const NOTIF_INTERFACE: &str = "org.freedesktop.Notifications"; // DBus interface name
// Properties of this server
fn server_properties() -> HashMap<String, String> {
@ -42,6 +45,7 @@ async fn main() -> Result<()> {
"GetAll" => {
// Client requested all properties of the server. Extract the interface name from the message body
let interface_name: String = msg.body().deserialize()?; // This method has only one parameter, the interface name
// Check if the requested interface is the right one
if interface_name == NOTIF_INTERFACE {
// Properties for the interface
@ -79,12 +83,14 @@ async fn main() -> Result<()> {
// 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
connection.reply(&msg, &notification_id).await?; // The client waits for this response in order to disconnect
// Get the body of the message
let msg_body = msg.body(); // The body has 8 parameters, ie., 8 parts of a notification
let msg_body: Body = msg.body(); // The body has 8 parameters, ie., 8 fields of a Notification object
// Convert the msg body to a Notification object
let notif = to_notif(&msg_body)?;
let notif: Notification = to_notif(&msg_body)?;
// Handle the notif
println!("New notification!\n{}", &notif.plain()); // Print the plain version
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

@ -16,7 +16,7 @@ pub struct Notification {
body: String,
// Action requests that can be sent back to the client - "Reply," "Mark as Read," "Play/Pause/Next," "Snooze/Dismiss," etc.
actions: Vec<String>,
// Extra useful data - notif type, urgency, sound file, icon data, etc.
// Useful extra data - notif type, urgency, notif sound, icon data, etc.
hints: HashMap<String, OwnedValue>,
// Seconds till this notif expires. Optional
expir_timeout: i32,
@ -105,7 +105,7 @@ pub fn to_notif(msg_body: &Body) -> Result<Notification> {
// Deserialize body into a tuple
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
msg_body.deserialize()?;
// Form a Notification object from the obtained tuple
// Form a Notification object from the obtained tuple's fields
Ok(Notification {
app_name,
replace_id,