Add place() method to VFolder for placing virtual file system on disk

This commit is contained in:
Moritz Hölting 2024-03-21 16:34:27 +01:00
parent 5cc403970b
commit 343f014e6c
1 changed files with 19 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, fs, io, path::Path};
#[derive(Debug, Default, Clone)]
pub struct VFolder {
@ -102,6 +102,24 @@ impl VFolder {
self.files.get_mut(name)
}
}
pub fn place(&self, path: &Path) -> io::Result<()> {
fs::create_dir_all(path)?;
for (name, folder) in &self.folders {
folder.place(&path.join(name))?;
}
for (name, file) in &self.files {
match file {
VFile::Text(text) => {
fs::write(path.join(name), text)?;
}
VFile::Binary(data) => {
fs::write(path.join(name), data)?;
}
}
}
Ok(())
}
}
#[derive(Debug, Clone)]