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

Merge branch 'remove-zm-builders' into 'main'

Remove the generated builders

See merge request !291
parents 3909d7d5 9924fc3a
Pipeline #310035 passed with stages
in 4 minutes and 57 seconds
......@@ -67,7 +67,7 @@ trait Notifications {
fn main() -> Result<(), Box<dyn Error>> {
let connection = zbus::Connection::new_session()?;
let proxy = NotificationsProxy::new(&connection)?;
let proxy = NotificationsProxy::new(&connection);
let reply = proxy.notify(
"my-app",
0,
......@@ -106,7 +106,7 @@ impl Greeter {
fn main() -> Result<(), Box<dyn Error>> {
let connection = zbus::Connection::new_session()?;
fdo::DBusProxy::new(&connection)?.request_name(
fdo::DBusProxy::new(&connection).request_name(
"org.zbus.MyGreeter",
fdo::RequestNameFlags::ReplaceExisting.into(),
)?;
......@@ -278,7 +278,7 @@ use zbus::Connection;
use zbus_polkit::policykit1::*;
let connection = Connection::new_system().unwrap();
let proxy = AuthorityProxy::new(&connection).unwrap();
let proxy = AuthorityProxy::new(&connection);
let subject = Subject::new_for_owner(std::process::id(), None, None).unwrap();
let result = proxy.check_authorization(
&subject,
......
......@@ -260,7 +260,7 @@ client.set_desktop_id("org.freedesktop.zbus").unwrap();
client
.connect_location_updated(move |_old, new| {
let location = LocationProxy::new_for_path(&conn, &new)?;
let location = LocationProxy::new_for_path(&conn, &new);
println!(
"Latitude: {}\nLongitude: {}",
location.latitude()?,
......@@ -326,7 +326,7 @@ actually getting set:
# let manager = ManagerProxy::new(&conn).unwrap();
# let client_path = manager.get_client().unwrap();
# let mut client =
# ClientProxy::new_for(&conn, "org.freedesktop.GeoClue2", &client_path).unwrap();
# ClientProxy::new_for(&conn, "org.freedesktop.GeoClue2", &client_path);
# // Gotta do this, sorry!
# client.set_desktop_id("org.freedesktop.zbus").unwrap();
#
......
......@@ -64,7 +64,7 @@ async fn run() -> Result<()> {
fn longitude(&self) -> Result<f64>;
}
let conn = Connection::new_system().await?;
let manager = AsyncManagerProxy::new(&conn)?;
let manager = AsyncManagerProxy::new(&conn);
let mut client = manager.get_client().await?;
// Gotta do this, sorry!
client.set_desktop_id("org.freedesktop.zbus").await?;
......@@ -75,7 +75,7 @@ async fn run() -> Result<()> {
let conn = conn.clone();
async move {
let location = AsyncLocationProxyBuilder::new(&conn)?.path(new)?.build()?;
let location = AsyncLocationProxy::builder(&conn).path(new)?.build();
println!(
"Latitude: {}\nLongitude: {}",
location.latitude().await?,
......@@ -152,16 +152,16 @@ example again to receive multiple signals on different proxies:
# fn longitude(&self) -> Result<f64>;
# }
# let conn = Connection::new_system().await?;
# let manager = AsyncManagerProxy::new(&conn)?;
# let manager = AsyncManagerProxy::new(&conn);
# let mut client = manager.get_client().await?;
#
// Everything else remains the same before this point.
client.set_desktop_id("org.freedesktop.zbus").await?;
let props = zbus::fdo::AsyncPropertiesProxyBuilder::new(&conn)?
let props = zbus::fdo::AsyncPropertiesProxy::builder(&conn)
.destination("org.freedesktop.GeoClue2")
.path(client.path())?
.build()?;
.build();
props
.connect_properties_changed(move |iface, changed, _| {
for (name, value) in changed.iter() {
......@@ -178,7 +178,9 @@ example again to receive multiple signals on different proxies:
let conn = conn.clone();
async move {
let location = AsyncLocationProxyBuilder::new(&conn)?.path(new)?.build()?;
let location = AsyncLocationProxy::builder(&conn)
.path(new)?
.build();
println!(
"Latitude: {}\nLongitude: {}",
location.latitude().await?,
......
......@@ -115,7 +115,7 @@ trait Notifications {
fn main() -> Result<(), Box<dyn Error>> {
let connection = zbus::Connection::new_session()?;
let proxy = NotificationsProxy::new(&connection)?;
let proxy = NotificationsProxy::new(&connection);
let reply = proxy.notify("my-app", 0, "dialog-information", "A summary", "Some body",
&[], HashMap::new(), 5000)?;
dbg!(reply);
......@@ -173,7 +173,7 @@ trait SystemdManager {
fn main() -> Result<(), Box<dyn Error>> {
let connection = zbus::Connection::new_session()?;
let proxy = SystemdManagerProxy::new(&connection)?;
let proxy = SystemdManagerProxy::new(&connection);
println!("Host architecture: {}", proxy.architecture()?);
println!("Environment:");
for env in proxy.environment()? {
......@@ -254,14 +254,16 @@ trait Location {
fn longitude(&self) -> Result<f64>;
}
let conn = Connection::new_system().unwrap();
let manager = ManagerProxy::new(&conn).unwrap();
let manager = ManagerProxy::new(&conn);
let mut client = manager.get_client().unwrap();
// Gotta do this, sorry!
client.set_desktop_id("org.freedesktop.zbus").unwrap();
client
.connect_location_updated(move |_old, new| {
let location = LocationProxyBuilder::new(&conn)?.path(new.as_str())?.build()?;
let location = LocationProxy::builder(&conn)
.path(new.as_str())?
.build();
println!(
"Latitude: {}\nLongitude: {}",
location.latitude()?,
......@@ -325,7 +327,7 @@ actually getting set:
# }
#
# let conn = Connection::new_system().unwrap();
# let manager = ManagerProxy::new(&conn).unwrap();
# let manager = ManagerProxy::new(&conn);
# let mut client = manager.get_client().unwrap();
# // Gotta do this, sorry!
# client.set_desktop_id("org.freedesktop.zbus").unwrap();
......@@ -334,10 +336,10 @@ actually getting set:
let conn_clone = conn.clone();
client.connect_location_updated(move |_old, new| {
let location = LocationProxyBuilder::new(&conn_clone)?
let location = LocationProxy::builder(&conn_clone)
.destination("org.freedesktop.GeoClue2")
.path(new.as_str())?
.build()?;
.build();
println!(
"Latitude: {}\nLongitude: {}",
location.latitude()?,
......@@ -347,10 +349,10 @@ client.connect_location_updated(move |_old, new| {
Ok(())
}).unwrap();
let props = zbus::fdo::PropertiesProxyBuilder::new(&conn).unwrap()
let props = zbus::fdo::PropertiesProxy::builder(&conn)
.destination("org.freedesktop.GeoClue2")
.path(client.path()).unwrap()
.build().unwrap();
.build();
props.connect_properties_changed(|iface, changed, _| {
for (name, value) in changed.iter() {
println!("{}.{} changed to `{:?}`", iface, name, value);
......
......@@ -22,7 +22,7 @@ use zbus::fdo;
fn main() -> std::result::Result<(), Box<dyn Error>> {
let connection = Connection::new_session()?;
fdo::DBusProxy::new(&connection)?.request_name(
fdo::DBusProxy::new(&connection).request_name(
"org.zbus.MyGreeter",
fdo::RequestNameFlags::ReplaceExisting.into(),
)?;
......@@ -53,7 +53,7 @@ by replacing the loop above with this code:
```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let connection = zbus::Connection::new_session()?;
# zbus::fdo::DBusProxy::new(&connection)?.request_name(
# zbus::fdo::DBusProxy::new(&connection).request_name(
# "org.zbus.MyGreeter",
# zbus::fdo::RequestNameFlags::ReplaceExisting.into(),
# )?;
......@@ -132,7 +132,7 @@ impl Greeter {
fn main() -> Result<(), Box<dyn Error>> {
let connection = zbus::Connection::new_session()?;
# fdo::DBusProxy::new(&connection)?.request_name(
# fdo::DBusProxy::new(&connection).request_name(
# "org.zbus.MyGreeter",
# fdo::RequestNameFlags::ReplaceExisting.into(),
# )?;
......
......@@ -685,7 +685,7 @@ impl Connection {
None => {
let match_rule = signal.create_match_rule();
if let Some(match_rule) = &match_rule {
fdo::AsyncDBusProxy::new(&self)?
fdo::AsyncDBusProxy::new(&self)
.add_match(&match_rule)
.await?;
}
......@@ -727,7 +727,7 @@ impl Connection {
if subscription.num_subscribers == 0 {
if let Some(match_rule) = &subscription.match_rule {
fdo::AsyncDBusProxy::new(&self)?
fdo::AsyncDBusProxy::new(&self)
.remove_match(match_rule.as_str())
.await?;
}
......@@ -742,7 +742,7 @@ impl Connection {
}
async fn hello_bus(self) -> Result<Self> {
let name = fdo::AsyncDBusProxy::new(&self)?.hello().await?;
let name = fdo::AsyncDBusProxy::new(&self).hello().await?;
self.0
.unique_name
......
......@@ -18,9 +18,11 @@ use std::{
use async_io::block_on;
use zvariant::{ObjectPath, OwnedValue, Value};
use crate::{azync::Connection, Error, Message, Result};
use crate::fdo::{self, AsyncIntrospectableProxyBuilder, AsyncPropertiesProxyBuilder};
use crate::{
azync::Connection,
fdo::{self, AsyncIntrospectableProxy, AsyncPropertiesProxy},
Error, Message, Result,
};
type SignalHandler = Box<dyn for<'msg> FnMut(&'msg Message) -> BoxFuture<'msg, Result<()>> + Send>;
......@@ -90,16 +92,16 @@ struct SignalHandlerInfo {
/// [`dbus_proxy`]: attr.dbus_proxy.html
#[derive(Debug)]
pub struct Proxy<'a> {
inner: Arc<ProxyInner<'a>>,
pub(crate) inner: Arc<ProxyInner<'a>>,
}
#[derive(derivative::Derivative)]
#[derivative(Debug)]
struct ProxyInner<'a> {
conn: Connection,
destination: Cow<'a, str>,
path: ObjectPath<'a>,
interface: Cow<'a, str>,
pub(crate) struct ProxyInner<'a> {
pub(crate) conn: Connection,
pub(crate) destination: Cow<'a, str>,
pub(crate) path: ObjectPath<'a>,
pub(crate) interface: Cow<'a, str>,
dest_unique_name: OnceCell<String>,
#[derivative(Debug = "ignore")]
sig_handlers: Mutex<SlotMap<SignalHandlerId, SignalHandlerInfo>>,
......@@ -123,65 +125,6 @@ impl<'a> ProxyInner<'a> {
}
}
/// Builder for [`Proxy`].
#[derive(Debug, Clone)]
pub struct ProxyBuilder<'a> {
conn: Connection,
destination: Option<Cow<'a, str>>,
path: Option<ObjectPath<'a>>,
interface: Option<Cow<'a, str>>,
}
impl<'a> ProxyBuilder<'a> {
/// Create a new [`ProxyBuilder`] for the given connection.
pub fn new(conn: &Connection) -> Self {
Self {
conn: conn.clone(),
destination: None,
path: None,
interface: None,
}
}
/// Set the proxy destination address.
pub fn destination<D: Into<Cow<'a, str>>>(mut self, destination: D) -> Self {
self.destination = Some(destination.into());
self
}
/// Set the proxy path.
pub fn path<E, P: TryInto<ObjectPath<'a>, Error = E>>(mut self, path: P) -> Result<Self>
where
Error: From<E>,
{
self.path = Some(path.try_into()?);
Ok(self)
}
/// Set the proxy interface.
pub fn interface<I: Into<Cow<'a, str>>>(mut self, interface: I) -> Self {
self.interface = Some(interface.into());
self
}
/// Build a [`Proxy`] from the builder.
///
/// An error is returned when the builder is lacking the necessary details.
pub fn build(self) -> Result<Proxy<'a>> {
match self {
ProxyBuilder {
conn,
destination: Some(destination),
path: Some(path),
interface: Some(interface),
} => Ok(Proxy {
inner: Arc::new(ProxyInner::new(conn, destination, path, interface)),
}),
_ => Err(Error::Unsupported),
}
}
}
impl<'a> Proxy<'a> {
/// Create a new `Proxy` for the given destination/path/interface.
pub fn new<E>(
......@@ -193,11 +136,11 @@ impl<'a> Proxy<'a> {
where
Error: From<E>,
{
ProxyBuilder::new(conn)
Ok(crate::ProxyBuilder::new_bare(conn)
.destination(destination)
.path(path)?
.interface(interface)
.build()
.build_bare_async())
}
/// Create a new `Proxy` for the given destination/path/interface, taking ownership of all
......@@ -211,11 +154,11 @@ impl<'a> Proxy<'a> {
where
Error: From<E>,
{
ProxyBuilder::new(&conn)
Ok(crate::ProxyBuilder::new_bare(&conn)
.destination(destination)
.path(path)?
.interface(interface)
.build()
.build_bare_async())
}
/// Get a reference to the associated connection.
......@@ -242,12 +185,12 @@ impl<'a> Proxy<'a> {
///
/// See the [xml](xml/index.html) module for parsing the result.
pub async fn introspect(&self) -> fdo::Result<String> {
AsyncIntrospectableProxyBuilder::new(&self.inner.conn)?
let proxy = AsyncIntrospectableProxy::builder(&self.inner.conn)
.destination(self.inner.destination.as_ref())
.path(&self.inner.path)?
.build()?
.introspect()
.await
.build();
proxy.introspect().await
}
/// Get the property `property_name`.
......@@ -257,10 +200,12 @@ impl<'a> Proxy<'a> {
where
T: TryFrom<OwnedValue>,
{
AsyncPropertiesProxyBuilder::new(&self.inner.conn)?
let proxy = AsyncPropertiesProxy::builder(&self.inner.conn)
.destination(self.inner.destination.as_ref())
.path(&self.inner.path)?
.build()?
.build();
proxy
.get(&self.inner.interface, property_name)
.await?
.try_into()
......@@ -274,10 +219,12 @@ impl<'a> Proxy<'a> {
where
T: Into<Value<'t>>,
{
AsyncPropertiesProxyBuilder::new(&self.inner.conn)?
let proxy = AsyncPropertiesProxy::builder(&self.inner.conn)
.destination(self.inner.destination.as_ref())
.path(&self.inner.path)?
.build()?
.build();
proxy
.set(&self.inner.interface, property_name, &value.into())
.await
}
......@@ -578,7 +525,7 @@ impl<'a> Proxy<'a> {
let unique_name = if destination.starts_with(':') || destination == "org.freedesktop.DBus" {
destination.to_string()
} else {
fdo::AsyncDBusProxy::new(&self.inner.conn)?
fdo::AsyncDBusProxy::new(&self.inner.conn)
.get_name_owner(destination)
.await?
};
......@@ -656,32 +603,6 @@ mod tests {
use futures_util::future::FutureExt;
use std::{future::ready, sync::Arc};
#[test]
fn builder() {
block_on(test_builder()).unwrap();
}
async fn test_builder() -> Result<()> {
let conn = Connection::new_session().await?;
let builder = ProxyBuilder::new(&conn);
assert!(builder.clone().build().is_err());
let builder = builder
.destination("org.freedesktop.DBus")
.path("/some/path")?
.interface("org.freedesktop.Interface");
assert!(matches!(
builder.clone().destination.unwrap(),
Cow::Borrowed(_)
));
let proxy = builder.build().unwrap();
assert!(matches!(proxy.inner.destination, Cow::Borrowed(_)));
assert!(matches!(proxy.inner.interface, Cow::Borrowed(_)));
Ok(())
}
#[test]
fn signal_stream() {
block_on(test_signal_stream()).unwrap();
......@@ -838,7 +759,6 @@ mod tests {
};
fdo::DBusProxy::new(&crate::Connection::from(conn))
.unwrap()
.request_name(&well_known, fdo::RequestNameFlags::ReplaceExisting.into())
.unwrap();
......
......@@ -628,7 +628,7 @@ mod tests {
let owner_change_signaled = Arc::new(Mutex::new(false));
let name_acquired_signaled = Arc::new(Mutex::new(false));
let proxy = fdo::DBusProxy::new(&conn).unwrap();
let proxy = fdo::DBusProxy::new(&conn);
let well_known = "org.freedesktop.zbus.FdoSignalTest";
let unique_name = conn.unique_name().unwrap().to_string();
......
......@@ -45,7 +45,7 @@
//! fn main() -> Result<(), Box<dyn Error>> {
//! let connection = zbus::Connection::new_session()?;
//!
//! let proxy = NotificationsProxy::new(&connection)?;
//! let proxy = NotificationsProxy::new(&connection);
//! let reply = proxy.notify(
//! "my-app",
//! 0,
......@@ -84,7 +84,7 @@
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//! let connection = zbus::Connection::new_session()?;
//! fdo::DBusProxy::new(&connection)?.request_name(
//! fdo::DBusProxy::new(&connection).request_name(
//! "org.zbus.MyGreeter",
//! fdo::RequestNameFlags::ReplaceExisting.into(),
//! )?;
......@@ -165,6 +165,9 @@ pub use connection::*;
mod proxy;
pub use proxy::*;
mod proxy_builder;
pub use proxy_builder::*;
mod signal_receiver;
pub use signal_receiver::*;
......@@ -545,7 +548,7 @@ mod tests {
.unwrap();
let serial = client_conn.send_message(msg).unwrap();
crate::fdo::DBusProxy::new(&conn).unwrap().get_id().unwrap();
crate::fdo::DBusProxy::new(&conn).get_id().unwrap();
loop {
let msg = conn.receive_message().unwrap();
......@@ -606,13 +609,11 @@ mod tests {
) -> zbus::Result<(OwnedValue, OwnedObjectPath)>;
}
let proxy = SecretProxyBuilder::new(&conn)
.unwrap()
let proxy = SecretProxy::builder(&conn)
.destination(&service_name)
.path("/org/freedesktop/secrets")
.unwrap()
.build()
.unwrap();
.build();
proxy.open_session("plain", &Value::from("")).unwrap();
......
......@@ -706,15 +706,15 @@ mod tests {
fn my_iface_test(tx: Sender<()>) -> std::result::Result<u32, Box<dyn Error>> {
let conn = Connection::new_session()?;
let proxy = MyIfaceProxyBuilder::new(&conn)?
let proxy = MyIfaceProxy::builder(&conn)
.destination("org.freedesktop.MyService")
.path("/org/freedesktop/MyService")?
.build()?;
.build();
let props_proxy = zbus::fdo::PropertiesProxyBuilder::new(&conn)?
let props_proxy = zbus::fdo::PropertiesProxy::builder(&conn)
.destination("org.freedesktop.MyService")
.path("/org/freedesktop/MyService")?
.build()?;
.build();
props_proxy
.connect_properties_changed(|_, changed, _| {
......@@ -773,10 +773,10 @@ mod tests {
let val = proxy.ping()?;
proxy.create_obj("MyObj")?;
let my_obj_proxy = MyIfaceProxyBuilder::new(&conn)?
let my_obj_proxy = MyIfaceProxy::builder(&conn)
.destination("org.freedesktop.MyService")
.path("/zbus/test/MyObj")?
.build()?;
.build();
my_obj_proxy.ping()?;
proxy.destroy_obj("MyObj")?;
assert!(my_obj_proxy.introspect().is_err());
......@@ -796,7 +796,6 @@ mod tests {
let action = Rc::new(Cell::new(NextAction::Nothing));
fdo::DBusProxy::new(&conn)
.unwrap()
.request_name(
"org.freedesktop.MyService",
fdo::RequestNameFlags::ReplaceExisting.into(),
......
......@@ -332,7 +332,6 @@ mod tests {
}
fdo::DBusProxy::new(&conn)
.unwrap()
.request_name(&well_known, fdo::RequestNameFlags::ReplaceExisting.into())
.unwrap();
......
use std::{borrow::Cow, convert::TryInto, marker::PhantomData, sync::Arc};
use zvariant::ObjectPath;
use crate::{azync, Error, Proxy, Result};
/// Builder for [`Proxy`]
#[derive(Debug, Clone)]
pub struct ProxyBuilder<'a, T = ()> {
conn: azync::Connection,
destination: Option<Cow<'a, str>>,
path: Option<ObjectPath<'a>>,
interface: Option<Cow<'a, str>>,
proxy_type: PhantomData<T>,
}
impl<'a> ProxyBuilder<'a> {
/// Create a new [`ProxyBuilder`] for the given connection.
pub fn new_bare<C>(conn: &C) -> Self
where
C: Clone + Into<azync::Connection>,
{
Self {
conn: conn.clone().into(),
destination: None,
path: None,
interface: None,
proxy_type: PhantomData,
}
}
}
impl<'a, T> ProxyBuilder<'a, T> {
/// Set the proxy destination address.
pub fn destination<D: Into<Cow<'a, str>>>(mut self, destination: D) -> Self {
self.destination = Some(destination.into());
self
}
/// Set the proxy path.
pub fn path<E, P: TryInto<ObjectPath<'a>, Error = E>>(mut self, path: P) -> Result<Self>
where
Error: From<E>,
{
self.path = Some(path.try_into()?);
Ok(self)
}
/// Set the proxy interface.
pub fn interface<I: Into<Cow<'a, str>>>(mut self, interface: I) -> Self {
self.interface = Some(interface.into());
self
}