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.get_udp_request().get_config().write().map(
46                |mut data: RwLockWriteGuard<'_, Config>| {
47                    data.host = host.into();
48                },
49            );
50        self
51    }
52
53    /// Sets the port for the UDP request.
54    ///
55    /// # Arguments
56    ///
57    /// - `usize` - The port number.
58    ///
59    /// # Returns
60    ///
61    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
62    pub fn port(&mut self, port: usize) -> &mut Self {
63        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
64            self.get_udp_request().get_config().write().map(
65                |mut data: RwLockWriteGuard<'_, Config>| {
66                    data.port = port;
67                },
68            );
69        self
70    }
71
72    /// Sets the buffer size for the UDP request.
73    ///
74    /// # Arguments
75    ///
76    /// - `usize` - The buffer size in bytes.
77    ///
78    /// # Returns
79    ///
80    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
81    pub fn buffer(&mut self, buffer_size: usize) -> &mut Self {
82        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
83            self.get_udp_request().get_config().write().map(
84                |mut data: RwLockWriteGuard<'_, Config>| {
85                    data.buffer_size = buffer_size;
86                },
87            );
88        self
89    }
90
91    /// Sets the timeout for the UDP request.
92    ///
93    /// # Arguments
94    ///
95    /// - `u64` - The timeout in milliseconds.
96    ///
97    /// # Returns
98    ///
99    /// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
100    pub fn timeout(&mut self, timeout: u64) -> &mut Self {
101        let _: Result<(), PoisonError<RwLockWriteGuard<'_, Config>>> =
102            self.get_udp_request().get_config().write().map(
103                |mut data: RwLockWriteGuard<'_, Config>| {
104                    data.timeout = timeout;
105                },
106            );
107        self
108    }
109
110    /// Builds the `UdpRequest` and returns a boxed trait object.
111    ///
112    /// # Returns
113    ///
114    /// - `BoxRequestTrait` - A boxed `RequestTrait` object that can be used to send the request.
115    pub fn build(&mut self) -> BoxRequestTrait {
116        let udp_request: UdpRequest = self.get_udp_request().clone();
117        self.set_builder(udp_request);
118        self.set_udp_request(UdpRequest::default());
119        Box::new(self.get_builder().clone())
120    }
121}