Print notification. And some basic structure changes.

This commit is contained in:
Candifloss 2024-08-12 00:42:21 +05:30
parent d497b9af67
commit b7cccbd631

View File

@ -1,24 +1,44 @@
use std::fmt;
use std::collections::HashMap; use std::collections::HashMap;
enum ActionType { // Structure of a notification
Normal,
Default,
}
struct NotifAction {
key: String,
label: String,
}
struct Notif { struct Notif {
app_name: String, app_name: String,
replace_id: u32, replace_id: Option<u32>,
ico: String, //It's string, right? ico: String,
summary: String, summary: String,
body: String, body: String,
actions: Vec<(String, String)>, actions: Vec<(String, String)>,
hints: HashMap<String, String>, hints: HashMap<String, String>,
expir_timeout: i32, 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
}
} }
// Summary should be generally <= 40 chars, as per the specification. // Summary should be generally <= 40 chars, as per the specification.
@ -30,9 +50,10 @@ fn truncate_summary(notif: &mut Notif) {
} }
fn main() { fn main() {
// Example notif
let mut not = Notif { let mut not = Notif {
app_name: "snot".to_string(), app_name: "snot".to_string(),
replace_id: 0, replace_id: Some(0),
ico: "alert.png".to_string(), ico: "alert.png".to_string(),
summary: "This is a very long suuummmaaarrryyy! Don't you believe me????".to_string(), summary: "This is a very long suuummmaaarrryyy! Don't you believe me????".to_string(),
body: "short body(hehe)".to_string(), body: "short body(hehe)".to_string(),
@ -40,27 +61,13 @@ fn main() {
("reply".to_string(), "Reply".to_string()), ("reply".to_string(), "Reply".to_string()),
("close".to_string(), "Close".to_string()), ("close".to_string(), "Close".to_string()),
], ],
hints: HashMap::new(), // Create an empty HashMap for hints hints: HashMap::new(), // Create an empty HashMap for hints, add the hints later
expir_timeout: 0 expir_timeout: Some(0)
}; };
not.hints.insert("urgency".to_string(), "critical".to_string()); not.hints.insert("urgency".to_string(), "critical".to_string());
not.hints.insert("category".to_string(), "network.error".to_string());
truncate_summary(&mut not); //Limit the summary length truncate_summary(&mut not); //Limit the summary length
println!("AppName: {}\nReplaceId: {}\nIcon: {}\nSummary: {}\nBody: {}\nExpirationTimeout: {}", println!("{}", not);
not.app_name, }
not.replace_id,
not.ico,
not.summary,
not.body,
not.expir_timeout
);
println!("Actions:");
for (key, label) in &not.actions {
println!("\t{}: {}, ", key, label);
}
println!("Hints:");
for (key, value) in not.hints.iter() {
println!("\t{}: {}, ", key, value);
}
}