From 22926ce29e3f8e040ec349401aeb6a77f32eae72 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 4 Mar 2026 21:26:04 +0800 Subject: Initialize Butchunker project structure and policy system --- src/utils/file_input_solve.rs | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/utils/file_input_solve.rs (limited to 'src/utils/file_input_solve.rs') diff --git a/src/utils/file_input_solve.rs b/src/utils/file_input_solve.rs new file mode 100644 index 0000000..30d5765 --- /dev/null +++ b/src/utils/file_input_solve.rs @@ -0,0 +1,82 @@ +use std::{ + env::current_dir, + path::{Path, PathBuf}, +}; + +use just_fmt::fmt_path::fmt_path; + +pub fn parse_path_input( + files: Vec, + recursive: bool, + exclude_dir: Vec<&str>, +) -> Vec { + let current_dir = current_dir().unwrap(); + let files = if recursive { + let mut result: Vec = Vec::new(); + for arg in files.iter().skip(1) { + if exclude_dir.contains(&arg.as_str()) { + continue; + } + let path = current_dir.join(arg); + if path.is_dir() { + if let Err(e) = collect_files_recursively(&path, &mut result) { + eprintln!("Error collecting files recursively: {}", e); + continue; + } + } else { + result.push(path); + } + } + result + } else { + let mut result = Vec::new(); + for arg in files.iter().skip(1) { + if exclude_dir.contains(&arg.as_str()) { + continue; + } + let path = current_dir.join(arg); + if path.is_dir() { + if files.len() == 2 { + for entry in std::fs::read_dir(&path) + .unwrap_or_else(|e| { + eprintln!("Error reading directory: {}", e); + std::fs::read_dir(".").unwrap() + }) + .flatten() + { + let entry_path = entry.path(); + if !entry_path.is_dir() { + result.push(entry_path); + } + } + } + } else { + result.push(path); + } + } + result + }; + files + .into_iter() + .filter_map(|path| match fmt_path(path) { + Ok(formatted_path) => Some(formatted_path), + Err(e) => { + eprintln!("Error formatting path: {}", e); + None + } + }) + .collect() +} + +fn collect_files_recursively(dir: &Path, files: &mut Vec) -> std::io::Result<()> { + for entry in std::fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + collect_files_recursively(&path, files)?; + } else { + files.push(path); + } + } + Ok(()) +} -- cgit