pop3_client/
builder.rs

1/// A builder to create a [`Client`] with a connection.
2///
3/// As it is possible to create the [`Client`] without using `Builder`, we recommend to only use in when you with to define a custom [`ClientConfig`] for the TLS connection.
4///
5/// [`Client`]: struct.Client
6/// [`ClientConfig`]: https://docs.rs/rustls/0.15.2/rustls/struct.ClientConfig.html
7pub struct Builder {
8    #[cfg(feature = "with-rustls")]
9    config: Arc<ClientConfig>,
10}
11
12impl Default for Builder {
13    #[cfg(not(feature = "with-rustls"))]
14    fn default() -> Self {
15        Self {}
16    }
17
18    #[cfg(feature = "with-rustls")]
19    fn default() -> Self {
20        let mut config = ClientConfig::new();
21        config
22            .root_store
23            .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
24
25        let config = Arc::new(config);
26
27        Self { config }
28    }
29}
30
31impl Builder {
32    /// Define a custom config for the TLS connection
33    ///
34    /// # Example
35    /// ```no_run
36    /// # use std::result::Result;
37    /// # use pop3_client::Builder;
38    ///   use rustls::ClientConfig;
39    /// #
40    /// # fn main() -> Result<(), String> {
41    ///
42    /// let mut config = ClientConfig::new();
43    /// config
44    ///     .root_store
45    ///     .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
46    ///
47    /// let client = Builder::default().rustls_config(config).connect("my.host.com", 995)?;
48    /// #    Ok(())
49    /// # }
50    /// ```
51    #[cfg(feature = "with-rustls")]
52    pub fn rustls_config(&mut self, config: ClientConfig) -> &mut Self {
53        self.config = Arc::new(config);
54        self
55    }
56}