electrum_client_netagnostic/
config.rs1use std::time::Duration;
2
3#[derive(Debug, Clone)]
10pub struct Config {
11 socks5: Option<Socks5Config>,
13 timeout: Option<Duration>,
15 retry: u8,
17 validate_domain: bool,
19 max_message_size: Option<usize>,
21}
22
23#[derive(Debug, Clone)]
25pub struct Socks5Config {
26 pub addr: String,
28 pub credentials: Option<Socks5Credential>,
30}
31
32#[derive(Debug, Clone)]
34pub struct Socks5Credential {
35 pub username: String,
36 pub password: String,
37}
38
39pub struct ConfigBuilder {
41 config: Config,
42}
43
44impl ConfigBuilder {
45 pub fn new() -> Self {
47 ConfigBuilder {
48 config: Config::default(),
49 }
50 }
51
52 pub fn socks5(mut self, socks5_config: Option<Socks5Config>) -> Self {
55 self.config.socks5 = socks5_config;
56 self
57 }
58
59 pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
61 self.config.timeout = timeout;
62 self
63 }
64
65 pub fn retry(mut self, retry: u8) -> Self {
67 self.config.retry = retry;
68 self
69 }
70
71 pub fn validate_domain(mut self, validate_domain: bool) -> Self {
73 self.config.validate_domain = validate_domain;
74 self
75 }
76
77 pub fn max_message_size(mut self, v: Option<usize>) -> Self {
79 self.config.max_message_size = v;
80 self
81 }
82
83 pub fn build(self) -> Config {
85 self.config
86 }
87}
88
89impl Default for ConfigBuilder {
90 fn default() -> Self {
91 Self::new()
92 }
93}
94
95impl Socks5Config {
96 pub fn new(addr: impl ToString) -> Self {
98 let addr = addr.to_string().replacen("socks5://", "", 1);
99 Socks5Config {
100 addr,
101 credentials: None,
102 }
103 }
104
105 pub fn with_credentials(addr: impl ToString, username: String, password: String) -> Self {
107 let mut config = Socks5Config::new(addr);
108 config.credentials = Some(Socks5Credential { username, password });
109 config
110 }
111}
112
113impl Config {
114 pub fn socks5(&self) -> &Option<Socks5Config> {
118 &self.socks5
119 }
120
121 pub fn retry(&self) -> u8 {
125 self.retry
126 }
127
128 pub fn timeout(&self) -> Option<Duration> {
132 self.timeout
133 }
134
135 pub fn validate_domain(&self) -> bool {
139 self.validate_domain
140 }
141
142 pub fn max_message_size(&self) -> Option<usize> {
146 self.max_message_size
147 }
148
149 pub fn builder() -> ConfigBuilder {
151 ConfigBuilder::new()
152 }
153}
154
155impl Default for Config {
156 fn default() -> Self {
157 Config {
158 socks5: None,
159 timeout: None,
160 retry: 1,
161 validate_domain: true,
162 max_message_size: Some(256 * 1024 * 1024),
163 }
164 }
165}