1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use ignore::WalkBuilder;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
/// Collect files from paths with given extensions
pub fn collect_files(paths: &[String], extensions: &[&str]) -> anyhow::Result<Vec<PathBuf>> {
let mut files = Vec::new();
let mut visited = HashSet::new();
// Process each path
for path_str in paths {
let path = Path::new(path_str);
if path.is_file() {
// If it's a file, check extension and add it
if let Some(ext) = path.extension() {
if let Some(ext_str) = ext.to_str() {
if extensions.contains(&ext_str) {
if let Ok(canonical) = path.canonicalize() {
if visited.insert(canonical.clone()) {
files.push(path.to_path_buf());
}
}
}
}
}
} else if path.is_dir() {
// If it's a directory, walk it respecting .gitignore
let walker = WalkBuilder::new(path).follow_links(false).build();
for entry in walker {
let entry = entry?;
let entry_path = entry.path();
// Skip if not a file
if !entry_path.is_file() {
continue;
}
// Check extension
if let Some(ext) = entry_path.extension() {
if let Some(ext_str) = ext.to_str() {
if extensions.contains(&ext_str) {
if let Ok(canonical) = entry_path.canonicalize() {
if visited.insert(canonical.clone()) {
files.push(entry_path.to_path_buf());
}
}
}
}
}
}
} else {
eprintln!("Path does not exist or is not accessible: {}", path_str);
}
}
// Sort files for consistent output
files.sort();
Ok(files)
}