begin json module
This commit is contained in:
parent
128ffb0db5
commit
f507d0b687
@ -9,4 +9,5 @@ zbus = "4.4.0"
|
|||||||
zvariant = "4.2.0"
|
zvariant = "4.2.0"
|
||||||
tokio = { version = "1.40.0", features = ["full"] }
|
tokio = { version = "1.40.0", features = ["full"] }
|
||||||
futures-util = "0.3.30"
|
futures-util = "0.3.30"
|
||||||
|
serde_json = "1.0.128"
|
||||||
# rson_rs = "0.2.1"
|
# rson_rs = "0.2.1"
|
13
src/formats/json.rs
Normal file
13
src/formats/json.rs
Normal 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!(
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -10,7 +10,7 @@ impl Notification {
|
|||||||
self.actions()
|
self.actions()
|
||||||
.iter()
|
.iter()
|
||||||
.fold(String::new(), |mut acc, (id, label)| {
|
.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.push_str(&format!("\n\t{id}: {label}"));
|
||||||
acc
|
acc
|
||||||
})
|
})
|
||||||
@ -21,7 +21,7 @@ impl Notification {
|
|||||||
Some(actn) => actn.0,
|
Some(actn) => actn.0,
|
||||||
};
|
};
|
||||||
// Hints
|
// Hints
|
||||||
let hints = if self.hints().is_empty() {
|
let hints: String = if self.hints().is_empty() {
|
||||||
"None".to_string()
|
"None".to_string()
|
||||||
} else {
|
} else {
|
||||||
self.hints()
|
self.hints()
|
||||||
@ -33,7 +33,7 @@ impl Notification {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Return the notification as a plain string
|
// 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.app_name(),
|
||||||
&self.replace_id(),
|
&self.replace_id(),
|
||||||
&self.icon(),
|
&self.icon(),
|
20
src/main.rs
20
src/main.rs
@ -1,16 +1,19 @@
|
|||||||
mod formats;
|
pub mod formats {
|
||||||
|
pub mod plain;
|
||||||
|
pub mod json;
|
||||||
|
}
|
||||||
mod notification;
|
mod notification;
|
||||||
use notification::to_notif;
|
use notification::{to_notif, Notification};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use futures_util::stream::TryStreamExt;
|
use futures_util::stream::TryStreamExt;
|
||||||
use zbus::{Connection, Result};
|
use zbus::{message::Body, Connection, Result};
|
||||||
|
|
||||||
const SERVER_NAME: &str = "SNot"; // Server software name
|
const SERVER_NAME: &str = "SNot"; // Server software name
|
||||||
const VENDOR: &str = "candifloss.cc"; // Server software vendor
|
const VENDOR: &str = "candifloss.cc"; // Server software vendor
|
||||||
const VERSION: &str = "0.1.0"; // Server software version
|
const VERSION: &str = "0.1.0"; // Server software version
|
||||||
const SPEC_VERSION: &str = "0.42"; // DBus specification 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
|
// Properties of this server
|
||||||
fn server_properties() -> HashMap<String, String> {
|
fn server_properties() -> HashMap<String, String> {
|
||||||
@ -42,6 +45,7 @@ async fn main() -> Result<()> {
|
|||||||
"GetAll" => {
|
"GetAll" => {
|
||||||
// Client requested all properties of the server. Extract the interface name from the message body
|
// 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
|
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
|
// Check if the requested interface is the right one
|
||||||
if interface_name == NOTIF_INTERFACE {
|
if interface_name == NOTIF_INTERFACE {
|
||||||
// Properties for the 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
|
// 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
|
let notification_id: u32 = 1; // This could be incremented or generated. DO IT LATER
|
||||||
connection.reply(&msg, ¬ification_id).await?; // The client waits for this response in order to disconnect
|
connection.reply(&msg, ¬ification_id).await?; // The client waits for this response in order to disconnect
|
||||||
|
|
||||||
// Get the body of the message
|
// 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
|
// Convert the msg body to a Notification object
|
||||||
let notif = to_notif(&msg_body)?;
|
let notif: Notification = to_notif(&msg_body)?;
|
||||||
// Handle the notif
|
// Handle the notif
|
||||||
println!("New notification!\n{}", ¬if.plain()); // Print the plain version
|
println!("New notification!\n{}\n", ¬if.plain()); // Print the plain version
|
||||||
}
|
}
|
||||||
"CloseNotification" => {
|
"CloseNotification" => {
|
||||||
// Client sent a close signal. Extract notification ID of the notif to be closed from the message body
|
// Client sent a close signal. Extract notification ID of the notif to be closed from the message body
|
||||||
|
@ -16,7 +16,7 @@ pub struct Notification {
|
|||||||
body: String,
|
body: String,
|
||||||
// Action requests that can be sent back to the client - "Reply," "Mark as Read," "Play/Pause/Next," "Snooze/Dismiss," etc.
|
// Action requests that can be sent back to the client - "Reply," "Mark as Read," "Play/Pause/Next," "Snooze/Dismiss," etc.
|
||||||
actions: Vec<String>,
|
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>,
|
hints: HashMap<String, OwnedValue>,
|
||||||
// Seconds till this notif expires. Optional
|
// Seconds till this notif expires. Optional
|
||||||
expir_timeout: i32,
|
expir_timeout: i32,
|
||||||
@ -105,7 +105,7 @@ pub fn to_notif(msg_body: &Body) -> Result<Notification> {
|
|||||||
// Deserialize body into a tuple
|
// Deserialize body into a tuple
|
||||||
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
|
let (app_name, replace_id, icon, summary, body, actions, hints, expir_timeout) =
|
||||||
msg_body.deserialize()?;
|
msg_body.deserialize()?;
|
||||||
// Form a Notification object from the obtained tuple
|
// Form a Notification object from the obtained tuple's fields
|
||||||
Ok(Notification {
|
Ok(Notification {
|
||||||
app_name,
|
app_name,
|
||||||
replace_id,
|
replace_id,
|
||||||
|
Loading…
Reference in New Issue
Block a user