summaryrefslogtreecommitdiff
path: root/src/utils/file_input_solve.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/file_input_solve.rs')
-rw-r--r--src/utils/file_input_solve.rs82
1 files changed, 82 insertions, 0 deletions
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<String>,
+ recursive: bool,
+ exclude_dir: Vec<&str>,
+) -> Vec<PathBuf> {
+ let current_dir = current_dir().unwrap();
+ let files = if recursive {
+ let mut result: Vec<PathBuf> = 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<PathBuf>) -> 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(())
+}