use std::{
collections::{hash_set::HashSet, BTreeMap},
io,
path::Path,
};
use cargo_toml_workspace::cargo_toml::AbstractFilesystem;
use normalize_path::NormalizePath;
#[derive(Debug, Default)]
pub(super) struct Vfs(BTreeMap<Box<Path>, HashSet<Box<str>>>);
impl Vfs {
pub(super) fn add_path(&mut self, mut path: &Path) {
while let Some(parent) = path.parent() {
let filename = path.file_name().unwrap();
let filename = filename.to_string_lossy();
self.0
.entry(parent.into())
.or_insert_with(|| HashSet::with_capacity(4))
.insert(filename.into());
path = parent;
}
}
}
impl AbstractFilesystem for Vfs {
fn file_names_in(&self, rel_path: &str) -> io::Result<HashSet<Box<str>>> {
let rel_path = Path::new(rel_path).normalize();
Ok(self.0.get(&*rel_path).cloned().unwrap_or_default())
}
}