Skip to content

Commit e70eb17

Browse files
committed
feat: v0.3.1
1 parent 80beb55 commit e70eb17

File tree

19 files changed

+191
-73
lines changed

19 files changed

+191
-73
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "udp-request"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2024"
55
authors = ["root@ltpp.vip"]
66
license = "MIT"

src/common/const.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1+
/// An empty string slice.
2+
///
3+
/// Used as a default value for various string fields.
14
pub const EMPTY_STR: &str = "";
5+
6+
/// The default port for web connections.
7+
///
8+
/// This is the standard port for HTTP.
29
pub const DEFAULT_WEB_PORT: usize = 80;
10+
11+
/// The default buffer size for I/O operations.
12+
///
13+
/// Set to 512 KB.
314
pub const DEFAULT_BUFFER_SIZE: usize = 512_000;
15+
16+
/// The default timeout value for network operations.
17+
///
18+
/// Set to the maximum value of `u64`.
419
pub const DEFAULT_TIMEOUT: u64 = u64::MAX;

src/common/type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
use crate::*;
22

3+
/// A type alias for an `Arc<RwLock<T>>`.
34
pub type ArcRwLock<T> = Arc<RwLock<T>>;

src/request/config/impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::*;
22

3-
/// Default implementation for Config.
3+
/// Implements the `Default` trait for the `Config` struct.
44
impl Default for Config {
5-
/// Creates a default Config instance.
5+
/// Creates a default `Config` instance.
66
///
77
/// # Returns
88
///
9-
/// - `Config` - Default configuration with empty host, default port, timeout and buffer size.
9+
/// - `Self` - A `Config` instance with default values: an empty host, default web port, maximum timeout, and default buffer size.
1010
fn default() -> Self {
1111
Self {
1212
timeout: DEFAULT_TIMEOUT,

src/request/config/struct.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use crate::*;
22

3-
/// Configuration for UDP request.
3+
/// Configuration for a UDP request.
44
///
5-
/// Contains network settings and timeout configurations.
5+
/// This structure holds all the necessary configuration for making a UDP request,
6+
/// including network settings and timeout values.
67
#[derive(Debug, Clone, PartialEq, Eq)]
78
pub struct Config {
8-
/// Remote host address.
9+
/// The remote host address to which the UDP request will be sent.
910
pub(crate) host: String,
10-
/// Remote port number.
11+
/// The remote port number to which the UDP request will be sent.
1112
pub(crate) port: usize,
12-
/// Request timeout in milliseconds.
13+
/// The request timeout in milliseconds.
1314
pub(crate) timeout: u64,
14-
/// Buffer size for receiving data.
15+
/// The buffer size for receiving data from the remote host.
1516
pub(crate) buffer_size: usize,
1617
}

src/request/error/enum.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
/// Error types for UDP requests.
1+
/// An enumeration of possible errors that can occur during a UDP request.
2+
///
3+
/// This enum covers a range of potential issues, from socket creation to network timeouts.
24
#[derive(Debug)]
35
pub enum RequestError {
4-
/// Invalid URL format.
6+
/// An error indicating that the provided URL is invalid.
57
InvalidUrl,
6-
/// Failed to create UDP socket.
8+
/// An error indicating that the UDP socket could not be created.
79
UdpSocketCreateError,
8-
/// Failed to connect UDP socket.
10+
/// An error indicating that the UDP socket could not connect to the specified address.
911
UdpSocketConnectError,
10-
/// General request error.
12+
/// A general request error, used for unspecified issues.
1113
RequestError,
12-
/// Failed to read from connection.
14+
/// An error indicating that reading from the connection failed.
1315
ReadConnectionError,
14-
/// Failed to set read timeout.
16+
/// An error indicating that setting the read timeout for the socket failed.
1517
SetReadTimeoutError,
16-
/// Failed to set write timeout.
18+
/// An error indicating that setting the write timeout for the socket failed.
1719
SetWriteTimeoutError,
18-
/// Failed to read response.
20+
/// An error indicating that reading the response from the socket failed.
1921
ReadResponseError,
20-
/// Failed to send response with error message.
22+
/// An error indicating that sending the request failed, with a descriptive message.
2123
SendResponseError(String),
2224
}

src/request/error/impl.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
use crate::*;
22

3+
/// Implements the `StdError` trait for `RequestError`.
34
impl StdError for RequestError {}
45

6+
/// Implements the `Display` trait for `RequestError`.
57
impl Display for RequestError {
8+
/// Formats the `RequestError` for display.
9+
///
10+
/// # Arguments
11+
///
12+
/// - `&mut fmt::Formatter<'_>` - The formatter to write the output to.
13+
///
14+
/// # Returns
15+
///
16+
/// - `fmt::Result` - The result of the formatting operation.
617
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
718
match self {
819
Self::InvalidUrl => write!(f, "Invalid url"),

src/request/request/impl.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::*;
22

3-
/// Default implementation for UdpRequest.
3+
/// Implements the `Default` trait for `UdpRequest`.
44
impl Default for UdpRequest {
5-
/// Creates a default UdpRequest instance.
5+
/// Creates a default `UdpRequest` instance.
66
///
77
/// # Returns
88
///
9-
/// - `UdpRequest` - Default request with default config and empty response.
9+
/// - `Self` - A new `UdpRequest` with default configuration and an empty response.
1010
fn default() -> Self {
1111
Self {
1212
config: Arc::new(RwLock::new(Config::default())),
@@ -15,17 +15,18 @@ impl Default for UdpRequest {
1515
}
1616
}
1717

18+
/// Implementation of `UdpRequest`.
1819
impl UdpRequest {
19-
/// Sends UDP request data through socket.
20+
/// Sends a UDP request and reads the response.
2021
///
2122
/// # Arguments
2223
///
23-
/// - `&mut UdpSocket` - UDP socket for communication.
24-
/// - `&[u8]` - Data to send.
24+
/// - `&mut UdpSocket` - The UDP socket to use for sending and receiving data.
25+
/// - `&[u8]` - The data to send in the request.
2526
///
2627
/// # Returns
2728
///
28-
/// - `Result<BoxResponseTrait, RequestError>` - Response or error.
29+
/// - `Result<BoxResponseTrait, RequestError>` - A `Result` containing the response as a boxed trait object or a `RequestError`.
2930
fn send_request(
3031
&mut self,
3132
socket: &mut UdpSocket,
@@ -37,15 +38,15 @@ impl UdpRequest {
3738
self.read_response(socket)
3839
}
3940

40-
/// Reads response from UDP socket.
41+
/// Reads the response from the UDP socket.
4142
///
4243
/// # Arguments
4344
///
44-
/// - `&mut UdpSocket` - UDP socket for communication.
45+
/// - `&mut UdpSocket` - The UDP socket to read the response from.
4546
///
4647
/// # Returns
4748
///
48-
/// - `Result<BoxResponseTrait, RequestError>` - Response or error.
49+
/// - `Result<BoxResponseTrait, RequestError>` - A `Result` containing the response as a boxed trait object or a `RequestError`.
4950
fn read_response(&mut self, socket: &mut UdpSocket) -> Result<BoxResponseTrait, RequestError> {
5051
let cfg_buffer_size: usize = self
5152
.config
@@ -64,16 +65,16 @@ impl UdpRequest {
6465
));
6566
}
6667

67-
/// Creates and configures UDP socket for connection.
68+
/// Creates and configures a UDP socket for the connection.
6869
///
6970
/// # Arguments
7071
///
71-
/// - `String` - Host address.
72-
/// - `usize` - Port number.
72+
/// - `String` - The host address to connect to.
73+
/// - `usize` - The port number to connect to.
7374
///
7475
/// # Returns
7576
///
76-
/// - `Result<UdpSocket, RequestError>` - Configured socket or error.
77+
/// - `Result<UdpSocket, RequestError>` - A `Result` containing the configured `UdpSocket` or a `RequestError`.
7778
fn get_connection_socket(&self, host: String, port: usize) -> Result<UdpSocket, RequestError> {
7879
let host_port: String = format!("{}:{}", host.clone(), port);
7980
let cfg_timeout: u64 = self
@@ -97,19 +98,19 @@ impl UdpRequest {
9798
}
9899
}
99100

100-
/// RequestTrait implementation for UdpRequest.
101+
/// Implements the `RequestTrait` for `UdpRequest`.
101102
impl RequestTrait for UdpRequest {
102103
type RequestResult = RequestResult;
103104

104-
/// Sends UDP request with given data.
105+
/// Sends a UDP request with the given data.
105106
///
106107
/// # Arguments
107108
///
108-
/// - `&[u8]` - Data to send.
109+
/// - `&[u8]` - The data to send in the request.
109110
///
110111
/// # Returns
111112
///
112-
/// - `RequestResult` - Response or error.
113+
/// - `Self::RequestResult` - The result of the request, containing either the response or an error.
113114
fn send(&mut self, data: &[u8]) -> Self::RequestResult {
114115
let cfg_timeout: Config = self
115116
.config

src/request/request/struct.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use crate::*;
22

3+
/// Represents a UDP request.
4+
///
5+
/// This struct holds the configuration and response for a UDP request.
36
#[derive(Debug, Clone)]
47
pub struct UdpRequest {
8+
/// The configuration for the UDP request, wrapped in an `Arc<RwLock<>>`.
59
pub(crate) config: ArcRwLock<Config>,
10+
/// The response of the UDP request, wrapped in an `Arc<RwLock<>>`.
611
pub(crate) response: ArcRwLock<UdpResponseBinary>,
712
}

src/request/request_builder/impl.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use crate::*;
22

3+
/// Implements the `Default` trait for `RequestBuilder`.
34
impl Default for RequestBuilder {
5+
/// Creates a default `RequestBuilder` instance.
6+
///
7+
/// # Returns
8+
///
9+
/// - `Self` - A new `RequestBuilder` with default values.
410
fn default() -> Self {
511
Self {
612
udp_request: UdpRequest::default(),
@@ -9,11 +15,26 @@ impl Default for RequestBuilder {
915
}
1016
}
1117

18+
/// Implementation of `RequestBuilder`.
1219
impl RequestBuilder {
20+
/// Creates a new `RequestBuilder`.
21+
///
22+
/// # Returns
23+
///
24+
/// - `Self` - A new `RequestBuilder` instance.
1325
pub fn new() -> Self {
1426
Self::default()
1527
}
1628

29+
/// Sets the host for the UDP request.
30+
///
31+
/// # Arguments
32+
///
33+
/// - `T` - The host address, which can be any type that implements `Into<String>`.
34+
///
35+
/// # Returns
36+
///
37+
/// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
1738
pub fn host<T>(&mut self, host: T) -> &mut Self
1839
where
1940
T: Into<String>,
@@ -25,6 +46,15 @@ impl RequestBuilder {
2546
self
2647
}
2748

49+
/// Sets the port for the UDP request.
50+
///
51+
/// # Arguments
52+
///
53+
/// - `usize` - The port number.
54+
///
55+
/// # Returns
56+
///
57+
/// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
2858
pub fn port(&mut self, port: usize) -> &mut Self {
2959
let _ = self.udp_request.config.write().and_then(|mut data| {
3060
data.port = port;
@@ -33,6 +63,15 @@ impl RequestBuilder {
3363
self
3464
}
3565

66+
/// Sets the buffer size for the UDP request.
67+
///
68+
/// # Arguments
69+
///
70+
/// - `usize` - The buffer size in bytes.
71+
///
72+
/// # Returns
73+
///
74+
/// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
3675
pub fn buffer(&mut self, buffer_size: usize) -> &mut Self {
3776
let _ = self.udp_request.config.write().and_then(|mut data| {
3877
data.buffer_size = buffer_size;
@@ -41,6 +80,15 @@ impl RequestBuilder {
4180
self
4281
}
4382

83+
/// Sets the timeout for the UDP request.
84+
///
85+
/// # Arguments
86+
///
87+
/// - `u64` - The timeout in milliseconds.
88+
///
89+
/// # Returns
90+
///
91+
/// - `&mut Self` - A mutable reference to the `RequestBuilder` for method chaining.
4492
pub fn timeout(&mut self, timeout: u64) -> &mut Self {
4593
let _ = self.udp_request.config.write().and_then(|mut data| {
4694
data.timeout = timeout;
@@ -49,6 +97,11 @@ impl RequestBuilder {
4997
self
5098
}
5199

100+
/// Builds the `UdpRequest` and returns a boxed trait object.
101+
///
102+
/// # Returns
103+
///
104+
/// - `BoxRequestTrait` - A boxed `RequestTrait` object that can be used to send the request.
52105
pub fn build(&mut self) -> BoxRequestTrait {
53106
self.builder = self.udp_request.clone();
54107
self.udp_request = UdpRequest::default();

0 commit comments

Comments
 (0)