2024-08-11 19:12:21 +00:00
|
|
|
use std::fmt;
|
2024-08-11 14:27:37 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-08-11 19:12:21 +00:00
|
|
|
// Structure of a notification
|
2024-08-11 11:31:05 +00:00
|
|
|
struct Notif {
|
|
|
|
app_name: String,
|
2024-08-11 19:12:21 +00:00
|
|
|
replace_id: Option<u32>,
|
|
|
|
ico: String,
|
2024-08-11 11:31:05 +00:00
|
|
|
summary: String,
|
|
|
|
body: String,
|
2024-08-11 14:27:37 +00:00
|
|
|
actions: Vec<(String, String)>,
|
|
|
|
hints: HashMap<String, String>,
|
2024-08-11 19:12:21 +00:00
|
|
|
expir_timeout: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to print the contents of the notification
|
|
|
|
impl fmt::Display for Notif {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f,"AppName: {}\n", &self.app_name)?;
|
|
|
|
match self.replace_id {
|
|
|
|
Some(notif_id) => write!(f, "ReplaceId: {}\n", notif_id)?,
|
|
|
|
None => write!(f, "None\n")?,
|
|
|
|
}
|
|
|
|
write!(f,"Icon: {}\nSummary: {}\nBody: {}\nActions:\n",
|
|
|
|
&self.ico,
|
|
|
|
&self.summary,
|
|
|
|
&self.body
|
|
|
|
)?;
|
|
|
|
for (key, label) in &self.actions {
|
|
|
|
write!(f, "\t{}: {}\n", key, label)?;
|
|
|
|
}
|
|
|
|
write!(f,"Hints:\n")?;
|
|
|
|
for (key, value) in &self.hints {
|
|
|
|
write!(f, "\t{}: {}\n", key, value)?;
|
|
|
|
}
|
|
|
|
match self.expir_timeout {
|
|
|
|
Some(millisec) => write!(f, "Expiration Timeout: {}\n", millisec)?,
|
|
|
|
None => write!(f, "None\n")?,
|
|
|
|
}
|
|
|
|
Ok(()) // Return Ok to indicate success
|
|
|
|
}
|
2024-08-10 21:29:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 11:31:05 +00:00
|
|
|
// 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_str("…");
|
2024-08-10 21:29:13 +00:00
|
|
|
}
|
2024-08-11 11:31:05 +00:00
|
|
|
}
|
2024-08-10 21:29:13 +00:00
|
|
|
|
2024-08-11 11:31:05 +00:00
|
|
|
fn main() {
|
2024-08-11 19:12:21 +00:00
|
|
|
// Example notif
|
2024-08-11 11:31:05 +00:00
|
|
|
let mut not = Notif {
|
|
|
|
app_name: "snot".to_string(),
|
2024-08-11 19:12:21 +00:00
|
|
|
replace_id: Some(0),
|
2024-08-11 14:27:37 +00:00
|
|
|
ico: "alert.png".to_string(),
|
2024-08-11 11:31:05 +00:00
|
|
|
summary: "This is a very long suuummmaaarrryyy! Don't you believe me????".to_string(),
|
|
|
|
body: "short body(hehe)".to_string(),
|
2024-08-11 14:27:37 +00:00
|
|
|
actions: vec![
|
|
|
|
("reply".to_string(), "Reply".to_string()),
|
|
|
|
("close".to_string(), "Close".to_string()),
|
|
|
|
],
|
2024-08-11 19:12:21 +00:00
|
|
|
hints: HashMap::new(), // Create an empty HashMap for hints, add the hints later
|
|
|
|
expir_timeout: Some(0)
|
2024-08-11 11:31:05 +00:00
|
|
|
};
|
2024-08-11 14:27:37 +00:00
|
|
|
not.hints.insert("urgency".to_string(), "critical".to_string());
|
2024-08-11 19:12:21 +00:00
|
|
|
not.hints.insert("category".to_string(), "network.error".to_string());
|
|
|
|
|
2024-08-11 11:32:35 +00:00
|
|
|
truncate_summary(&mut not); //Limit the summary length
|
2024-08-11 11:31:05 +00:00
|
|
|
|
2024-08-11 19:12:21 +00:00
|
|
|
println!("{}", not);
|
|
|
|
}
|