Refactored the application to improve performance, maintainability, and binary size. Key Changes: - **Core Algorithm**: Switched to a parallel, single-pass tree construction algorithm for O(N) traversal. - **Architecture**: Extract core logic from `main.rs` to `lib.rs` to enable unit testing. - **Testing**: Added comprehensive unit tests and integration tests (`tests/cli.rs`) covering CLI arguments and output formats. - **Benchmarking**: Added `criterion` benchmarks for performance tracking. - **Dependencies**: - Removed `walkdir` (logic replaced by `ignore` + custom tree). - Removed `humansize` (consolidated to `byte-unit`). - Replaced `chrono` with lightweight `time` crate. - Removed unused `atty`. - **Reporting**: Optimized CSV and JSON output for correctness and zero-copy performance. - **Documentation**: Updated CHANGELOG.md and bumped version to 0.2.0.
39 lines
1.4 KiB
Rust
39 lines
1.4 KiB
Rust
use clap::Parser;
|
|
use sized::{run, Args, OutputFormat};
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
use time::macros::format_description;
|
|
use time::OffsetDateTime;
|
|
|
|
fn main() {
|
|
let args = Args::parse();
|
|
|
|
// Determine output writer logic (moved mostly to main to keep lib clean,
|
|
// or keep in lib? Plan said simplified main. Let's keep file creation in main usually for CLI apps,
|
|
// but the logic for "auto name" is business logic.
|
|
// Wait, the lib function `run` takes `writer`. So we need to create the writer here.
|
|
|
|
let mut writer: Box<dyn Write> = if let Some(path_arg) = &args.save {
|
|
let output_path = if path_arg.to_string_lossy() == "_AUTO_" {
|
|
let format = format_description!("[year][month][day]-[hour][minute][second]");
|
|
let timestamp = OffsetDateTime::now_utc().format(format).unwrap();
|
|
let dir_name = args.path.file_name().unwrap_or_default().to_string_lossy();
|
|
let ext = match args.format {
|
|
OutputFormat::Csv => "csv",
|
|
OutputFormat::Json => "json",
|
|
_ => "txt",
|
|
};
|
|
PathBuf::from(format!("{}_{}.{}", timestamp, dir_name, ext))
|
|
} else {
|
|
path_arg.clone()
|
|
};
|
|
|
|
let f = std::fs::File::create(&output_path).expect("Failed to create output file");
|
|
Box::new(f)
|
|
} else {
|
|
Box::new(std::io::stdout())
|
|
};
|
|
|
|
run(args, &mut writer);
|
|
}
|