mod anchored_path;
pub mod file_set;
pub mod loader;
mod path_interner;
mod vfs_path;
use std::{fmt, mem};
use crate::path_interner::PathInterner;
pub use crate::{
anchored_path::{AnchoredPath, AnchoredPathBuf},
vfs_path::VfsPath,
};
pub use paths::{AbsPath, AbsPathBuf};
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct FileId(pub u32);
#[derive(Default)]
pub struct Vfs {
interner: PathInterner,
data: Vec<Option<Vec<u8>>>,
changes: Vec<ChangedFile>,
}
pub struct ChangedFile {
pub file_id: FileId,
pub change_kind: ChangeKind,
}
impl ChangedFile {
pub fn exists(&self) -> bool {
self.change_kind != ChangeKind::Delete
}
pub fn is_created_or_deleted(&self) -> bool {
matches!(self.change_kind, ChangeKind::Create | ChangeKind::Delete)
}
}
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum ChangeKind {
Create,
Modify,
Delete,
}
impl Vfs {
pub fn len(&self) -> usize {
self.data.len()
}
pub fn file_id(&self, path: &VfsPath) -> Option<FileId> {
self.interner.get(path).filter(|&it| self.get(it).is_some())
}
pub fn file_path(&self, file_id: FileId) -> VfsPath {
self.interner.lookup(file_id).clone()
}
pub fn file_contents(&self, file_id: FileId) -> &[u8] {
self.get(file_id).as_deref().unwrap()
}
pub fn iter(&self) -> impl Iterator<Item = (FileId, &VfsPath)> + '_ {
(0..self.data.len())
.map(|it| FileId(it as u32))
.filter(move |&file_id| self.get(file_id).is_some())
.map(move |file_id| {
let path = self.interner.lookup(file_id);
(file_id, path)
})
}
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
let file_id = self.alloc_file_id(path);
let change_kind = match (&self.get(file_id), &contents) {
(None, None) => return false,
(None, Some(_)) => ChangeKind::Create,
(Some(_), None) => ChangeKind::Delete,
(Some(old), Some(new)) if old == new => return false,
(Some(_), Some(_)) => ChangeKind::Modify,
};
*self.get_mut(file_id) = contents;
self.changes.push(ChangedFile { file_id, change_kind });
true
}
pub fn has_changes(&self) -> bool {
!self.changes.is_empty()
}
pub fn take_changes(&mut self) -> Vec<ChangedFile> {
mem::take(&mut self.changes)
}
fn alloc_file_id(&mut self, path: VfsPath) -> FileId {
let file_id = self.interner.intern(path);
let idx = file_id.0 as usize;
let len = self.data.len().max(idx + 1);
self.data.resize_with(len, || None);
file_id
}
fn get(&self, file_id: FileId) -> &Option<Vec<u8>> {
&self.data[file_id.0 as usize]
}
fn get_mut(&mut self, file_id: FileId) -> &mut Option<Vec<u8>> {
&mut self.data[file_id.0 as usize]
}
}
impl fmt::Debug for Vfs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Vfs").field("n_files", &self.data.len()).finish()
}
}