Skip to content

Commit

Permalink
Merge pull request #445 from zeenix/macro-improv
Browse files Browse the repository at this point in the history
A fix & an API change to dbus_proxy
  • Loading branch information
zeenix authored Aug 17, 2023
2 parents d1e10c3 + 35ae174 commit 383ef04
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
5 changes: 4 additions & 1 deletion book/src/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ use std::error::Error;
use zbus::{zvariant::Value, dbus_proxy, Connection};
#[dbus_proxy]
#[dbus_proxy(
default_service = "org.freedesktop.Notifications",
default_path = "/org/freedesktop/Notifications"
)]
trait Notifications {
/// Call the org.freedesktop.Notifications.Notify D-Bus method
fn notify(&self,
Expand Down
7 changes: 3 additions & 4 deletions zbus_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ mod utils;
/// * `blocking_name` - Specify the exact name of the blocking proxy type.
///
/// * `assume_defaults` - whether to auto-generate values for `default_path` and `default_service`
/// if none are specified (default: `true`). `dbus_proxy` currently generates a warning if neither
/// this attribute nor one of the default values are specified. A future release will change the
/// default to `false`. Please make sure to explicitly set either this attribute or the default
/// values, according to your needs.
/// if none are specified (default: `false`). `dbus_proxy` generates a warning if neither this
/// attribute nor one of the default values are specified. Please make sure to explicitly set
/// either this attribute or the default values, according to your needs.
///
/// Each trait method will be expanded to call to the associated D-Bus remote interface.
///
Expand Down
39 changes: 17 additions & 22 deletions zbus_macros/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use proc_macro2::{Literal, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use regex::Regex;
use syn::{
self, fold::Fold, parse_quote, spanned::Spanned, AttributeArgs, Error, FnArg, Ident, ItemTrait,
ReturnType, TraitItemMethod,
self, fold::Fold, parse_quote, parse_str, spanned::Spanned, AttributeArgs, Error, FnArg, Ident,
ItemTrait, Path, ReturnType, TraitItemMethod,
};
use zvariant_utils::{case, def_attrs};

Expand Down Expand Up @@ -170,12 +170,7 @@ pub fn create_proxy(
let iface_name = iface_name
.map(ToString::to_string)
.unwrap_or(format!("org.freedesktop.{ident}"));
if assume_defaults.is_none() && default_path.is_none() && default_service.is_none() {
eprintln!(
"#[dbus_proxy(...)] macro invocation on '{proxy_name}' without explicit defaults. Please set 'assume_defaults = true', or configure default path/service directly."
);
};
let assume_defaults = assume_defaults.unwrap_or(true);
let assume_defaults = assume_defaults.unwrap_or(false);
let (default_path, default_service) = if assume_defaults {
let path = default_path
.map(ToString::to_string)
Expand Down Expand Up @@ -252,7 +247,7 @@ pub fn create_proxy(

method
} else {
gen_proxy_method_call(&member_name, &method_name, m, &attrs, &async_opts)
gen_proxy_method_call(&member_name, &method_name, m, &attrs, &async_opts)?
};
methods.extend(m);
}
Expand Down Expand Up @@ -476,7 +471,7 @@ fn gen_proxy_method_call(
m: &TraitItemMethod,
attrs: &MethodAttributes,
async_opts: &AsyncOpts,
) -> TokenStream {
) -> Result<TokenStream, Error> {
let AsyncOpts {
usage,
wait,
Expand Down Expand Up @@ -580,14 +575,14 @@ fn gen_proxy_method_call(
}
let (_, ty_generics, where_clause) = generics.split_for_impl();

if let Some(proxy_name) = proxy_object {
let proxy = Ident::new(&proxy_name, Span::call_site());
if let Some(proxy_path) = proxy_object {
let proxy_path = parse_str::<Path>(&proxy_path)?;
let signature = quote! {
fn #method #ty_generics(#inputs) -> #zbus::Result<#proxy<'c>>
fn #method #ty_generics(#inputs) -> #zbus::Result<#proxy_path<'c>>
#where_clause
};

quote! {
Ok(quote! {
#(#other_attrs)*
pub #usage #signature {
let object_path: #zbus::zvariant::OwnedObjectPath =
Expand All @@ -596,12 +591,12 @@ fn gen_proxy_method_call(
&(#(#args),*),
)
#wait?;
#proxy::builder(&self.0.connection())
#proxy_path::builder(&self.0.connection())
.path(object_path)?
.build()
#wait
}
}
})
} else {
let body = if args.len() == 1 {
// Wrap single arg in a tuple so if it's a struct/tuple itself, zbus will only remove
Expand All @@ -624,15 +619,15 @@ fn gen_proxy_method_call(

if let Some(method_flags) = method_flags {
if no_reply {
quote! {
Ok(quote! {
#(#other_attrs)*
pub #usage #signature {
self.0.call_with_flags::<_, _, ()>(#method_name, #method_flags, #body)#wait?;
::std::result::Result::Ok(())
}
}
})
} else {
quote! {
Ok(quote! {
#(#other_attrs)*
pub #usage #signature {
let reply = self.0.call_with_flags(#method_name, #method_flags, #body)#wait?;
Expand All @@ -645,16 +640,16 @@ fn gen_proxy_method_call(
// unwrap
::std::result::Result::Ok(reply.unwrap())
}
}
})
}
} else {
quote! {
Ok(quote! {
#(#other_attrs)*
pub #usage #signature {
let reply = self.0.call(#method_name, #body)#wait?;
::std::result::Result::Ok(reply)
}
}
})
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions zbus_macros/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ use std::future::ready;
use zbus::{block_on, fdo, object_server::SignalContext, proxy::CacheProperties};
use zbus_macros::{dbus_interface, dbus_proxy, DBusError};

#[test]
fn test_proxy() {
#[dbus_proxy(
mod param {
#[zbus_macros::dbus_proxy(
interface = "org.freedesktop.zbus_macros.ProxyParam",
default_service = "org.freedesktop.zbus_macros",
default_path = "/org/freedesktop/zbus_macros/test"
)]
trait ProxyParam {
#[dbus_proxy(object = "Test")]
#[dbus_proxy(object = "super::test::Test")]
fn some_method<T>(&self, test: &T);
}
}

#[dbus_proxy(
mod test {
use zbus::fdo;

#[zbus_macros::dbus_proxy(
assume_defaults = false,
interface = "org.freedesktop.zbus_macros.Test",
default_service = "org.freedesktop.zbus_macros"
Expand Down Expand Up @@ -51,10 +54,13 @@ fn test_proxy() {
where
T: AsRef<str>;
}
}

#[test]
fn test_proxy() {
block_on(async move {
let connection = zbus::Connection::session().await.unwrap();
let proxy = TestProxy::builder(&connection)
let proxy = test::TestProxy::builder(&connection)
.path("/org/freedesktop/zbus_macros/test")
.unwrap()
.cache_properties(CacheProperties::No)
Expand Down
6 changes: 3 additions & 3 deletions zbus_xmlgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
};

use zbus::{
blocking::{connection, proxy::Builder as ProxyBuilder, Connection},
blocking::{connection, fdo::IntrospectableProxy, Connection},
names::BusName,
xml::{Interface, Node},
};
Expand All @@ -22,8 +22,8 @@ use zvariant::ObjectPath;
fn main() -> Result<(), Box<dyn Error>> {
let input_src;

let proxy = |conn: Connection, service, path| -> zbus::blocking::fdo::IntrospectableProxy<'_> {
ProxyBuilder::new(&conn)
let proxy = |conn: Connection, service, path| -> IntrospectableProxy<'_> {
IntrospectableProxy::builder(&conn)
.destination(service)
.expect("invalid destination")
.path(path)
Expand Down

0 comments on commit 383ef04

Please sign in to comment.