Skip to content

Commit

Permalink
🐛 zbus_xmlgen: Fix structs with a single element
Browse files Browse the repository at this point in the history
When the xml element had a struct containing only a single type, the
parser was returning the element itself. This was wrong since a D-Bus
struct is expected to be translated into a tuple, regardless of how many
types it has (except for 0 elements, which is not accepted according to
D-Bus spec). Returning a single-element tuple type instead.

Note: As defined on Rust syntax, the single-element tuple type has a
trailing comma inside parenthesis, not to be confused with compiler
lint "unused_parens". Examples:
(i32,) --> OK. Single-element tuple with i32 element.
(i32) --> ERROR. Wrong use of parenthesis that clutters readability.

Co-authored-by: Vitor Nagata <[email protected]>
  • Loading branch information
Paulo Serra Filho and nagatavit committed Nov 29, 2023
1 parent 8cc2f74 commit a922fbd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion zbus_xmlgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn to_rust_type(ty: &CompleteType, input: bool, as_ref: bool) -> String {
} else if vec.len() > 1 {
format!("{}({})", if as_ref { "&" } else { "" }, vec.join(", "))
} else {
vec[0].to_string()
format!("{}({},)", if as_ref { "&" } else { "" }, vec[0])
}
}
_ => unimplemented!(),
Expand Down
3 changes: 3 additions & 0 deletions zbus_xmlgen/tests/data/sample_object0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ trait SampleInterface0 {
/// BarplexSig method
fn barplex_sig(&self, rule: &(&[i32], i32, std::collections::HashMap<&str, &str>, i32, &[i32], i32, &[&str], i32, bool)) -> zbus::Result<Vec<(String, zbus::zvariant::OwnedObjectPath)>>;

/// Bazic method
fn bazic(&self, bar: &(i32, i32), foo: &(i32,)) -> zbus::Result<((i32, i32), Vec<(i32,)>)>;

/// Bazify method
fn bazify(&self, bar: &(i32, i32, u32)) -> zbus::Result<zbus::zvariant::OwnedValue>;

Expand Down
6 changes: 6 additions & 0 deletions zbus_xmlgen/tests/data/sample_object0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<arg name="bar" type="s" direction="out"/>
<arg name="baz" type="a{us}" direction="out"/>
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
</method>
<method name="Bazic">
<arg name="bar" type="(ii)" direction="in"/>
<arg name="foo" type="(i)" direction="in"/>
<arg name="baz" type="(ii)" direction="out"/>
<arg name="foz" type="a(i)" direction="out"/>
</method>
<method name="Bazify">
<arg name="bar" type="(iiu)" direction="in"/>
Expand Down

0 comments on commit a922fbd

Please sign in to comment.