From 343f014e6cef2958ac44de1604d883a38ff2b826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:34:27 +0100 Subject: [PATCH] Add place() method to VFolder for placing virtual file system on disk --- src/virtual_fs.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/virtual_fs.rs b/src/virtual_fs.rs index a9672c0..cc0189c 100644 --- a/src/virtual_fs.rs +++ b/src/virtual_fs.rs @@ -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)]