use rustc_hash::FxHashMap as HashMap;
#[derive(Debug, Clone)]
pub struct FileContent {
pub source: String,
pub display_path: String,
}
impl FileContent {
pub fn new(source: impl Into<String>, display_path: impl Into<String>) -> Self {
Self {
source: source.into(),
display_path: display_path.into(),
}
}
}
pub type Files = HashMap<String, FileContent>;
pub trait Loader {
fn scan_folder(&self, folder: &str) -> Files;
}
#[derive(Debug, Clone, Default)]
pub struct MemoryLoader {
folders: HashMap<String, Files>,
}
impl MemoryLoader {
pub fn new() -> Self {
Self::default()
}
pub fn add_file(&mut self, folder: &str, filename: &str, content: &str) {
self.add_file_with_display(folder, filename, filename, content);
}
pub fn add_file_with_display(
&mut self,
folder: &str,
filename: &str,
display_path: &str,
content: &str,
) {
self.folders.entry(folder.to_string()).or_default().insert(
filename.to_string(),
FileContent::new(content.to_string(), display_path.to_string()),
);
}
pub fn folders(&self) -> Vec<String> {
self.folders.keys().cloned().collect()
}
}
impl Loader for MemoryLoader {
fn scan_folder(&self, folder: &str) -> Files {
self.folders.get(folder).cloned().unwrap_or_default()
}
}