hyperlane/config/
impl.rs

1use crate::*;
2
3/// Implements the `Default` trait for `ServerConfigInner`.
4///
5/// This provides a default configuration for the server with predefined values.
6impl Default for ServerConfigInner {
7    /// Creates a default `ServerConfigInner`.
8    ///
9    /// # Returns
10    ///
11    /// - `Self` - A `ServerConfigInner` instance with default settings.
12    #[inline(always)]
13    fn default() -> Self {
14        Self {
15            host: DEFAULT_HOST.to_owned(),
16            port: DEFAULT_WEB_PORT,
17            buffer: DEFAULT_BUFFER_SIZE,
18            nodelay: DEFAULT_NODELAY,
19            linger: DEFAULT_LINGER,
20            ttl: DEFAULT_TTI,
21        }
22    }
23}
24
25/// Implements the `Default` trait for `ServerConfig`.
26///
27/// This wraps the default `ServerConfigInner` in an `Arc<RwLock>`.
28impl Default for ServerConfig {
29    /// Creates a default `ServerConfig`.
30    ///
31    /// # Returns
32    ///
33    /// - `Self` - A `ServerConfig` instance with default settings.
34    #[inline(always)]
35    fn default() -> Self {
36        Self(arc_rwlock(ServerConfigInner::default()))
37    }
38}
39
40/// Implements the `PartialEq` trait for `ServerConfig`.
41///
42/// This allows for comparing two `ServerConfig` instances for equality.
43impl PartialEq for ServerConfig {
44    /// Checks if two `ServerConfig` instances are equal.
45    ///
46    /// It first checks for pointer equality for performance. If the pointers are not equal,
47    /// it compares the inner `ServerConfigInner` values.
48    ///
49    /// # Arguments
50    ///
51    /// - `&Self`- The other `ServerConfig` to compare against.
52    ///
53    /// # Returns
54    ///
55    /// - `bool` - Indicating whether the configurations are equal.
56    #[inline(always)]
57    fn eq(&self, other: &Self) -> bool {
58        if Arc::ptr_eq(self.get_0(), other.get_0()) {
59            return true;
60        }
61        if let (Ok(s), Ok(o)) = (self.get_0().try_read(), other.get_0().try_read()) {
62            *s == *o
63        } else {
64            false
65        }
66    }
67}
68
69/// Implements the `Eq` trait for `ServerConfig`.
70///
71/// This indicates that `ServerConfig` has a total equality relation.
72impl Eq for ServerConfig {}
73
74/// Implementation block for `ServerConfig`.
75impl ServerConfig {
76    /// Creates a new `ServerConfig` with default values.
77    ///
78    /// # Returns
79    ///
80    /// - `Self` - A new `ServerConfig` instance.
81    pub async fn new() -> Self {
82        Self::default()
83    }
84
85    /// Acquires a read lock on the server configuration.
86    ///
87    /// # Returns
88    ///
89    /// - `ConfigReadGuard` - A `ConfigReadGuard` for the inner configuration.
90    async fn read(&self) -> ConfigReadGuard<'_> {
91        self.get_0().read().await
92    }
93
94    /// Acquires a write lock on the server configuration.
95    ///
96    /// # Returns
97    ///
98    /// - `ConfigWriteGuard` - A `ConfigWriteGuard` for the inner configuration.
99    async fn write(&self) -> ConfigWriteGuard<'_> {
100        self.get_0().write().await
101    }
102
103    /// Retrieves a clone of the inner server configuration.
104    ///
105    /// This function provides a snapshot of the current configuration by acquiring a read lock
106    /// and cloning the inner `ServerConfigInner`.
107    ///
108    /// # Returns
109    ///
110    /// - `ServerConfigInner` - A `ServerConfigInner` instance containing the current server configuration.
111    pub(crate) async fn get_inner(&self) -> ServerConfigInner {
112        self.read().await.clone()
113    }
114
115    /// Sets the host address for the server.
116    ///
117    /// # Arguments
118    ///
119    /// - `H`- The host address to set.
120    ///
121    /// # Returns
122    ///
123    /// - `&Self` - A reference to `Self` for method chaining.
124    pub async fn host<H: ToString>(&self, host: H) -> &Self {
125        self.write().await.set_host(host.to_string());
126        self
127    }
128
129    /// Sets the port for the server.
130    ///
131    /// # Arguments
132    ///
133    /// - `usize`- The port number to set.
134    ///
135    /// # Returns
136    ///
137    /// - `&Self` - A reference to `Self` for method chaining.
138    pub async fn port(&self, port: usize) -> &Self {
139        self.write().await.set_port(port);
140        self
141    }
142
143    /// Sets the HTTP buffer size.
144    ///
145    /// # Arguments
146    ///
147    /// - `usize`- The HTTP buffer size to set.
148    ///
149    /// # Returns
150    ///
151    /// - `&Self` - A reference to `Self` for method chaining.
152    pub async fn buffer(&self, buffer: usize) -> &Self {
153        self.write().await.set_buffer(buffer);
154        self
155    }
156
157    /// Sets the `TCP_NODELAY` option.
158    ///
159    /// # Arguments
160    ///
161    /// - `bool`- The `bool` value for `TCP_NODELAY`.
162    ///
163    /// # Returns
164    ///
165    /// - `&Self` - A reference to `Self` for method chaining.
166    pub async fn nodelay(&self, nodelay: bool) -> &Self {
167        self.write().await.set_nodelay(Some(nodelay));
168        self
169    }
170
171    /// Enables the `TCP_NODELAY` option.
172    ///
173    /// # Returns
174    ///
175    /// - `&Self` - A reference to `Self` for method chaining.
176    pub async fn enable_nodelay(&self) -> &Self {
177        self.nodelay(true).await
178    }
179
180    /// Disables the `TCP_NODELAY` option.
181    ///
182    /// # Returns
183    ///
184    /// - `&Self` - A reference to `Self` for method chaining.
185    pub async fn disable_nodelay(&self) -> &Self {
186        self.nodelay(false).await
187    }
188
189    /// Sets the `SO_LINGER` option.
190    ///
191    /// # Arguments
192    ///
193    /// - `OptionDuration`- The `Duration` value for `SO_LINGER`.
194    ///
195    /// # Returns
196    ///
197    /// - `&Self` - A reference to `Self` for method chaining.
198    pub async fn linger(&self, linger_opt: OptionDuration) -> &Self {
199        self.write().await.set_linger(linger_opt);
200        self
201    }
202
203    /// Enables the `SO_LINGER` option.
204    ///
205    /// # Arguments
206    ///
207    /// - `Duration`- The `Duration` value for `SO_LINGER`.
208    ///
209    /// # Returns
210    ///
211    /// - `&Self` - A reference to `Self` for method chaining.
212    pub async fn enable_linger(&self, linger: Duration) -> &Self {
213        self.linger(Some(linger)).await;
214        self
215    }
216
217    /// Disables the `SO_LINGER` option.
218    ///
219    /// # Returns
220    ///
221    /// - `&Self` - A reference to `Self` for method chaining.
222    pub async fn disable_linger(&self) -> &Self {
223        self.linger(None).await;
224        self
225    }
226
227    /// Sets the `IP_TTL` option.
228    ///
229    /// # Arguments
230    ///
231    /// - `u32`- The `u32` value for `IP_TTL`.
232    ///
233    /// # Returns
234    ///
235    /// - `&Self` - A reference to `Self` for method chaining.
236    pub async fn ttl(&self, ttl: u32) -> &Self {
237        self.write().await.set_ttl(Some(ttl));
238        self
239    }
240
241    /// Creates a `ServerConfig` from a JSON string.
242    ///
243    /// # Arguments
244    ///
245    /// - `&str`- The JSON string to parse.
246    ///
247    /// # Returns
248    ///
249    /// - `ConfigLoadResult` - A `ConfigLoadResult` which is a `Result` containing either the `ServerConfig` or a `serde_json::Error`.
250    ///   Creates a `ServerConfig` from a JSON string.
251    ///
252    /// # Arguments
253    ///
254    /// - `config_str` - The JSON string to parse.
255    ///
256    /// # Returns
257    ///
258    /// - `ConfigLoadResult` - A `ConfigLoadResult` which is a `Result` containing either the `ServerConfig` or a `serde_json::Error`.
259    pub fn from_json_str(config_str: &str) -> ConfigLoadResult {
260        serde_json::from_str(config_str).map(|config: ServerConfigInner| Self(arc_rwlock(config)))
261    }
262}