Commit ca5da70c authored by Zeeshan Ali's avatar Zeeshan Ali
Browse files

Merge branch 'fix/156' into 'main'

zm: Generate <property>_changed method for both getters and setters

Closes #156

See merge request !293
parents bb928f73 a6275bcb
Pipeline #315204 passed with stages
in 7 minutes and 41 seconds
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::collections::HashMap;
use std::collections::{hash_map::Entry, HashMap};
use syn::{
self, parse_quote, punctuated::Punctuated, AngleBracketedGenericArguments, AttributeArgs,
FnArg, Ident, ImplItem, ItemImpl, Lit::Str, Meta, Meta::NameValue, MetaList, MetaNameValue,
......@@ -179,17 +179,32 @@ pub fn expand(args: AttributeArgs, mut input: ItemImpl) -> syn::Result<TokenStre
)
});
} else if is_property {
let p = properties
.entry(member_name.to_string())
.or_insert_with(Property::new);
let p = properties.entry(member_name.to_string());
let prop_changed_method_name = format_ident!("{}_changed", snake_case(&member_name));
if matches!(p, Entry::Vacant(_)) {
let prop_changed_method = quote!(
pub fn #prop_changed_method_name(&self) -> #zbus::Result<()> {
let mut changed = ::std::collections::HashMap::new();
let value = ::#zbus::Interface::get(self, &#member_name)
.expect(&format!("Property '{}' does not exist", #member_name))?;
changed.insert(#member_name, &*value);
let properties_iface = ::#zbus::fdo::Properties;
properties_iface.properties_changed(
&#iface_name,
&changed,
&[],
)
}
);
generated_signals.extend(prop_changed_method);
}
let p = p.or_insert_with(Property::new);
p.doc_comments.extend(doc_comments);
if has_inputs {
p.write = true;
let prop_changed_method_name =
format_ident!("{}_changed", snake_case(&member_name));
let set_call = if is_result_output {
quote!(self.#ident(val))
} else {
......@@ -209,23 +224,6 @@ pub fn expand(args: AttributeArgs, mut input: ItemImpl) -> syn::Result<TokenStre
}
);
set_dispatch.extend(q);
let vis = &method.vis;
let q = quote!(
#vis fn #prop_changed_method_name(&self) -> #zbus::Result<()> {
let mut changed = ::std::collections::HashMap::new();
let value = ::#zbus::Interface::get(self, &#member_name)
.expect(&format!("Property '{}' does not exist", #member_name))?;
changed.insert(#member_name, &*value);
let properties_iface = ::#zbus::fdo::Properties;
properties_iface.properties_changed(
&#iface_name,
&changed,
&[]
)
}
);
generated_signals.extend(q)
} else {
p.ty = Some(get_property_type(output)?);
p.read = true;
......
......@@ -164,9 +164,9 @@ pub fn dbus_proxy(attr: TokenStream, item: TokenStream) -> TokenStream {
/// * `out_args` - When returning multiple values from a method, naming the out arguments become
/// important. You can use `out_args` for specifying names for your out arguments.
///
/// Note: a `<property_name_in_snake_case>_changed` method is generated along with property
/// setters: this method emits the "PropertiesChanged" signal for the associated property. The
/// setter will automatically call this method.
/// Note: a `<property_name_in_snake_case>_changed` method is generated for each property: this
/// method emits the "PropertiesChanged" signal for the associated property. The setter (if it
/// exists) will automatically call this method.
/// For instance, a property setter named `set_foo` will be called to set the property "Foo", and
/// will emit the "PropertiesChanged" signal with the new value for "Foo". Other changes to the
/// "Foo" property can be signaled manually with the generated `foo_changed` method.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment