use super::{MemoryFile, MemoryWorkspace};
use std::path::PathBuf;
impl MemoryWorkspace {
#[must_use]
pub fn with_file(self, path: &str, content: &str) -> Self {
let path_buf = PathBuf::from(path);
self.ensure_parent_dirs(&path_buf);
self.files
.write()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.insert(path_buf, MemoryFile::new(content.as_bytes().to_vec()));
self
}
#[must_use]
pub fn with_file_at_time(
self,
path: &str,
content: &str,
modified: std::time::SystemTime,
) -> Self {
let path_buf = PathBuf::from(path);
self.ensure_parent_dirs(&path_buf);
self.files.write()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.insert(
path_buf,
MemoryFile::with_modified(content.as_bytes().to_vec(), modified),
);
self
}
#[must_use]
pub fn with_file_bytes(self, path: &str, content: &[u8]) -> Self {
let path_buf = PathBuf::from(path);
self.ensure_parent_dirs(&path_buf);
self.files
.write()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.insert(path_buf, MemoryFile::new(content.to_vec()));
self
}
#[must_use]
pub fn with_dir(self, path: &str) -> Self {
let path_buf = PathBuf::from(path);
self.ensure_dir_path(&path_buf);
self
}
pub fn list_files_in_dir(&self, dir: &str) -> Vec<PathBuf> {
let dir_path = PathBuf::from(dir);
self.files
.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.keys()
.filter(|path| {
path.parent()
.is_some_and(|p| p == dir_path || p.starts_with(&dir_path))
})
.cloned()
.collect()
}
pub fn get_modified(&self, path: &str) -> Option<std::time::SystemTime> {
self.files
.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.get(&PathBuf::from(path))
.map(|f| f.modified)
}
pub fn list_directories(&self) -> Vec<PathBuf> {
self.directories.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace directories lock")
.iter().cloned().collect()
}
pub fn written_files(&self) -> std::collections::HashMap<PathBuf, Vec<u8>> {
self.files
.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.iter()
.map(|(k, v)| (k.clone(), v.content.clone()))
.collect()
}
pub fn get_file(&self, path: &str) -> Option<String> {
self.files
.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.get(&PathBuf::from(path))
.map(|f| String::from_utf8_lossy(&f.content).to_string())
}
pub fn get_file_bytes(&self, path: &str) -> Option<Vec<u8>> {
self.files
.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.get(&PathBuf::from(path))
.map(|f| f.content.clone())
}
pub fn was_written(&self, path: &str) -> bool {
self.files
.read()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.contains_key(&PathBuf::from(path))
}
pub fn clear(&self) {
self.files.write()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace files lock")
.clear();
self.directories.write()
.expect("RwLock poisoned - indicates panic in another thread holding MemoryWorkspace directories lock")
.clear();
}
}