11use crate :: * ;
22
3+ /// Manages the internal state of the context.
34impl InnerContext {
5+ /// Creates a new `InnerContext` with default values.
6+ ///
7+ /// # Returns
8+ ///
9+ /// - `InnerContext` - A new instance of `InnerContext`.
410 pub fn new ( ) -> Self {
511 InnerContext {
612 stream : None ,
@@ -11,40 +17,123 @@ impl InnerContext {
1117 }
1218}
1319
20+ /// Provides thread-safe access and manipulation of the context.
1421impl Context {
22+ /// Creates a `Context` from an `InnerContext`.
23+ ///
24+ /// # Arguments
25+ ///
26+ /// - `InnerContext` - The inner context to wrap.
27+ ///
28+ /// # Returns
29+ ///
30+ /// - `Self` - A new `Context` instance.
1531 pub ( crate ) fn from_inner_context ( ctx : InnerContext ) -> Self {
1632 Self ( Arc :: new ( RwLock :: new ( ctx) ) )
1733 }
1834
35+ /// Gets a read lock for the inner context.
36+ ///
37+ /// # Arguments
38+ ///
39+ /// - `&self` - A reference to the `Context`.
40+ ///
41+ /// # Returns
42+ ///
43+ /// - `RwLockReadContext` - A read guard for the inner context.
1944 pub async fn get_read_lock ( & self ) -> RwLockReadContext {
2045 self . 0 . read ( ) . await
2146 }
2247
48+ /// Gets a write lock for the inner context.
49+ ///
50+ /// # Arguments
51+ ///
52+ /// - `&self` - A reference to the `Context`.
53+ ///
54+ /// # Returns
55+ ///
56+ /// - `RwLockWriteContext` - A write guard for the inner context.
2357 pub async fn get_write_lock ( & self ) -> RwLockWriteContext {
2458 self . 0 . write ( ) . await
2559 }
2660
61+ /// Gets a clone of the inner context.
62+ ///
63+ /// # Arguments
64+ ///
65+ /// - `&self` - A reference to the `Context`.
66+ ///
67+ /// # Returns
68+ ///
69+ /// - `InnerContext` - A cloned inner context.
2770 pub async fn get ( & self ) -> InnerContext {
2871 self . get_read_lock ( ) . await . clone ( )
2972 }
3073
74+ /// Gets the stream from the inner context.
75+ ///
76+ /// # Arguments
77+ ///
78+ /// - `&self` - A reference to the `Context`.
79+ ///
80+ /// # Returns
81+ ///
82+ /// - `OptionArcRwLockStream` - The optional stream.
3183 pub async fn get_stream ( & self ) -> OptionArcRwLockStream {
3284 self . get ( ) . await . stream . clone ( )
3385 }
3486
87+ /// Gets the request from the inner context.
88+ ///
89+ /// # Arguments
90+ ///
91+ /// - `&self` - A reference to the `Context`.
92+ ///
93+ /// # Returns
94+ ///
95+ /// - `Request` - The request object.
3596 pub async fn get_request ( & self ) -> Request {
3697 self . get ( ) . await . request . clone ( )
3798 }
3899
100+ /// Gets the response from the inner context.
101+ ///
102+ /// # Arguments
103+ ///
104+ /// - `&self` - A reference to the `Context`.
105+ ///
106+ /// # Returns
107+ ///
108+ /// - `Response` - The response object.
39109 pub async fn get_response ( & self ) -> Response {
40110 self . get ( ) . await . response . clone ( )
41111 }
42112
113+ /// Sets response data.
114+ ///
115+ /// # Arguments
116+ ///
117+ /// - `&self` - A reference to the `Context`.
118+ /// - `data` - The data to set, which can be converted into `ResponseData`.
119+ ///
120+ /// # Returns
121+ ///
122+ /// - `&Self` - A reference to the `Context`.
43123 pub ( super ) async fn set_response_data < T : Into < ResponseData > > ( & self , data : T ) -> & Self {
44124 self . get_write_lock ( ) . await . response . set_response_data ( data) ;
45125 self
46126 }
47127
128+ /// Gets the socket address from the stream.
129+ ///
130+ /// # Arguments
131+ ///
132+ /// - `&self` - A reference to the `Context`.
133+ ///
134+ /// # Returns
135+ ///
136+ /// - `OptionSocketAddr` - The optional socket address.
48137 pub async fn get_socket_addr ( & self ) -> OptionSocketAddr {
49138 let stream_result: OptionArcRwLockStream = self . get_stream ( ) . await ;
50139 if stream_result. is_none ( ) {
@@ -59,6 +148,15 @@ impl Context {
59148 socket_addr_opt
60149 }
61150
151+ /// Gets the socket address or a default if none is available.
152+ ///
153+ /// # Arguments
154+ ///
155+ /// - `&self` - A reference to the `Context`.
156+ ///
157+ /// # Returns
158+ ///
159+ /// - `SocketAddr` - The socket address or the default socket address.
62160 pub async fn get_socket_addr_or_default ( & self ) -> SocketAddr {
63161 let stream_result: OptionArcRwLockStream = self . get_stream ( ) . await ;
64162 if stream_result. is_none ( ) {
@@ -73,26 +171,72 @@ impl Context {
73171 socket_addr
74172 }
75173
174+ /// Gets the socket address as a string.
175+ ///
176+ /// # Arguments
177+ ///
178+ /// - `&self` - A reference to the `Context`.
179+ ///
180+ /// # Returns
181+ ///
182+ /// - `Option<String>` - The socket address as a string, if available.
76183 pub async fn get_socket_addr_string ( & self ) -> Option < String > {
77184 self . get_socket_addr ( ) . await . map ( |data| data. to_string ( ) )
78185 }
79186
187+ /// Gets the socket address as a string, or a default string if none is available.
188+ ///
189+ /// # Arguments
190+ ///
191+ /// - `&self` - A reference to the `Context`.
192+ ///
193+ /// # Returns
194+ ///
195+ /// - `String` - The socket address as a string, or the default socket address string.
80196 pub async fn get_socket_addr_or_default_string ( & self ) -> String {
81197 self . get_socket_addr_or_default ( ) . await . to_string ( )
82198 }
83199
200+ /// Gets the socket host from the socket address.
201+ ///
202+ /// # Arguments
203+ ///
204+ /// - `&self` - A reference to the `Context`.
205+ ///
206+ /// # Returns
207+ ///
208+ /// - `OptionSocketHost` - The optional socket host.
84209 pub async fn get_socket_host ( & self ) -> OptionSocketHost {
85210 self . get_socket_addr ( )
86211 . await
87212 . map ( |socket_addr : SocketAddr | socket_addr. ip ( ) )
88213 }
89214
215+ /// Gets the socket port from the socket address.
216+ ///
217+ /// # Arguments
218+ ///
219+ /// - `&self` - A reference to the `Context`.
220+ ///
221+ /// # Returns
222+ ///
223+ /// - `OptionSocketPort` - The optional socket port.
90224 pub async fn get_socket_port ( & self ) -> OptionSocketPort {
91225 self . get_socket_addr ( )
92226 . await
93227 . map ( |socket_addr : SocketAddr | socket_addr. port ( ) )
94228 }
95229
230+ /// Sends data through the stream.
231+ ///
232+ /// # Arguments
233+ ///
234+ /// - `&self` - A reference to the `Context`.
235+ /// - `data` - The data to send, which can be converted into `ResponseData`.
236+ ///
237+ /// # Returns
238+ ///
239+ /// - `ResponseResult` - Ok(()) on success, or an error if the stream is not found or sending fails.
96240 pub async fn send < T : Into < ResponseData > > ( & self , data : T ) -> ResponseResult {
97241 if let Some ( stream) = self . get_stream ( ) . await {
98242 self . set_response_data ( data)
@@ -106,6 +250,15 @@ impl Context {
106250 Err ( ResponseError :: NotFoundStream )
107251 }
108252
253+ /// Closes the stream.
254+ ///
255+ /// # Arguments
256+ ///
257+ /// - `&self` - A reference to the `Context`.
258+ ///
259+ /// # Returns
260+ ///
261+ /// - `ResponseResult` - Ok(()) on success, or an error if the stream is not found or closing fails.
109262 pub async fn close ( & self ) -> ResponseResult {
110263 if let Some ( stream) = self . get_stream ( ) . await {
111264 self . get_response ( ) . await . close ( & stream) . await ?;
@@ -114,6 +267,15 @@ impl Context {
114267 Err ( ResponseError :: NotFoundStream )
115268 }
116269
270+ /// Flushes the stream.
271+ ///
272+ /// # Arguments
273+ ///
274+ /// - `&self` - A reference to the `Context`.
275+ ///
276+ /// # Returns
277+ ///
278+ /// - `ResponseResult` - Ok(()) on success, or an error if the stream is not found or flushing fails.
117279 pub async fn flush ( & self ) -> ResponseResult {
118280 if let Some ( stream) = self . get_stream ( ) . await {
119281 self . get_response ( ) . await . flush ( & stream) . await ?;
@@ -122,6 +284,17 @@ impl Context {
122284 Err ( ResponseError :: NotFoundStream )
123285 }
124286
287+ /// Sets a data value in the context's data map.
288+ ///
289+ /// # Arguments
290+ ///
291+ /// - `&self` - A reference to the `Context`.
292+ /// - `key` - The key for the data.
293+ /// - `value` - The value to set, which must be cloneable and thread-safe.
294+ ///
295+ /// # Returns
296+ ///
297+ /// - `&Self` - A reference to the `Context`.
125298 pub async fn set_data_value < T : Any + Send + Sync + Clone > (
126299 & self ,
127300 key : & str ,
@@ -134,6 +307,16 @@ impl Context {
134307 self
135308 }
136309
310+ /// Gets a data value from the context's data map.
311+ ///
312+ /// # Arguments
313+ ///
314+ /// - `&self` - A reference to the `Context`.
315+ /// - `key` - The key for the data.
316+ ///
317+ /// # Returns
318+ ///
319+ /// - `Option<T>` - The data value if found and successfully downcasted, otherwise `None`.
137320 pub async fn get_data_value < T : Any + Send + Sync + Clone > ( & self , key : & str ) -> Option < T > {
138321 self . get_read_lock ( )
139322 . await
@@ -143,11 +326,30 @@ impl Context {
143326 . cloned ( )
144327 }
145328
329+ /// Removes a data value from the context's data map.
330+ ///
331+ /// # Arguments
332+ ///
333+ /// - `&self` - A reference to the `Context`.
334+ /// - `key` - The key of the data to remove.
335+ ///
336+ /// # Returns
337+ ///
338+ /// - `&Self` - A reference to the `Context`.
146339 pub async fn remove_data_value ( & self , key : & str ) -> & Self {
147340 self . get_write_lock ( ) . await . data . remove ( key) ;
148341 self
149342 }
150343
344+ /// Clears all data from the context's data map.
345+ ///
346+ /// # Arguments
347+ ///
348+ /// - `&self` - A reference to the `Context`.
349+ ///
350+ /// # Returns
351+ ///
352+ /// - `&Self` - A reference to the `Context`.
151353 pub async fn clear_data ( & self ) -> & Self {
152354 self . get_write_lock ( ) . await . data . clear ( ) ;
153355 self
0 commit comments