qanglang_test/
test_file.rs1use std::path::{Path, PathBuf};
2
3#[derive(Debug, Clone)]
5pub struct SourceFile {
6 pub file_path: PathBuf,
8 pub display_path: String,
10}
11
12impl SourceFile {
13 pub fn new(file_path: PathBuf, display_path: String) -> Self {
14 Self {
15 file_path,
16 display_path,
17 }
18 }
19}
20
21pub struct SourceFileResolver {
22 user_working_dir: PathBuf,
23}
24
25impl SourceFileResolver {
28 pub fn new() -> std::io::Result<Self> {
29 let user_working_dir = std::env::current_dir()?;
30 Ok(Self { user_working_dir })
31 }
32
33 pub fn resolve(&self, user_input: &str) -> std::io::Result<Vec<SourceFile>> {
35 let input_path = Path::new(user_input);
36
37 let absolute_path = if input_path.is_absolute() {
39 input_path.to_path_buf()
40 } else {
41 self.user_working_dir.join(input_path)
42 };
43
44 let canonical_path = absolute_path
46 .canonicalize()
47 .unwrap_or_else(|_| absolute_path.clone());
48
49 if canonical_path.is_file() {
50 self.resolve_single_file(&canonical_path)
52 } else if canonical_path.is_dir() {
53 self.resolve_directory(&canonical_path)
55 } else {
56 Err(std::io::Error::new(
57 std::io::ErrorKind::NotFound,
58 format!("Path '{}' does not exist", user_input),
59 ))
60 }
61 }
62
63 fn resolve_single_file(&self, absolute_path: &Path) -> std::io::Result<Vec<SourceFile>> {
64 let display_path = self.create_display_path(absolute_path);
66
67 Ok(vec![SourceFile::new(
68 absolute_path.to_path_buf(),
69 display_path,
70 )])
71 }
72
73 fn resolve_directory(&self, absolute_path: &Path) -> std::io::Result<Vec<SourceFile>> {
74 let mut source_files = Vec::new();
75 self.collect_ql_files(absolute_path, &mut source_files)?;
76
77 source_files.sort_by(|a, b| a.display_path.cmp(&b.display_path));
79
80 Ok(source_files)
81 }
82
83 fn collect_ql_files(&self, dir: &Path, files: &mut Vec<SourceFile>) -> std::io::Result<()> {
84 for entry in std::fs::read_dir(dir)? {
85 let entry = entry?;
86 let path = entry.path();
87
88 if path.is_dir() {
89 self.collect_ql_files(&path, files)?;
90 } else if path.extension().and_then(|s| s.to_str()) == Some("ql") {
91 let display_path = self.create_display_path(&path);
92 files.push(SourceFile::new(path, display_path));
93 }
94 }
95 Ok(())
96 }
97
98 fn create_display_path(&self, absolute_path: &Path) -> String {
99 let canonical_work_dir = self
101 .user_working_dir
102 .canonicalize()
103 .unwrap_or_else(|_| self.user_working_dir.clone());
104
105 if let Ok(relative) = absolute_path.strip_prefix(&canonical_work_dir) {
107 relative.to_string_lossy().to_string()
108 } else {
109 self.find_best_display_path(absolute_path)
111 }
112 }
113
114 fn find_best_display_path(&self, absolute_path: &Path) -> String {
115 let file_components: Vec<_> = absolute_path.components().collect();
117 let work_components: Vec<_> = self.user_working_dir.components().collect();
118
119 let mut common_len = 0;
121 for (file_comp, work_comp) in file_components.iter().zip(work_components.iter()) {
122 if file_comp == work_comp {
123 common_len += 1;
124 } else {
125 break;
126 }
127 }
128
129 if common_len > 0 {
130 let remaining_components = &file_components[common_len..];
132 let mut result = PathBuf::new();
133 for component in remaining_components {
134 result.push(component);
135 }
136 result.to_string_lossy().to_string()
137 } else {
138 absolute_path
140 .file_name()
141 .unwrap_or(absolute_path.as_os_str())
142 .to_string_lossy()
143 .to_string()
144 }
145 }
146}