Skip to main content

zaun_client/
builder.rs

1//! Starting a [`Client`]: the SimpleX store, its passphrase, relays and timeouts.
2
3use std::io::Write;
4use std::path::PathBuf;
5use std::time::Duration;
6
7use crate::client::Client;
8use crate::error::Result;
9use crate::wire::DATA_TAG;
10
11/// Configures a [`Client`]. The directory holds the SimpleX store and `zaun/`, the community
12/// keys and state; give every client its own.
13pub struct ClientBuilder {
14    pub(crate) dir: PathBuf,
15    pub(crate) simplex: simplex_client::ClientBuilder,
16    pub(crate) timeout: Duration,
17    pub(crate) join_timeout: Duration,
18    pub(crate) transfer_timeout: Duration,
19    /// Every relay given, to know which XFTP servers uploads go to.
20    pub(crate) relays: Vec<String>,
21}
22
23impl ClientBuilder {
24    pub(crate) fn new(dir: PathBuf) -> Self {
25        Self {
26            simplex: simplex_client::Client::builder(dir.clone()).data_tag(DATA_TAG),
27            dir,
28            timeout: Duration::from_secs(60),
29            join_timeout: Duration::from_secs(120),
30            transfer_timeout: Duration::from_secs(600),
31            relays: Vec::new(),
32        }
33    }
34
35    /// The SimpleX profile name a new store starts with.
36    #[must_use]
37    pub fn name(mut self, name: impl Into<String>) -> Self {
38        self.simplex = self.simplex.name(name);
39        self
40    }
41
42    /// The store passphrase.
43    #[must_use]
44    pub fn key(mut self, key: impl Into<String>) -> Self {
45        self.simplex = self.simplex.key(key);
46        self
47    }
48
49    /// Adds a relay: an SMP server (`smp://…`) or an XFTP file server (`xftp://…`). With no
50    /// SMP server the official SimpleX servers are used, and with them the official file
51    /// servers unless an `xftp://` one is given. Uploads go only to these file servers, so
52    /// they must be among the ones a community's host downloads from
53    /// ([`CommunityData::attachments`](crate::CommunityData::attachments)).
54    #[must_use]
55    pub fn relay(mut self, address: impl Into<String>) -> Self {
56        let address = address.into();
57        self.relays.push(address.clone());
58        self.simplex = self.simplex.relay(address);
59        self
60    }
61
62    /// Where the SimpleX client logs (stderr by default).
63    #[must_use]
64    pub fn log(mut self, sink: impl Write + Send + 'static) -> Self {
65        self.simplex = self.simplex.log(sink);
66        self
67    }
68
69    /// How long a command waits for the host's answer (60 s by default).
70    #[must_use]
71    pub const fn timeout(mut self, timeout: Duration) -> Self {
72        self.timeout = timeout;
73        self
74    }
75
76    /// How long a join or a community creation may take, connection included (120 s).
77    #[must_use]
78    pub const fn join_timeout(mut self, timeout: Duration) -> Self {
79        self.join_timeout = timeout;
80        self
81    }
82
83    /// How long an attachment upload or download may take (10 minutes by default).
84    #[must_use]
85    pub const fn transfer_timeout(mut self, timeout: Duration) -> Self {
86        self.transfer_timeout = timeout;
87        self
88    }
89
90    /// Starts the SimpleX client and reconnects every saved community.
91    ///
92    /// # Errors
93    /// The SimpleX client failed to start (wrong passphrase, unreadable store).
94    pub fn start(self) -> Result<Client> {
95        Client::start(self)
96    }
97}