Skip to main content

udp_request/request/request_builder/
impl.rs

1use super::*;
2
3/// Implements the `Default` trait for `RequestBuilder`.
4impl Default for RequestBuilder {
5    /// Creates a default `RequestBuilder` instance.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - A new `RequestBuilder` with default values.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            udp_request: UdpRequest::default(),
14            builder: UdpRequest::default(),
15        }
16    }
17}
18
19/// Implementation of `RequestBuilder`.
20impl RequestBuilder {
21    /// Creates a new `RequestBuilder`.
22    ///
23    /// # Returns
24    ///
25    /// - `Self` - A new `RequestBuilder` instance.
26    #[inline(always)]
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Sets the host for the UDP request.
32    ///
33    /// # Arguments
34    ///
35    /// - `T` - The host address, which can be any type that implements `Into<String>`.
36    ///
37    /// # Returns
38    ///
39    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
40    pub fn host<T>(&mut self, host: T) -> &mut Self
41    where
42        T: Into<String>,
43    {
44        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
45            self.udp_request.config.write().map(|mut data| {
46                data.host = host.into();
47            });
48        self
49    }
50
51    /// Sets the port for the UDP request.
52    ///
53    /// # Arguments
54    ///
55    /// - `usize` - The port number.
56    ///
57    /// # Returns
58    ///
59    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
60    pub fn port(&mut self, port: usize) -> &mut Self {
61        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
62            self.udp_request.config.write().map(|mut data| {
63                data.port = port;
64            });
65        self
66    }
67
68    /// Sets the buffer size for the UDP request.
69    ///
70    /// # Arguments
71    ///
72    /// - `usize` - The buffer size in bytes.
73    ///
74    /// # Returns
75    ///
76    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
77    pub fn buffer(&mut self, buffer_size: usize) -> &mut Self {
78        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
79            self.udp_request.config.write().map(|mut data| {
80                data.buffer_size = buffer_size;
81            });
82        self
83    }
84
85    /// Sets the timeout for the UDP request.
86    ///
87    /// # Arguments
88    ///
89    /// - `u64` - The timeout in milliseconds.
90    ///
91    /// # Returns
92    ///
93    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
94    pub fn timeout(&mut self, timeout: u64) -> &mut Self {
95        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
96            self.udp_request.config.write().map(|mut data| {
97                data.timeout = timeout;
98            });
99        self
100    }
101
102    /// Builds the `UdpRequest` and returns a boxed trait object.
103    ///
104    /// # Returns
105    ///
106    /// - `BoxRequestTrait` - A boxed `RequestTrait` object that can be used to send the request.
107    pub fn build(&mut self) -> BoxRequestTrait {
108        self.builder = self.udp_request.clone();
109        self.udp_request = UdpRequest::default();
110        Box::new(self.builder.clone())
111    }
112}