use std::path::PathBuf;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct File {
pub path: PathBuf,
pub content: String
}
impl File {
pub fn new(path: PathBuf, content: String) -> Self {
Self { path, content }
}
pub fn write<S: AsRef<str>>(&mut self, content: S) {
self.content.push_str(content.as_ref());
}
pub fn writeln<S: AsRef<str>>(&mut self, content: S) {
self.content.push_str(content.as_ref());
self.content.push('\n');
}
}
#[derive(Debug, Default, Clone)]
pub struct FileSet {
pub(crate) files: HashMap<PathBuf, File>
}
impl FileSet {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, file: File) {
self.files.insert(file.path.clone(), file);
}
pub fn get_mut(&mut self, path: &PathBuf) -> Option<&mut File> {
self.files.get_mut(path)
}
pub fn entry(&mut self, path: &PathBuf) -> &mut File {
self.files.entry(path.to_path_buf()).or_insert(File::new(path.clone(), Default::default()))
}
}