use std::collections::HashMap; use std::fmt; use zbus_notification::NotifyUnit; // Structure of a notification struct Notif { app_name: String, replace_id: Option, ico: String, summary: String, body: String, actions: Vec<(String, String)>, hints: HashMap, expir_timeout: Option, } // Function to print the contents of the notification impl fmt::Display for Notif { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "AppName: {}", &self.app_name)?; match self.replace_id { Some(notif_id) => writeln!(f, "ReplaceId: {notif_id}")?, None => writeln!(f, "None")?, } write!( f, "Icon: {}\nSummary: {}\nBody: {}\nActions:\n", &self.ico, &self.summary, &self.body )?; for (key, label) in &self.actions { writeln!(f, "\t{key}: {label}")?; } writeln!(f, "Hints:")?; for (key, value) in &self.hints { writeln!(f, "\t{key}: {value}")?; } match self.expir_timeout { Some(millisec) => writeln!(f, "Expiration Timeout: {millisec}")?, None => writeln!(f, "None")?, } Ok(()) // Return Ok to indicate success } } // Summary should be generally <= 40 chars, as per the specification. fn truncate_summary(notif: &mut Notif) { if notif.summary.len() > 40 { notif.summary.truncate(39); notif.summary.push('…'); } } fn main() { // Example notif let mut not = Notif { app_name: "snot".to_string(), replace_id: Some(0), ico: "alert.png".to_string(), summary: "This is a very long suuummmaaarrryyy! Don't you believe me????".to_string(), body: "short body(hehe)".to_string(), actions: vec![ ("reply".to_string(), "Reply".to_string()), ("close".to_string(), "Close".to_string()), ], hints: HashMap::new(), // Create an empty HashMap for hints, add the hints later expir_timeout: Some(0), }; not.hints .insert("urgency".to_string(), "critical".to_string()); not.hints .insert("category".to_string(), "network.error".to_string()); // End of eg. notif truncate_summary(&mut not); //Limit the summary length println!("{not}"); }