dev #1
@ -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
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							@ -1,10 +1,12 @@
 | 
				
			|||||||
pub mod formats {
 | 
					pub mod formats {
 | 
				
			||||||
    pub mod json;
 | 
					    pub mod json;
 | 
				
			||||||
    pub mod plain;
 | 
					    pub mod plain;
 | 
				
			||||||
 | 
					    //pub mod rson;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
mod notification;
 | 
					mod notification;
 | 
				
			||||||
use notification::{to_notif, Notification};
 | 
					use notification::{to_notif, Notification};
 | 
				
			||||||
use std::collections::HashMap;
 | 
					use std::collections::HashMap;
 | 
				
			||||||
 | 
					use std::env;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use futures_util::stream::TryStreamExt;
 | 
					use futures_util::stream::TryStreamExt;
 | 
				
			||||||
use zbus::{message::Body, Connection, Result};
 | 
					use zbus::{message::Body, Connection, Result};
 | 
				
			||||||
@ -29,6 +31,13 @@ fn server_properties() -> HashMap<String, String> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[tokio::main]
 | 
					#[tokio::main]
 | 
				
			||||||
async fn main() -> Result<()> {
 | 
					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?;
 | 
					    let connection = Connection::session().await?;
 | 
				
			||||||
    connection
 | 
					    connection
 | 
				
			||||||
        .request_name(NOTIF_INTERFACE) // Requesting dbus for this service name. Any other services/procs using this name should be stopped/disabled before this
 | 
					        .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
 | 
					    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
 | 
					    // Iterate on the message stream
 | 
				
			||||||
    while let Some(msg) = stream.try_next().await? {
 | 
					    while let Some(msg) = stream.try_next().await? {
 | 
				
			||||||
        // Check the method calls in the received message's header
 | 
					        // Check the method calls in the received message's header
 | 
				
			||||||
@ -81,7 +93,7 @@ async fn main() -> Result<()> {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                "Notify" => {
 | 
					                "Notify" => {
 | 
				
			||||||
                    // 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
 | 
					                    notification_id += 1; // This could be incremented or generated.
 | 
				
			||||||
                    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
 | 
				
			||||||
@ -90,8 +102,11 @@ async fn main() -> Result<()> {
 | 
				
			|||||||
                    // Convert the msg body to a Notification object
 | 
					                    // Convert the msg body to a Notification object
 | 
				
			||||||
                    let notif: Notification = to_notif(&msg_body)?;
 | 
					                    let notif: Notification = to_notif(&msg_body)?;
 | 
				
			||||||
                    // Handle the notif
 | 
					                    // Handle the notif
 | 
				
			||||||
                    println!("New notification!\n{}\n", ¬if.plain()); // Print the plain version
 | 
					                    if op_format == "j" {
 | 
				
			||||||
                    println!("JSON!\n{}\n", ¬if.json()); // Print the plain version
 | 
					                        println!("JSON!\n{}\n", ¬if.json()); // Print the json version
 | 
				
			||||||
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        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
 | 
				
			||||||
 | 
				
			|||||||
@ -65,6 +65,13 @@ impl Notification {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        actions
 | 
					        actions
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    /*
 | 
				
			||||||
 | 
					    pub fn actions(&self) -> Vec<(String, String)> {
 | 
				
			||||||
 | 
					    self.actions
 | 
				
			||||||
 | 
					        .chunks(2)
 | 
				
			||||||
 | 
					        .map(|chunk| (chunk[0].clone(), chunk[1].clone()))
 | 
				
			||||||
 | 
					        .collect()
 | 
				
			||||||
 | 
					    } */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Hints
 | 
					    // Hints
 | 
				
			||||||
    pub fn hints(&self) -> &HashMap<String, OwnedValue> {
 | 
					    pub fn hints(&self) -> &HashMap<String, OwnedValue> {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user