-
-
Notifications
You must be signed in to change notification settings - Fork 139
🐛 xmlgen: avoid generating duplicate receive_*_changed methods
#1684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use snakecase::ascii::to_snakecase; | ||
| use std::{ | ||
| collections::HashMap, | ||
| error::Error, | ||
| fmt::{Display, Formatter, Write}, | ||
| process::{Command, Stdio}, | ||
|
|
@@ -185,6 +186,11 @@ impl GenTrait<'_> { | |
|
|
||
| let mut signals = iface.signals().to_vec(); | ||
| signals.sort_by(|a, b| a.name().partial_cmp(&b.name()).unwrap()); | ||
|
|
||
| // collect signal method names for collision detection with implicit property change | ||
| // receivers | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
| let mut changed_suffix_signals = HashMap::new(); | ||
|
|
||
| for signal in &signals { | ||
| let args = parse_signal_args(signal.args()); | ||
| let name = to_identifier(&to_snakecase(signal.name().as_str())); | ||
|
|
@@ -196,20 +202,41 @@ impl GenTrait<'_> { | |
| writeln!(w, " #[zbus(signal)]")?; | ||
| } | ||
| writeln!(w, " fn {name}({args}) -> zbus::Result<()>;",)?; | ||
|
|
||
| if name.ends_with("_changed") { | ||
| changed_suffix_signals.insert(name, signal.name()); | ||
| } | ||
| } | ||
|
|
||
| let mut props = iface.properties().to_vec(); | ||
| props.sort_by(|a, b| a.name().partial_cmp(&b.name()).unwrap()); | ||
| for p in props { | ||
| let name = to_identifier(&to_snakecase(p.name().as_str())); | ||
| let fn_attribute = if pascal_case(&name) != p.name().as_str() { | ||
| format!(" #[zbus(property, name = \"{}\")]", p.name()) | ||
|
|
||
| let prop_change_receiver_name = format!("{name}_changed"); | ||
| let signal_collision = changed_suffix_signals.get(&prop_change_receiver_name); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I liked the previous naming better. The variable isn't holding a collision but rather the |
||
|
|
||
| let (property_args, comment) = if let Some(signal_collision) = signal_collision { | ||
| ( | ||
| "(emits_changed_signal = \"false\")", | ||
| format!( | ||
| " // Note: change signal is shadowed by `{}` signal.\n", | ||
| signal_collision.as_str() | ||
| ), | ||
| ) | ||
| } else { | ||
| ("", "".to_string()) | ||
| }; | ||
| let name_arg = if pascal_case(&name) != p.name().as_str() { | ||
| format!(", name = \"{}\"", p.name()) | ||
| } else { | ||
| " #[zbus(property)]".to_string() | ||
| "".to_string() | ||
| }; | ||
| let fn_attribute = format!(" #[zbus(property{property_args}{name_arg})]"); | ||
|
|
||
| writeln!(w)?; | ||
| writeln!(w, " /// {} property", p.name())?; | ||
| write!(w, "{comment}")?; | ||
| if p.access().read() { | ||
| writeln!(w, "{fn_attribute}")?; | ||
| let output = to_rust_type(p.ty(), false, false); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,10 @@ pub trait SampleInterface0 { | |||||
| #[zbus(signal)] | ||||||
| fn signal_value(&self, value: zbus::zvariant::Value<'_>) -> zbus::Result<()>; | ||||||
|
|
||||||
| /// StateChanged signal | ||||||
| #[zbus(signal)] | ||||||
| fn state_changed(&self, state: u32, something_else: u32) -> zbus::Result<()>; | ||||||
|
|
||||||
| /// Bar property | ||||||
| #[zbus(property)] | ||||||
| fn bar(&self) -> zbus::Result<u8>; | ||||||
|
|
@@ -94,4 +98,9 @@ pub trait SampleInterface0 { | |||||
| std::collections::HashMap<String, zbus::zvariant::OwnedValue>, | ||||||
| )>, | ||||||
| >; | ||||||
|
|
||||||
| /// State property | ||||||
| // Note: change signal is shadowed by `StateChanged` signal. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? That would also make the xmlgen part simpler and you can also go back to HashSet but this time w/o the caveat of reallocation. 😉 |
||||||
| #[zbus(property(emits_changed_signal = "false"))] | ||||||
zeenix marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| fn state(&self) -> zbus::Result<u32>; | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, can't we just switch to be a HashMap instead? Wouldn't it make the code even simpler? 🤔