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 = 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); }