use std::path::{Path, PathBuf};
use super::{FileTreeNode, QuillSource};
impl QuillSource {
pub fn get_file<P: AsRef<Path>>(&self, path: P) -> Option<&[u8]> {
self.files.get_file(path)
}
pub fn file_exists<P: AsRef<Path>>(&self, path: P) -> bool {
self.files.file_exists(path)
}
pub fn dir_exists<P: AsRef<Path>>(&self, path: P) -> bool {
self.files.dir_exists(path)
}
pub fn list_files<P: AsRef<Path>>(&self, path: P) -> Vec<String> {
self.files.list_files(path)
}
pub fn list_subdirectories<P: AsRef<Path>>(&self, path: P) -> Vec<String> {
self.files.list_subdirectories(path)
}
pub fn list_directory<P: AsRef<Path>>(&self, dir_path: P) -> Vec<PathBuf> {
let dir_path = dir_path.as_ref();
let filenames = self.files.list_files(dir_path);
filenames
.iter()
.map(|name| {
if dir_path == Path::new("") {
PathBuf::from(name)
} else {
dir_path.join(name)
}
})
.collect()
}
pub fn list_directories<P: AsRef<Path>>(&self, dir_path: P) -> Vec<PathBuf> {
let dir_path = dir_path.as_ref();
let subdirs = self.files.list_subdirectories(dir_path);
subdirs
.iter()
.map(|name| {
if dir_path == Path::new("") {
PathBuf::from(name)
} else {
dir_path.join(name)
}
})
.collect()
}
pub fn find_files<P: AsRef<Path>>(&self, pattern: P) -> Vec<PathBuf> {
let pattern_str = pattern.as_ref().to_string_lossy();
let mut matches = Vec::new();
let glob_pattern = match glob::Pattern::new(&pattern_str) {
Ok(pat) => pat,
Err(_) => return matches, };
Self::find_files_recursive(&self.files, Path::new(""), &glob_pattern, &mut matches);
matches.sort();
matches
}
fn find_files_recursive(
node: &FileTreeNode,
current_path: &Path,
pattern: &glob::Pattern,
matches: &mut Vec<PathBuf>,
) {
match node {
FileTreeNode::File { .. } => {
let path_str = current_path.to_string_lossy();
if pattern.matches(&path_str) {
matches.push(current_path.to_path_buf());
}
}
FileTreeNode::Directory { files } => {
for (name, child_node) in files {
let child_path = if current_path == Path::new("") {
PathBuf::from(name)
} else {
current_path.join(name)
};
Self::find_files_recursive(child_node, &child_path, pattern, matches);
}
}
}
}
}