From d2e4cb85af2c6101cd435c10cfd3d4084cd83e76 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 18 Jun 2026 00:50:06 +0800 Subject: refactor: use `io::Error::other` and simplify closures --- rola-bucket/src/protocol.rs | 2 ++ rola-bucket/src/protocol/local_fs.rs | 8 +++--- .../functions/src/copy_with_temp_rename/async.rs | 31 +++++++++------------- .../functions/src/copy_with_temp_rename/sync.rs | 31 +++++++++------------- rola-utils/functions/src/test_sandbox.rs | 4 +-- 5 files changed, 32 insertions(+), 44 deletions(-) diff --git a/rola-bucket/src/protocol.rs b/rola-bucket/src/protocol.rs index b4d60a8..6d1074a 100644 --- a/rola-bucket/src/protocol.rs +++ b/rola-bucket/src/protocol.rs @@ -1,3 +1,5 @@ +#![allow(clippy::manual_async_fn)] + use std::path::Path; mod error; diff --git a/rola-bucket/src/protocol/local_fs.rs b/rola-bucket/src/protocol/local_fs.rs index 1f287f2..b3885a7 100644 --- a/rola-bucket/src/protocol/local_fs.rs +++ b/rola-bucket/src/protocol/local_fs.rs @@ -38,7 +38,7 @@ impl BucketTransferProtocol for LocalFileSystemProtocol { let abs_send_path = req.absolute_file_path_to_upload; let abs_receive_path = bucket_root.join(req.relative_file_path_to_receive); copy_with_temp_rename(abs_send_path, abs_receive_path) - .map_err(|e| BucketTransferProtocolError::IoError(e))?; + .map_err(BucketTransferProtocolError::IoError)?; Ok(()) } @@ -54,7 +54,7 @@ impl BucketTransferProtocol for LocalFileSystemProtocol { let abs_send_path = bucket_root.join(req.relative_file_path_to_transfer); let abs_receive_path = req.absolute_file_path_to_download; copy_with_temp_rename(abs_send_path, abs_receive_path) - .map_err(|e| BucketTransferProtocolError::IoError(e))?; + .map_err(BucketTransferProtocolError::IoError)?; Ok(()) } @@ -70,7 +70,7 @@ impl BucketTransferProtocol for LocalFileSystemProtocol { let abs_send_path = req.absolute_file_path_to_transfer; let abs_receive_path = client_root.join(req.relative_file_path_to_download); copy_with_temp_rename(abs_send_path, abs_receive_path) - .map_err(|e| BucketTransferProtocolError::IoError(e))?; + .map_err(BucketTransferProtocolError::IoError)?; Ok(()) } @@ -86,7 +86,7 @@ impl BucketTransferProtocol for LocalFileSystemProtocol { let abs_send_path = req.absolute_file_path_to_receive; let abs_receive_path = client_root.join(req.relative_file_path_to_upload); copy_with_temp_rename(abs_send_path, abs_receive_path) - .map_err(|e| BucketTransferProtocolError::IoError(e))?; + .map_err(BucketTransferProtocolError::IoError)?; Ok(()) } } diff --git a/rola-utils/functions/src/copy_with_temp_rename/async.rs b/rola-utils/functions/src/copy_with_temp_rename/async.rs index 83275a1..33f7693 100644 --- a/rola-utils/functions/src/copy_with_temp_rename/async.rs +++ b/rola-utils/functions/src/copy_with_temp_rename/async.rs @@ -35,29 +35,23 @@ async fn internal_copy_with_temp_rename(src: &Path, dst: &Path) -> io::Result<() } } (Err(e), _) | (_, Err(e)) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize paths (src: {}, dst: {}): {}", - src.display(), - dst.display(), - e - ), - )); + return Err(io::Error::other(format!( + "failed to canonicalize paths (src: {}, dst: {}): {}", + src.display(), + dst.display(), + e + ))); } } } else { // dst doesn't exist yet, so it cannot be the same file as src // If src canonicalization fails, propagate the error let _ = src_canonical.map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize source path (src: {}): {}", - src.display(), - e - ), - ) + io::Error::other(format!( + "failed to canonicalize source path (src: {}): {}", + src.display(), + e + )) })?; } @@ -215,8 +209,7 @@ async fn atomic_create_temp(dir: &Path, base: &str) -> io::Result { } } - Err(io::Error::new( - io::ErrorKind::Other, + Err(io::Error::other( "exceeded max attempts to create temp file", )) } diff --git a/rola-utils/functions/src/copy_with_temp_rename/sync.rs b/rola-utils/functions/src/copy_with_temp_rename/sync.rs index cdbe3db..18e0fcf 100644 --- a/rola-utils/functions/src/copy_with_temp_rename/sync.rs +++ b/rola-utils/functions/src/copy_with_temp_rename/sync.rs @@ -28,29 +28,23 @@ fn internal_copy_with_temp_rename(src: &Path, dst: &Path) -> io::Result<()> { } } (Err(e), _) | (_, Err(e)) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize paths (src: {}, dst: {}): {}", - src.display(), - dst.display(), - e - ), - )); + return Err(io::Error::other(format!( + "failed to canonicalize paths (src: {}, dst: {}): {}", + src.display(), + dst.display(), + e + ))); } } } else { // dst doesn't exist yet, so it cannot be the same file as src // If src canonicalization fails, propagate the error let _ = src_canonical.map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize source path (src: {}): {}", - src.display(), - e - ), - ) + io::Error::other(format!( + "failed to canonicalize source path (src: {}): {}", + src.display(), + e + )) })?; } @@ -202,8 +196,7 @@ fn atomic_create_temp(dir: &Path, base: &str) -> io::Result { } } - Err(io::Error::new( - io::ErrorKind::Other, + Err(io::Error::other( "exceeded max attempts to create temp file", )) } diff --git a/rola-utils/functions/src/test_sandbox.rs b/rola-utils/functions/src/test_sandbox.rs index e0065f6..2705389 100644 --- a/rola-utils/functions/src/test_sandbox.rs +++ b/rola-utils/functions/src/test_sandbox.rs @@ -73,9 +73,9 @@ fn try_clear_contents(path: &Path) -> std::io::Result<()> { for entry in fs::read_dir(path)? { let entry = entry?; if entry.file_type()?.is_dir() { - fs::remove_dir_all(&entry.path())?; + fs::remove_dir_all(entry.path())?; } else { - fs::remove_file(&entry.path())?; + fs::remove_file(entry.path())?; } } } -- cgit