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