summary refs log tree commit diff stats
path: root/src/file_tree/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/file_tree/mod.rs')
-rw-r--r--src/file_tree/mod.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/file_tree/mod.rs b/src/file_tree/mod.rs
new file mode 100644
index 0000000..d6f0c3c
--- /dev/null
+++ b/src/file_tree/mod.rs
@@ -0,0 +1,88 @@
+/*
+* Copyright (C) 2023 - 2024:
+* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
+* SPDX-License-Identifier: LGPL-3.0-or-later
+*
+* This file is part of the Trixy crate for Trinitrix.
+*
+* Trixy is free software: you can redistribute it and/or modify
+* it under the terms of the Lesser GNU General Public License as
+* published by the Free Software Foundation, either version 3 of
+* the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* and the Lesser GNU General Public License along with this program.
+* If not, see <https://www.gnu.org/licenses/>.
+*/
+
+//! [`FileTree`]s are the fundamental data structure used by trixy to present generated data to
+//! you.
+
+use std::{
+    fs, io,
+    path::{Path, PathBuf},
+};
+
+/// A file tree containing all files that were generated. These are separated into host and
+/// auxiliary files. See their respective descriptions about what differentiates them.
+#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
+pub struct FileTree {
+    /// Files, that are supposed to be included in the compiled crate.
+    pub files: Vec<GeneratedFile>,
+}
+
+/// A generated files
+#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
+pub struct GeneratedFile {
+    /// The path this generated file would like to be placed at.
+    /// This path is relative to the crate root.
+    pub path: PathBuf,
+
+    /// The content of this file.
+    ///
+    /// This should already be formatted and ready to be used.
+    pub value: String,
+}
+
+impl GeneratedFile {
+    pub fn new(path: PathBuf, value: String) -> Self {
+        Self { path, value }
+    }
+    pub fn new_in_out_dir(name: String, value: String, out_dir: &Path) -> Self {
+        let path = out_dir.join(name);
+        Self { path, value }
+    }
+
+    pub fn materialize(self) -> io::Result<()> {
+        fs::create_dir_all(self.path.parent().expect("This path should have a parent"))?;
+        fs::write(self.path, self.value.as_bytes())?;
+        Ok(())
+    }
+}
+
+impl FileTree {
+    pub fn new() -> Self {
+        Self::default()
+    }
+
+    pub fn add_file(&mut self, file: GeneratedFile) {
+        self.files.push(file)
+    }
+
+    pub fn extend(&mut self, files: Vec<GeneratedFile>) {
+        files.into_iter().for_each(|file| self.add_file(file));
+    }
+
+    pub fn materialize(self) -> io::Result<()> {
+        self.files
+            .into_iter()
+            .map(|file| -> io::Result<()> { file.materialize() })
+            .collect::<io::Result<()>>()?;
+        Ok(())
+    }
+}