zbus/blocking/proxy/
builder.rs

1use zbus_names::{BusName, InterfaceName};
2use zvariant::ObjectPath;
3
4use crate::{blocking::Connection, proxy::CacheProperties, utils::block_on, Error, Result};
5
6pub use crate::proxy::Defaults;
7
8/// Builder for proxies.
9#[derive(Debug, Clone)]
10pub struct Builder<'a, T = ()>(crate::proxy::Builder<'a, T>);
11
12impl<'a, T> Builder<'a, T> {
13    /// Set the proxy destination address.
14    pub fn destination<D>(self, destination: D) -> Result<Self>
15    where
16        D: TryInto<BusName<'a>>,
17        D::Error: Into<Error>,
18    {
19        crate::proxy::Builder::destination(self.0, destination).map(Self)
20    }
21
22    /// Set the proxy path.
23    pub fn path<P>(self, path: P) -> Result<Self>
24    where
25        P: TryInto<ObjectPath<'a>>,
26        P::Error: Into<Error>,
27    {
28        crate::proxy::Builder::path(self.0, path).map(Self)
29    }
30
31    /// Set the proxy interface.
32    pub fn interface<I>(self, interface: I) -> Result<Self>
33    where
34        I: TryInto<InterfaceName<'a>>,
35        I::Error: Into<Error>,
36    {
37        crate::proxy::Builder::interface(self.0, interface).map(Self)
38    }
39
40    /// Set whether to cache properties.
41    #[must_use]
42    pub fn cache_properties(self, cache: CacheProperties) -> Self {
43        Self(self.0.cache_properties(cache))
44    }
45
46    /// Specify a set of properties (by name) which should be excluded from caching.
47    #[must_use]
48    pub fn uncached_properties(self, properties: &[&'a str]) -> Self {
49        Self(self.0.uncached_properties(properties))
50    }
51
52    /// Build a proxy from the builder.
53    ///
54    /// # Panics
55    ///
56    /// Panics if the builder is lacking the necessary details to build a proxy.
57    pub fn build(self) -> Result<T>
58    where
59        T: From<crate::Proxy<'a>>,
60    {
61        block_on(self.0.build())
62    }
63}
64
65impl<T> Builder<'_, T>
66where
67    T: Defaults,
68{
69    /// Create a new [`Builder`] for the given connection.
70    #[must_use]
71    pub fn new(conn: &Connection) -> Self {
72        Self(crate::proxy::Builder::new(&conn.clone().into()))
73    }
74}