bothan_bitfinex/worker/opts.rs
1//! Configuration options for initializing a `BitfinexWorker`.
2//!
3//! This module provides the [`WorkerOpts`] used to configure a `BitfinexWorker`.
4//! It allows setting the REST API URL and the update interval for polling,
5//! which are used by [`Worker`](`crate::worker::Worker`) to interact with the Bitfinex REST API.
6//!
7//! The module provides:
8//! - The [`WorkerOpts`] for specifying worker parameters
9//! - Serialization and deserialization support for configuration files
10//! - Defaults for REST API URL and update interval
11//! - Internal helpers for handling empty or missing configuration values
12//!
13use std::time::Duration;
14
15use serde::{Deserialize, Serialize};
16
17use crate::api::rest::DEFAULT_URL;
18
19const DEFAULT_UPDATE_INTERVAL: Duration = Duration::from_secs(60);
20
21/// Options for configuring the `BitfinexWorker`.
22///
23/// [`WorkerOpts`] provides a way to specify custom values for creating a
24/// `BitfinexWorker`. It specifies parameters such as the REST API URL and the update interval
25/// for polling, which are used to interact with the Bitfinex REST API.
26///
27/// # Examples
28///
29/// ```rust
30/// use bothan_bitfinex::worker::opts::WorkerOpts;
31/// use std::time::Duration;
32///
33/// let opts = WorkerOpts {
34/// url: "https://api-pub.bitfinex.com/v2/".to_string(),
35/// update_interval: Duration::from_secs(30),
36/// };
37///
38/// // Or use defaults
39/// let opts = WorkerOpts::default();
40/// ```
41#[derive(Clone, Debug, Serialize, Deserialize)]
42pub struct WorkerOpts {
43 /// The base URL for the worker's REST API connection. If not provided,
44 /// a default URL will be used.
45 #[serde(default = "default_url")]
46 pub url: String,
47
48 /// The interval between REST API polling requests for asset updates.
49 /// If not specified, a default value will be used.
50 #[serde(default = "default_update_interval")]
51 #[serde(with = "humantime_serde")]
52 pub update_interval: Duration,
53}
54
55/// This function returns the default REST API URL for the Bitfinex API.
56fn default_url() -> String {
57 DEFAULT_URL.to_string()
58}
59
60/// This function returns the default update interval for REST API polling.
61fn default_update_interval() -> Duration {
62 DEFAULT_UPDATE_INTERVAL
63}
64
65impl Default for WorkerOpts {
66 /// Creates a new `WorkerOpts` with default values.
67 ///
68 /// This method initializes the configuration with:
69 /// - Default Bitfinex REST API URL
70 /// - Default update interval for polling
71 ///
72 /// # Returns
73 ///
74 /// A [`WorkerOpts`] instance with default settings
75 ///
76 /// # Examples
77 ///
78 /// ```rust
79 /// use bothan_bitfinex::worker::opts::WorkerOpts;
80 ///
81 /// let opts = WorkerOpts::default();
82 /// assert_eq!(opts.url, "https://api-pub.bitfinex.com/v2/");
83 /// assert_eq!(opts.update_interval.as_secs(), 60);
84 /// ```
85 fn default() -> Self {
86 Self {
87 url: default_url(),
88 update_interval: default_update_interval(),
89 }
90 }
91}