trixy 0.4.0

A rust crate used to generate multi-language apis for your application
Documentation
/*
* Copyright (C) 2023 - 2024:
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
* SPDX-License-Identifier: GPL-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 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 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},
};

use super::trixy::Language;

pub mod markdown_representation;

/// 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 host_files: Vec<GeneratedFile>,

    /// Files, that should be shared with the consumer (e. g. c headers).
    pub auxiliary_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,

    /// The language this file is written in.
    ///
    /// # Note
    /// This can also be a associated language,
    /// like having this set to [`Language::C`] for a c header file.
    pub language: Language,
}

impl GeneratedFile {
    pub fn new(path: PathBuf, value: String, language: Language) -> Self {
        Self {
            path,
            value,
            language,
        }
    }
    pub fn new_in_out_dir(name: String, value: String, language: Language, out_dir: &Path) -> Self {
        let path = out_dir.join(name);
        Self {
            path,
            value,
            language,
        }
    }

    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_host_file(&mut self, file: GeneratedFile) {
        self.host_files.push(file)
    }

    pub fn add_auxiliary_file(&mut self, file: GeneratedFile) {
        self.auxiliary_files.push(file)
    }

    pub fn extend_host(&mut self, files: Vec<GeneratedFile>) {
        files.into_iter().for_each(|file| self.add_host_file(file));
    }

    pub fn extend_auxiliary(&mut self, files: Vec<GeneratedFile>) {
        files
            .into_iter()
            .for_each(|file| self.add_auxiliary_file(file));
    }

    pub fn materialize(self) -> io::Result<()> {
        self.host_files
            .into_iter()
            .map(|file| -> io::Result<()> { file.materialize() })
            .collect::<io::Result<()>>()?;
        self.auxiliary_files
            .into_iter()
            .map(|file| -> io::Result<()> { file.materialize() })
            .collect::<io::Result<()>>()?;
        Ok(())
    }
}