1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use core::fmt;
use std::net::{SocketAddr, ToSocketAddrs};
use std::time::Duration;
#[derive(Clone, Debug)]
pub enum Host {
Tcp(SocketAddr),
}
impl Default for Host {
fn default() -> Self {
let addr = (String::from("localhost"), 3493)
.to_socket_addrs()
.expect("Failed to create local UPS socket address. This is a bug.")
.next()
.expect("Failed to create local UPS socket address. This is a bug.");
Self::Tcp(addr)
}
}
#[derive(Clone)]
pub struct Auth {
pub(crate) username: String,
pub(crate) password: Option<String>,
}
impl Auth {
pub fn new(username: String, password: Option<String>) -> Self {
Auth { username, password }
}
}
impl fmt::Debug for Auth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Auth")
.field("username", &self.username)
.field("password", &self.password.as_ref().map(|_| "(redacted)"))
.finish()
}
}
#[derive(Clone, Debug)]
pub struct Config {
pub(crate) host: Host,
pub(crate) auth: Option<Auth>,
pub(crate) timeout: Duration,
}
impl Config {
pub fn new(host: Host, auth: Option<Auth>, timeout: Duration) -> Self {
Config {
host,
auth,
timeout,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ConfigBuilder {
host: Option<Host>,
auth: Option<Auth>,
timeout: Option<Duration>,
}
impl ConfigBuilder {
pub fn new() -> Self {
ConfigBuilder::default()
}
pub fn with_host(mut self, host: Host) -> Self {
self.host = Some(host);
self
}
pub fn with_auth(mut self, auth: Option<Auth>) -> Self {
self.auth = auth;
self
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn build(self) -> Config {
Config::new(
self.host.unwrap_or_default(),
self.auth,
self.timeout.unwrap_or_else(|| Duration::from_secs(5)),
)
}
}