use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct SourceFile {
pub file_path: PathBuf,
pub display_path: String,
}
impl SourceFile {
pub fn new(file_path: PathBuf, display_path: String) -> Self {
Self {
file_path,
display_path,
}
}
}
pub struct SourceFileResolver {
user_working_dir: PathBuf,
}
impl SourceFileResolver {
pub fn new() -> std::io::Result<Self> {
let user_working_dir = std::env::current_dir()?;
Ok(Self { user_working_dir })
}
pub fn resolve(&self, user_input: &str) -> std::io::Result<Vec<SourceFile>> {
let input_path = Path::new(user_input);
let absolute_path = if input_path.is_absolute() {
input_path.to_path_buf()
} else {
self.user_working_dir.join(input_path)
};
let canonical_path = absolute_path
.canonicalize()
.unwrap_or_else(|_| absolute_path.clone());
if canonical_path.is_file() {
self.resolve_single_file(&canonical_path)
} else if canonical_path.is_dir() {
self.resolve_directory(&canonical_path)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Path '{}' does not exist", user_input),
))
}
}
fn resolve_single_file(&self, absolute_path: &Path) -> std::io::Result<Vec<SourceFile>> {
let display_path = self.create_display_path(absolute_path);
Ok(vec![SourceFile::new(
absolute_path.to_path_buf(),
display_path,
)])
}
fn resolve_directory(&self, absolute_path: &Path) -> std::io::Result<Vec<SourceFile>> {
let mut source_files = Vec::new();
self.collect_ql_files(absolute_path, &mut source_files)?;
source_files.sort_by(|a, b| a.display_path.cmp(&b.display_path));
Ok(source_files)
}
fn collect_ql_files(&self, dir: &Path, files: &mut Vec<SourceFile>) -> std::io::Result<()> {
for entry in std::fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
self.collect_ql_files(&path, files)?;
} else if path.extension().and_then(|s| s.to_str()) == Some("ql") {
let display_path = self.create_display_path(&path);
files.push(SourceFile::new(path, display_path));
}
}
Ok(())
}
fn create_display_path(&self, absolute_path: &Path) -> String {
let canonical_work_dir = self
.user_working_dir
.canonicalize()
.unwrap_or_else(|_| self.user_working_dir.clone());
if let Ok(relative) = absolute_path.strip_prefix(&canonical_work_dir) {
relative.to_string_lossy().to_string()
} else {
self.find_best_display_path(absolute_path)
}
}
fn find_best_display_path(&self, absolute_path: &Path) -> String {
let file_components: Vec<_> = absolute_path.components().collect();
let work_components: Vec<_> = self.user_working_dir.components().collect();
let mut common_len = 0;
for (file_comp, work_comp) in file_components.iter().zip(work_components.iter()) {
if file_comp == work_comp {
common_len += 1;
} else {
break;
}
}
if common_len > 0 {
let remaining_components = &file_components[common_len..];
let mut result = PathBuf::new();
for component in remaining_components {
result.push(component);
}
result.to_string_lossy().to_string()
} else {
absolute_path
.file_name()
.unwrap_or(absolute_path.as_os_str())
.to_string_lossy()
.to_string()
}
}
}