better ways to convert actions & hints to string, & handle GetAll

This commit is contained in:
Candifloss 2024-09-19 04:04:25 +05:30
parent 7caf294f38
commit df42d63461
2 changed files with 34 additions and 24 deletions

View File

@ -4,39 +4,44 @@ impl Notification {
// Plain string format, useful for printing // Plain string format, useful for printing
pub fn plain(&self) -> String { pub fn plain(&self) -> String {
// Actions // Actions
let mut actions: String = String::new(); let actions: String = if self.actions().is_empty() {
if self.actions().is_empty() { "None".to_string()
actions = "None".to_string();
} else { } else {
for actn in &self.actions() { self.actions()
actions = actions + "\n\t" + actn.0.as_str() + ": " + actn.1.as_str(); .iter()
} .fold(String::new(), |mut acc, (id, label)| {
} // the formatted string is appended to the accumulator string, acc
acc.push_str(&format!("\n\t{id}: {label}"));
acc
})
};
// Default action // Default action
let default_action: String = match self.default_action() { let default_action: String = match self.default_action() {
None => "None".to_string(), None => "None".to_string(),
Some(actn) => actn.0, Some(actn) => actn.0,
}; };
// Hints // Hints
let mut hints: String = String::new(); let hints = if self.hints().is_empty() {
if self.hints().is_empty() { "None".to_string()
hints = "None".to_string();
} else { } else {
for (key, value) in self.hints() { self.hints()
hints = hints + "\n\t" + key + ": " + &value.to_string(); .iter()
} .fold(String::new(), |mut acc, (key, value)| {
} acc.push_str(&format!("\n\t{}: {}", key, **value));
acc
})
};
// 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 = format!("App Name: {}\nReplace ID: {}\nIcon: {}\nSummary: {}\nBody: {}\nActions: {}\nHints: {}\nExpiration Timeout: {}\nUrgency: {}\nDefault Action: {}",
&self.app_name(), &self.app_name(),
&self.replace_id().to_string(), &self.replace_id(),
&self.icon(), &self.icon(),
&self.summary(), &self.summary(),
&self.body(), &self.body(),
actions, actions,
hints, hints,
&self.expir_timeout().to_string(), &self.expir_timeout(),
&self.urgency(), &self.urgency(),
default_action, default_action,
); );

View File

@ -48,12 +48,17 @@ async fn main() -> Result<()> {
let properties = server_properties(); let properties = server_properties();
// Reply with the properties // Reply with the properties
connection.reply(&msg, &properties).await?; connection.reply(&msg, &properties).await?;
println!("Request received: GetAll for interface {interface_name}"); println!("GetAll request received for interface: {interface_name}");
} else { } else {
println!("Unknown interface requested: {interface_name}"); println!("Unknown interface requested: {interface_name}");
// Reply with an error or empty hashmap // Reply with an error
let empty_props: HashMap<String, String> = HashMap::new(); connection
connection.reply(&msg, &empty_props).await?; .reply_error(
&msg,
"org.freedesktop.DBus.Error.UnknownInterface",
&"Unknown interface".to_string(),
)
.await?;
} }
} }
"GetServerInformation" => { "GetServerInformation" => {
@ -75,7 +80,7 @@ async fn main() -> Result<()> {
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, &notification_id).await?; // The client waits for this response in order to disconnect connection.reply(&msg, &notification_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(); let msg_body = msg.body(); // The body has 8 parameters, ie., 8 parts of a notification
// Convert the msg body to a Notification object // Convert the msg body to a Notification object
let notif = to_notif(&msg_body)?; let notif = to_notif(&msg_body)?;
// Handle the notif // Handle the notif