From 0ad594277e61e9fb41b2e470c34cff7534d6c780 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 14 Sep 2025 13:12:27 +0800 Subject: Fixed codes by Zed --- crates/utils/cfg_file/cfg_file_derive/src/lib.rs | 18 +++---- crates/utils/cfg_file/cfg_file_test/Cargo.toml | 2 +- crates/utils/cfg_file/cfg_file_test/src/lib.rs | 23 ++++---- crates/utils/cfg_file/src/config.rs | 31 ++++++----- crates/utils/cfg_file/src/lib.rs | 2 +- crates/utils/member_verify/src/verifier.rs | 1 + crates/utils/tcp_connection/src/handle.rs | 3 -- crates/utils/tcp_connection/src/lib.rs | 2 +- crates/utils/tcp_connection/src/target.rs | 61 ++++++++++++---------- .../tcp_connection_test/src/example_handle.rs | 12 ++--- .../tcp_connection/tcp_connection_test/src/lib.rs | 2 +- .../src/test_tcp_target_build.rs | 13 +++-- 12 files changed, 88 insertions(+), 82 deletions(-) (limited to 'crates/utils') diff --git a/crates/utils/cfg_file/cfg_file_derive/src/lib.rs b/crates/utils/cfg_file/cfg_file_derive/src/lib.rs index c43fd59..66a6d6f 100644 --- a/crates/utils/cfg_file/cfg_file_derive/src/lib.rs +++ b/crates/utils/cfg_file/cfg_file_derive/src/lib.rs @@ -3,11 +3,7 @@ extern crate proc_macro; use proc_macro::TokenStream; use quote::quote; use syn::parse::ParseStream; -use syn::{ - parse_macro_input, - DeriveInput, - Attribute -}; +use syn::{Attribute, DeriveInput, parse_macro_input}; #[proc_macro_derive(ConfigFile, attributes(cfg_file))] pub fn derive_config_file(input: TokenStream) -> TokenStream { @@ -55,9 +51,13 @@ fn find_cfg_file_path(attrs: &[Attribute]) -> Option { let parser = |meta: ParseStream| { let path_meta: syn::MetaNameValue = meta.parse()?; if path_meta.path.is_ident("path") - && let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(lit), .. }) = path_meta.value { - return Ok(lit.value()); - } + && let syn::Expr::Lit(syn::ExprLit { + lit: syn::Lit::Str(lit), + .. + }) = path_meta.value + { + return Ok(lit.value()); + } Err(meta.error("expected `path = \"...\"`")) }; @@ -82,4 +82,4 @@ fn to_snake_case(s: &str) -> String { } } snake -} \ No newline at end of file +} diff --git a/crates/utils/cfg_file/cfg_file_test/Cargo.toml b/crates/utils/cfg_file/cfg_file_test/Cargo.toml index 73e580b..d1b527a 100644 --- a/crates/utils/cfg_file/cfg_file_test/Cargo.toml +++ b/crates/utils/cfg_file/cfg_file_test/Cargo.toml @@ -6,4 +6,4 @@ edition = "2024" [dependencies] cfg_file = { path = "../../cfg_file", features = ["default"] } tokio = { version = "1.46.1", features = ["full"] } -serde = { version = "1.0.219", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0.219", features = ["derive"] } diff --git a/crates/utils/cfg_file/cfg_file_test/src/lib.rs b/crates/utils/cfg_file/cfg_file_test/src/lib.rs index ea490c9..fd805f1 100644 --- a/crates/utils/cfg_file/cfg_file_test/src/lib.rs +++ b/crates/utils/cfg_file/cfg_file_test/src/lib.rs @@ -4,10 +4,10 @@ pub fn add(left: u64, right: u64) -> u64 { #[cfg(test)] mod test_cfg_file { - use std::collections::HashMap; - use serde::{Deserialize, Serialize}; - use cfg_file::config::ConfigFile; use cfg_file::ConfigFile; + use cfg_file::config::ConfigFile; + use serde::{Deserialize, Serialize}; + use std::collections::HashMap; #[derive(ConfigFile, Deserialize, Serialize, Default)] #[cfg_file(path = "./.temp/example_cfg.toml")] @@ -23,19 +23,22 @@ mod test_cfg_file { let mut example = ExampleConfig { name: "Weicao".to_string(), age: 22, - hobby: vec![ "Programming", "Painting" ] + hobby: vec!["Programming", "Painting"] .iter() .map(|m| m.to_string()) .collect(), - secret: HashMap::new() + secret: HashMap::new(), }; - let secret_no_comments = "Actually, I'm really too lazy to write comments, documentation, and unit tests."; - example.secret + let secret_no_comments = + "Actually, I'm really too lazy to write comments, documentation, and unit tests."; + example + .secret .entry("No comments".to_string()) .insert_entry(secret_no_comments.to_string()); let secret_peek = "Of course, it's peeking at you who's reading the source code."; - example.secret + example + .secret .entry("Peek".to_string()) .insert_entry(secret_peek.to_string()); @@ -45,8 +48,8 @@ mod test_cfg_file { let read_cfg = ExampleConfig::read().await; assert_eq!(read_cfg.name, "Weicao"); assert_eq!(read_cfg.age, 22); - assert_eq!(read_cfg.hobby, vec![ "Programming", "Painting" ]); + assert_eq!(read_cfg.hobby, vec!["Programming", "Painting"]); assert_eq!(read_cfg.secret["No comments"], secret_no_comments); assert_eq!(read_cfg.secret["Peek"], secret_peek); } -} \ No newline at end of file +} diff --git a/crates/utils/cfg_file/src/config.rs b/crates/utils/cfg_file/src/config.rs index 2bc104d..8d97bf0 100644 --- a/crates/utils/cfg_file/src/config.rs +++ b/crates/utils/cfg_file/src/config.rs @@ -1,15 +1,12 @@ use async_trait::async_trait; -use serde::{ Deserialize, Serialize }; +use serde::{Deserialize, Serialize}; use std::{ borrow::Cow, env::current_dir, io::Error, - path:: { PathBuf, Path }, -}; -use tokio::{ - fs, - io::AsyncReadExt + path::{Path, PathBuf}, }; +use tokio::{fs, io::AsyncReadExt}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum ConfigFormat { @@ -46,7 +43,7 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { Self: Sized + Send + Sync, { let Ok(path) = Self::default_path() else { - return Self::DataType::default() + return Self::DataType::default(); }; Self::read_from(path).await @@ -86,7 +83,8 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { } // Determine file format - let format = file_path.file_name() + let format = file_path + .file_name() .and_then(|name| name.to_str()) .and_then(ConfigFormat::from_filename) .unwrap_or(ConfigFormat::Json); // Default to JSON @@ -136,9 +134,10 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { let path = path.as_ref(); if let Some(parent) = path.parent() - && ! parent.exists() { - let _ = tokio::fs::create_dir_all(parent).await; - } + && !parent.exists() + { + let _ = tokio::fs::create_dir_all(parent).await; + } let file_path = match current_dir() { Ok(cwd) => cwd.join(path), @@ -149,7 +148,8 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { }; // Determine file format - let format = file_path.file_name() + let format = file_path + .file_name() .and_then(|name| name.to_str()) .and_then(ConfigFormat::from_filename) .unwrap_or(ConfigFormat::Json); // Default to JSON @@ -181,7 +181,10 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { // Don't write if serialization failed if contents.is_empty() { - eprintln!("Serialization failed for file {}, not writing", path.display()); + eprintln!( + "Serialization failed for file {}, not writing", + path.display() + ); return; } @@ -190,4 +193,4 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default { eprintln!("Failed to write file {}: {}", path.display(), e); } } -} \ No newline at end of file +} diff --git a/crates/utils/cfg_file/src/lib.rs b/crates/utils/cfg_file/src/lib.rs index 945c3fd..72246e7 100644 --- a/crates/utils/cfg_file/src/lib.rs +++ b/crates/utils/cfg_file/src/lib.rs @@ -4,4 +4,4 @@ extern crate cfg_file_derive; #[cfg(feature = "derive")] pub use cfg_file_derive::*; -pub mod config; \ No newline at end of file +pub mod config; diff --git a/crates/utils/member_verify/src/verifier.rs b/crates/utils/member_verify/src/verifier.rs index e69de29..8b13789 100644 --- a/crates/utils/member_verify/src/verifier.rs +++ b/crates/utils/member_verify/src/verifier.rs @@ -0,0 +1 @@ + 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 { - fn process(stream: TcpStream); } pub trait ServerHandle { - 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 -where Client: ClientHandle, - Server: ServerHandle { - +where + Client: ClientHandle, + Server: ServerHandle, +{ /// Client Handle client_handle: Option, @@ -25,8 +26,10 @@ where Client: ClientHandle, } impl Default for TcpServerTarget -where Client: ClientHandle, - Server: ServerHandle { +where + Client: ClientHandle, + Server: ServerHandle, +{ fn default() -> Self { Self { client_handle: None, @@ -38,23 +41,25 @@ where Client: ClientHandle, } impl From for TcpServerTarget -where Client: ClientHandle, - Server: ServerHandle { - +where + Client: ClientHandle, + Server: ServerHandle, +{ /// Convert SocketAddr to TcpServerTarget fn from(value: SocketAddr) -> Self { Self { port: value.port(), bind_addr: value.ip(), - .. Self::default() + ..Self::default() } } } impl From> for SocketAddr -where Client: ClientHandle, - Server: ServerHandle { - +where + Client: ClientHandle, + Server: ServerHandle, +{ /// Convert TcpServerTarget to SocketAddr fn from(val: TcpServerTarget) -> Self { SocketAddr::new(val.bind_addr, val.port) @@ -62,23 +67,26 @@ where Client: ClientHandle, } impl Display for TcpServerTarget -where Client: ClientHandle, - Server: ServerHandle { +where + Client: ClientHandle, + Server: ServerHandle, +{ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}:{}", self.bind_addr, self.port) } } impl TcpServerTarget -where Client: ClientHandle, - Server: ServerHandle { - +where + Client: ClientHandle, + Server: ServerHandle, +{ /// Create target by address pub fn from_addr(addr: impl Into, port: impl Into) -> Self { Self { port: port.into(), bind_addr: addr.into(), - .. Self::default() + ..Self::default() } } @@ -86,12 +94,8 @@ where Client: ClientHandle, pub fn from_str<'a>(addr_str: impl Into<&'a str>) -> Result { 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() { - 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 for ExampleClientHandle { - fn process(stream: TcpStream) { - - } + fn process(stream: TcpStream) {} } pub(crate) struct ExampleServerHandle; impl ServerHandle 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::::from_str(host) else { + let Ok(target) = TcpServerTarget::::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::::from_domain(host).await else { + TcpServerTarget::::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, -- cgit