From 2e5e4b5736c446198e36760e254b7c17dd987166 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sun, 31 Mar 2024 21:57:01 +0200 Subject: refactor(treewide): Improve code quality by working with a FileTree The FileTree has been taken from the implementation written by my for the Trinitrix project. It alleviates the problem, where functions had to do many things themselves. --- src/file_tree/mod.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/file_tree/mod.rs (limited to 'src/file_tree/mod.rs') 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 +* 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 . +*/ + +//! [`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, +} + +/// 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) { + 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::>()?; + Ok(()) + } +} -- cgit 1.4.1