better ways to convert actions & hints to string, & handle GetAll
This commit is contained in:
parent
7caf294f38
commit
df42d63461
@ -4,39 +4,44 @@ impl Notification {
|
||||
// Plain string format, useful for printing
|
||||
pub fn plain(&self) -> String {
|
||||
// Actions
|
||||
let mut actions: String = String::new();
|
||||
if self.actions().is_empty() {
|
||||
actions = "None".to_string();
|
||||
let actions: String = if self.actions().is_empty() {
|
||||
"None".to_string()
|
||||
} else {
|
||||
for actn in &self.actions() {
|
||||
actions = actions + "\n\t" + actn.0.as_str() + ": " + actn.1.as_str();
|
||||
}
|
||||
}
|
||||
self.actions()
|
||||
.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
|
||||
let default_action: String = match self.default_action() {
|
||||
None => "None".to_string(),
|
||||
Some(actn) => actn.0,
|
||||
};
|
||||
// Hints
|
||||
let mut hints: String = String::new();
|
||||
if self.hints().is_empty() {
|
||||
hints = "None".to_string();
|
||||
let hints = if self.hints().is_empty() {
|
||||
"None".to_string()
|
||||
} else {
|
||||
for (key, value) in self.hints() {
|
||||
hints = hints + "\n\t" + key + ": " + &value.to_string();
|
||||
}
|
||||
}
|
||||
self.hints()
|
||||
.iter()
|
||||
.fold(String::new(), |mut acc, (key, value)| {
|
||||
acc.push_str(&format!("\n\t{}: {}", key, **value));
|
||||
acc
|
||||
})
|
||||
};
|
||||
|
||||
// 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: {}",
|
||||
&self.app_name(),
|
||||
&self.replace_id().to_string(),
|
||||
&self.replace_id(),
|
||||
&self.icon(),
|
||||
&self.summary(),
|
||||
&self.body(),
|
||||
actions,
|
||||
hints,
|
||||
&self.expir_timeout().to_string(),
|
||||
&self.expir_timeout(),
|
||||
&self.urgency(),
|
||||
default_action,
|
||||
);
|
||||
|
15
src/main.rs
15
src/main.rs
@ -48,12 +48,17 @@ async fn main() -> Result<()> {
|
||||
let properties = server_properties();
|
||||
// Reply with the properties
|
||||
connection.reply(&msg, &properties).await?;
|
||||
println!("Request received: GetAll for interface {interface_name}");
|
||||
println!("GetAll request received for interface: {interface_name}");
|
||||
} else {
|
||||
println!("Unknown interface requested: {interface_name}");
|
||||
// Reply with an error or empty hashmap
|
||||
let empty_props: HashMap<String, String> = HashMap::new();
|
||||
connection.reply(&msg, &empty_props).await?;
|
||||
// Reply with an error
|
||||
connection
|
||||
.reply_error(
|
||||
&msg,
|
||||
"org.freedesktop.DBus.Error.UnknownInterface",
|
||||
&"Unknown interface".to_string(),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
"GetServerInformation" => {
|
||||
@ -75,7 +80,7 @@ async fn main() -> Result<()> {
|
||||
let notification_id: u32 = 1; // This could be incremented or generated. DO IT LATER
|
||||
connection.reply(&msg, ¬ification_id).await?; // The client waits for this response in order to disconnect
|
||||
// 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
|
||||
let notif = to_notif(&msg_body)?;
|
||||
// Handle the notif
|
||||
|
Loading…
Reference in New Issue
Block a user