refactor: modularize CLI and optimize cache metadata lookups
- Extract error handling, path validation, and handlers into separate modules - Add CacheCheckResult to pre-fetch metadata and avoid double lookups - Deprecate legacy cache methods in favor of optimized alternatives - Enable tokio fs feature for async file operations - Remove debug profile from release build
This commit is contained in:
30
src/cli/path_utils.rs
Normal file
30
src/cli/path_utils.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Validates that a path exists and is a readable directory.
|
||||
/// Returns the canonicalized path if validation succeeds.
|
||||
pub async fn validate_and_normalize_path(path: &Path) -> Result<PathBuf, String> {
|
||||
// Use tokio::fs for async file operations
|
||||
let metadata = tokio::fs::metadata(path).await.map_err(|e| {
|
||||
if e.kind() == std::io::ErrorKind::NotFound {
|
||||
format!("Path '{}' does not exist", path.display())
|
||||
} else {
|
||||
format!("Cannot access '{}': {}", path.display(), e)
|
||||
}
|
||||
})?;
|
||||
|
||||
if !metadata.is_dir() {
|
||||
return Err(format!("Path '{}' is not a directory", path.display()));
|
||||
}
|
||||
|
||||
// Check if we can read the directory
|
||||
let _ = tokio::fs::read_dir(path)
|
||||
.await
|
||||
.map_err(|e| format!("Cannot access directory '{}': {}", path.display(), e))?;
|
||||
|
||||
// canonicalize is sync-only, use spawn_blocking
|
||||
let path_owned = path.to_path_buf();
|
||||
tokio::task::spawn_blocking(move || path_owned.canonicalize())
|
||||
.await
|
||||
.map_err(|e| format!("Task failed: {}", e))?
|
||||
.map_err(|e| format!("Failed to normalize path '{}': {}", path.display(), e))
|
||||
}
|
||||
Reference in New Issue
Block a user