From 9a60751a901f568bdeb154c4115235d4f3a0f8b9 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 20 Mar 2026 21:54:29 +0800 Subject: Apply clippy suggestions and improve code quality --- systems/_asset/src/asset.rs | 37 +++++++++++++++---------------------- systems/_asset/src/rw.rs | 16 ++++++++-------- 2 files changed, 23 insertions(+), 30 deletions(-) (limited to 'systems/_asset/src') diff --git a/systems/_asset/src/asset.rs b/systems/_asset/src/asset.rs index c2b75e6..f452e2b 100644 --- a/systems/_asset/src/asset.rs +++ b/systems/_asset/src/asset.rs @@ -99,21 +99,17 @@ where .open(&lock_path) .await { - Ok(_) => { - return Ok(Handle { - _data_type: PhantomData, - writed: false, - asset_path: self.path.clone(), - lock_path, - temp_path, - }); - } + Ok(_) => Ok(Handle { + _data_type: PhantomData, + writed: false, + asset_path: self.path.clone(), + lock_path, + temp_path, + }), Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { - return Err(HandleLockError::AssetLocked); - } - Err(e) => { - return Err(HandleLockError::IoError(e)); + Err(HandleLockError::AssetLocked) } + Err(e) => Err(HandleLockError::IoError(e)), } } @@ -231,7 +227,7 @@ where if self.writed { tokio::fs::rename(&from, &to) .await - .map_err(|e| DataApplyError::IoError(e))?; + .map_err(DataApplyError::IoError)?; } Ok(()) } @@ -350,18 +346,15 @@ async fn check_asset_path(handle: &Handle) -> Result<(), PrecheckFailed> where D: RWData, { - if let Some(file_name) = handle.asset_path.file_name() { - if check_path(file_name).is_ok() { - return Ok(()); - } + if let Some(file_name) = handle.asset_path.file_name() + && check_path(file_name).is_ok() + { + return Ok(()); } Err(PrecheckFailed::AssetPathInvalid) } -async fn check_handle_is_cross_directory( - from: &PathBuf, - to: &PathBuf, -) -> Result<(), PrecheckFailed> { +async fn check_handle_is_cross_directory(from: &Path, to: &Path) -> Result<(), PrecheckFailed> { let from_parent = from.parent(); let to_parent = to.parent(); diff --git a/systems/_asset/src/rw.rs b/systems/_asset/src/rw.rs index 9a46144..7595138 100644 --- a/systems/_asset/src/rw.rs +++ b/systems/_asset/src/rw.rs @@ -1,17 +1,17 @@ -use std::path::PathBuf; +use std::path::Path; use crate::error::{DataReadError, DataWriteError}; pub trait RWData { /// Implement read logic /// Given a path, return the specific data - fn read(path: &PathBuf) -> impl Future> + Send + Sync; + fn read(path: &Path) -> impl Future> + Send + Sync; /// Implement write logic /// Given data and a path, write to the filesystem fn write( data: DataType, - path: &PathBuf, + path: &Path, ) -> impl Future> + Send + Sync; /// Provide test data @@ -42,10 +42,10 @@ pub struct FooData { } impl RWData for FooData { - async fn read(path: &PathBuf) -> Result { + async fn read(path: &Path) -> Result { let content = tokio::fs::read_to_string(path) .await - .map_err(|e| DataReadError::IoError(e))?; + .map_err(DataReadError::IoError)?; let parts: Vec<&str> = content.split('=').collect(); if parts.len() != 2 { return Err(DataReadError::ParseError("Invalid format".to_string())); @@ -57,11 +57,11 @@ impl RWData for FooData { Ok(FooData { age, name }) } - async fn write(data: FooData, path: &PathBuf) -> Result<(), DataWriteError> { + async fn write(data: FooData, path: &Path) -> Result<(), DataWriteError> { let content = format!("{}={}", data.name, data.age); tokio::fs::write(path, content) .await - .map_err(|e| DataWriteError::IoError(e))?; + .map_err(DataWriteError::IoError)?; Ok(()) } @@ -75,6 +75,6 @@ impl RWData for FooData { fn verify_data(data_a: FooData, data_b: FooData) -> bool { crate::ensure_eq!(data_a.age, data_b.age); crate::ensure_eq!(data_a.name, data_b.name); - return true; + true } } -- cgit