tlq_client/config.rs
1use std::time::Duration;
2
3/// Configuration settings for TLQ client connections.
4///
5/// This struct contains all the configurable parameters for connecting to and
6/// interacting with a TLQ server, including network settings and retry behavior.
7///
8/// # Default Values
9///
10/// - `host`: "localhost"
11/// - `port`: 1337
12/// - `timeout`: 30 seconds
13/// - `max_retries`: 3
14/// - `retry_delay`: 100 milliseconds (base delay for exponential backoff)
15///
16/// # Examples
17///
18/// ```
19/// use tlq_client::{Config, ConfigBuilder};
20/// use std::time::Duration;
21///
22/// // Using defaults
23/// let default_config = Config::default();
24/// assert_eq!(default_config.host, "localhost");
25/// assert_eq!(default_config.port, 1337);
26///
27/// // Using builder pattern
28/// let custom_config = ConfigBuilder::new()
29/// .host("queue.example.com")
30/// .port(8080)
31/// .timeout(Duration::from_secs(60))
32/// .max_retries(5)
33/// .build();
34/// ```
35#[derive(Debug, Clone)]
36pub struct Config {
37 /// Hostname or IP address of the TLQ server
38 pub host: String,
39 /// Port number of the TLQ server
40 pub port: u16,
41 /// Maximum time to wait for a single request to complete
42 pub timeout: Duration,
43 /// Maximum number of retry attempts for failed operations
44 pub max_retries: u32,
45 /// Base delay between retry attempts (exponential backoff multiplier)
46 pub retry_delay: Duration,
47}
48
49impl Default for Config {
50 fn default() -> Self {
51 Self {
52 host: "localhost".to_string(),
53 port: 1337,
54 timeout: Duration::from_secs(30),
55 max_retries: 3,
56 retry_delay: Duration::from_millis(100),
57 }
58 }
59}
60
61/// Builder for creating [`Config`] instances with custom settings.
62///
63/// `ConfigBuilder` provides a fluent interface for constructing [`Config`] objects
64/// with specific settings. All methods return `self` to enable method chaining.
65/// Call [`build()`](Self::build) to create the final [`Config`] instance.
66///
67/// # Examples
68///
69/// ```
70/// use tlq_client::ConfigBuilder;
71/// use std::time::Duration;
72///
73/// let config = ConfigBuilder::new()
74/// .host("queue.example.com")
75/// .port(8080)
76/// .timeout_ms(5000) // 5 second timeout
77/// .max_retries(2) // Only retry twice
78/// .retry_delay_ms(250) // 250ms base delay
79/// .build();
80///
81/// assert_eq!(config.host, "queue.example.com");
82/// assert_eq!(config.port, 8080);
83/// assert_eq!(config.timeout, Duration::from_millis(5000));
84/// ```
85pub struct ConfigBuilder {
86 config: Config,
87}
88
89impl ConfigBuilder {
90 /// Creates a new `ConfigBuilder` with default settings.
91 ///
92 /// Equivalent to `Config::default()` but allows method chaining.
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// use tlq_client::ConfigBuilder;
98 ///
99 /// let builder = ConfigBuilder::new();
100 /// let config = builder.build();
101 /// ```
102 pub fn new() -> Self {
103 Self {
104 config: Config::default(),
105 }
106 }
107
108 /// Sets the TLQ server hostname or IP address.
109 ///
110 /// # Arguments
111 ///
112 /// * `host` - Any type that can be converted to `String`
113 ///
114 /// # Examples
115 ///
116 /// ```
117 /// use tlq_client::ConfigBuilder;
118 ///
119 /// let config = ConfigBuilder::new()
120 /// .host("queue.example.com")
121 /// .build();
122 /// assert_eq!(config.host, "queue.example.com");
123 /// ```
124 pub fn host(mut self, host: impl Into<String>) -> Self {
125 self.config.host = host.into();
126 self
127 }
128
129 /// Sets the TLQ server port number.
130 ///
131 /// # Arguments
132 ///
133 /// * `port` - Port number (1-65535)
134 ///
135 /// # Examples
136 ///
137 /// ```
138 /// use tlq_client::ConfigBuilder;
139 ///
140 /// let config = ConfigBuilder::new()
141 /// .port(8080)
142 /// .build();
143 /// assert_eq!(config.port, 8080);
144 /// ```
145 pub fn port(mut self, port: u16) -> Self {
146 self.config.port = port;
147 self
148 }
149
150 /// Sets the request timeout duration.
151 ///
152 /// # Arguments
153 ///
154 /// * `timeout` - Maximum time to wait for each request
155 ///
156 /// # Examples
157 ///
158 /// ```
159 /// use tlq_client::ConfigBuilder;
160 /// use std::time::Duration;
161 ///
162 /// let config = ConfigBuilder::new()
163 /// .timeout(Duration::from_secs(60))
164 /// .build();
165 /// assert_eq!(config.timeout, Duration::from_secs(60));
166 /// ```
167 pub fn timeout(mut self, timeout: Duration) -> Self {
168 self.config.timeout = timeout;
169 self
170 }
171
172 /// Sets the request timeout in milliseconds.
173 ///
174 /// Convenience method equivalent to `timeout(Duration::from_millis(ms))`.
175 ///
176 /// # Arguments
177 ///
178 /// * `ms` - Timeout in milliseconds
179 ///
180 /// # Examples
181 ///
182 /// ```
183 /// use tlq_client::ConfigBuilder;
184 /// use std::time::Duration;
185 ///
186 /// let config = ConfigBuilder::new()
187 /// .timeout_ms(5000) // 5 seconds
188 /// .build();
189 /// assert_eq!(config.timeout, Duration::from_millis(5000));
190 /// ```
191 pub fn timeout_ms(mut self, ms: u64) -> Self {
192 self.config.timeout = Duration::from_millis(ms);
193 self
194 }
195
196 /// Sets the maximum number of retry attempts.
197 ///
198 /// When a retryable error occurs, the client will retry the operation
199 /// up to this many times before giving up.
200 ///
201 /// # Arguments
202 ///
203 /// * `retries` - Maximum retry attempts (0 disables retries)
204 ///
205 /// # Examples
206 ///
207 /// ```
208 /// use tlq_client::ConfigBuilder;
209 ///
210 /// let config = ConfigBuilder::new()
211 /// .max_retries(5)
212 /// .build();
213 /// assert_eq!(config.max_retries, 5);
214 /// ```
215 pub fn max_retries(mut self, retries: u32) -> Self {
216 self.config.max_retries = retries;
217 self
218 }
219
220 /// Sets the base retry delay duration.
221 ///
222 /// The actual delay between retries uses exponential backoff:
223 /// delay = base_delay × 2^attempt_number
224 ///
225 /// # Arguments
226 ///
227 /// * `delay` - Base delay for exponential backoff
228 ///
229 /// # Examples
230 ///
231 /// ```
232 /// use tlq_client::ConfigBuilder;
233 /// use std::time::Duration;
234 ///
235 /// let config = ConfigBuilder::new()
236 /// .retry_delay(Duration::from_millis(500))
237 /// .build();
238 /// assert_eq!(config.retry_delay, Duration::from_millis(500));
239 /// ```
240 pub fn retry_delay(mut self, delay: Duration) -> Self {
241 self.config.retry_delay = delay;
242 self
243 }
244
245 /// Sets the base retry delay in milliseconds.
246 ///
247 /// Convenience method equivalent to `retry_delay(Duration::from_millis(ms))`.
248 ///
249 /// # Arguments
250 ///
251 /// * `ms` - Base delay in milliseconds
252 ///
253 /// # Examples
254 ///
255 /// ```
256 /// use tlq_client::ConfigBuilder;
257 /// use std::time::Duration;
258 ///
259 /// let config = ConfigBuilder::new()
260 /// .retry_delay_ms(200) // 200ms base delay
261 /// .build();
262 /// assert_eq!(config.retry_delay, Duration::from_millis(200));
263 /// ```
264 pub fn retry_delay_ms(mut self, ms: u64) -> Self {
265 self.config.retry_delay = Duration::from_millis(ms);
266 self
267 }
268
269 /// Builds and returns the final [`Config`] instance.
270 ///
271 /// Consumes the builder and returns a [`Config`] with all the
272 /// specified settings.
273 ///
274 /// # Examples
275 ///
276 /// ```
277 /// use tlq_client::ConfigBuilder;
278 ///
279 /// let config = ConfigBuilder::new()
280 /// .host("localhost")
281 /// .port(1337)
282 /// .max_retries(3)
283 /// .build();
284 /// // Use config...
285 /// ```
286 pub fn build(self) -> Config {
287 self.config
288 }
289}
290
291impl Default for ConfigBuilder {
292 fn default() -> Self {
293 Self::new()
294 }
295}