summaryrefslogtreecommitdiff
path: root/crates/utils/tcp_connection
diff options
context:
space:
mode:
Diffstat (limited to 'crates/utils/tcp_connection')
-rw-r--r--crates/utils/tcp_connection/src/handle.rs3
-rw-r--r--crates/utils/tcp_connection/src/lib.rs2
-rw-r--r--crates/utils/tcp_connection/src/target.rs61
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs12
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/lib.rs2
-rw-r--r--crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs13
6 files changed, 46 insertions, 47 deletions
diff --git a/crates/utils/tcp_connection/src/handle.rs b/crates/utils/tcp_connection/src/handle.rs
index de7815d..ab3a5ff 100644
--- a/crates/utils/tcp_connection/src/handle.rs
+++ b/crates/utils/tcp_connection/src/handle.rs
@@ -1,12 +1,9 @@
use tokio::net::TcpStream;
pub trait ClientHandle<RequestServer> {
-
fn process(stream: TcpStream);
}
pub trait ServerHandle<RequestClient> {
-
fn process(stream: TcpStream);
}
-
diff --git a/crates/utils/tcp_connection/src/lib.rs b/crates/utils/tcp_connection/src/lib.rs
index 4c72735..e38fea1 100644
--- a/crates/utils/tcp_connection/src/lib.rs
+++ b/crates/utils/tcp_connection/src/lib.rs
@@ -1,4 +1,4 @@
#[allow(dead_code)]
pub mod target;
-pub mod handle; \ No newline at end of file
+pub mod handle;
diff --git a/crates/utils/tcp_connection/src/target.rs b/crates/utils/tcp_connection/src/target.rs
index 8fc14ef..84c9029 100644
--- a/crates/utils/tcp_connection/src/target.rs
+++ b/crates/utils/tcp_connection/src/target.rs
@@ -1,16 +1,17 @@
+use crate::handle::{ClientHandle, ServerHandle};
use std::fmt::{Display, Formatter};
use std::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr};
use std::str::FromStr;
use tokio::net::lookup_host;
-use crate::handle::{ClientHandle, ServerHandle};
const DEFAULT_PORT: u16 = 8080;
#[derive(Debug, Eq, PartialEq)]
pub struct TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Client Handle
client_handle: Option<Client>,
@@ -25,8 +26,10 @@ where Client: ClientHandle<Server>,
}
impl<Client, Server> Default for TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
fn default() -> Self {
Self {
client_handle: None,
@@ -38,23 +41,25 @@ where Client: ClientHandle<Server>,
}
impl<Client, Server> From<SocketAddr> for TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Convert SocketAddr to TcpServerTarget
fn from(value: SocketAddr) -> Self {
Self {
port: value.port(),
bind_addr: value.ip(),
- .. Self::default()
+ ..Self::default()
}
}
}
impl<Client, Server> From<TcpServerTarget<Client, Server>> for SocketAddr
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Convert TcpServerTarget to SocketAddr
fn from(val: TcpServerTarget<Client, Server>) -> Self {
SocketAddr::new(val.bind_addr, val.port)
@@ -62,23 +67,26 @@ where Client: ClientHandle<Server>,
}
impl<Client, Server> Display for TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.bind_addr, self.port)
}
}
impl<Client, Server> TcpServerTarget<Client, Server>
-where Client: ClientHandle<Server>,
- Server: ServerHandle<Client> {
-
+where
+ Client: ClientHandle<Server>,
+ Server: ServerHandle<Client>,
+{
/// Create target by address
pub fn from_addr(addr: impl Into<IpAddr>, port: impl Into<u16>) -> Self {
Self {
port: port.into(),
bind_addr: addr.into(),
- .. Self::default()
+ ..Self::default()
}
}
@@ -86,12 +94,8 @@ where Client: ClientHandle<Server>,
pub fn from_str<'a>(addr_str: impl Into<&'a str>) -> Result<Self, AddrParseError> {
let socket_addr = SocketAddr::from_str(addr_str.into());
match socket_addr {
- Ok(socket_addr) => {
- Ok(Self::from_addr(socket_addr.ip(), socket_addr.port()))
- }
- Err(err) => {
- Err(err)
- }
+ Ok(socket_addr) => Ok(Self::from_addr(socket_addr.ip(), socket_addr.port())),
+ Err(err) => Err(err),
}
}
@@ -117,7 +121,10 @@ async fn domain_to_addr<'a>(domain: impl Into<&'a str>) -> Result<SocketAddr, st
}
if let Ok(_v6_addr) = domain.parse::<std::net::Ipv6Addr>() {
- return Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), default_port));
+ return Ok(SocketAddr::new(
+ IpAddr::V4(Ipv4Addr::LOCALHOST),
+ default_port,
+ ));
}
let (host, port_str) = if let Some((host, port)) = domain.rsplit_once(':') {
@@ -138,4 +145,4 @@ async fn domain_to_addr<'a>(domain: impl Into<&'a str>) -> Result<SocketAddr, st
}
Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
-} \ No newline at end of file
+}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs b/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs
index 95eb5ea..cefeef0 100644
--- a/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs
+++ b/crates/utils/tcp_connection/tcp_connection_test/src/example_handle.rs
@@ -1,18 +1,14 @@
-use tokio::net::TcpStream;
use tcp_connection::handle::{ClientHandle, ServerHandle};
+use tokio::net::TcpStream;
pub(crate) struct ExampleClientHandle;
impl ClientHandle<ExampleServerHandle> for ExampleClientHandle {
- fn process(stream: TcpStream) {
-
- }
+ fn process(stream: TcpStream) {}
}
pub(crate) struct ExampleServerHandle;
impl ServerHandle<ExampleClientHandle> for ExampleServerHandle {
- fn process(stream: TcpStream) {
-
- }
-} \ No newline at end of file
+ fn process(stream: TcpStream) {}
+}
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs b/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
index 2b72a15..697e847 100644
--- a/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
+++ b/crates/utils/tcp_connection/tcp_connection_test/src/lib.rs
@@ -1,4 +1,4 @@
#[cfg(test)]
pub mod test_tcp_target_build;
-pub(crate) mod example_handle; \ No newline at end of file
+pub(crate) mod example_handle;
diff --git a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs
index 4ef91cb..b375671 100644
--- a/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs
+++ b/crates/utils/tcp_connection/tcp_connection_test/src/test_tcp_target_build.rs
@@ -1,14 +1,13 @@
-use tcp_connection::target::TcpServerTarget;
use crate::example_handle::{ExampleClientHandle, ExampleServerHandle};
+use tcp_connection::target::TcpServerTarget;
#[test]
fn test_tcp_test_target_build() {
-
let host = "127.0.0.1:8080";
// Test build target by string
- let Ok(target) =
- TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_str(host) else {
+ let Ok(target) = TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_str(host)
+ else {
panic!("Test target built from a target addr `{}`", host);
};
assert_eq!(target.to_string(), "127.0.0.1:8080");
@@ -16,15 +15,15 @@ fn test_tcp_test_target_build() {
#[tokio::test]
async fn test_tcp_test_target_build_domain() {
-
let host = "localhost";
// Test build target by DomainName and Connection
let Ok(target) =
- TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_domain(host).await else {
+ TcpServerTarget::<ExampleClientHandle, ExampleServerHandle>::from_domain(host).await
+ else {
panic!("Test target built from a domain named `{}`", host);
};
// Test into string
assert_eq!(target.to_string(), "127.0.0.1:8080");
-} \ No newline at end of file
+} default_port,