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