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/>.
*/

use std::io::Write;
use std::process::{Command, Stdio};

use crate::{
    macros::{config::trixy::TrixyConfig, VIM_LINE_C},
    parser::command_spec::CommandSpec,
};

pub mod c;

// FIXME(@soispha): We made to promise to not panic (outside of the toplevel generate function).
// Therefore these panics here should be avoided. <2024-03-25>
pub fn format_c(input: String) -> String {
    let mut clang_format = Command::new("clang-format")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .args(["--style", "GNU"])
        .spawn()
        .expect("`clang-format` is a dependency and should be provided by the user");

    let mut stdin = clang_format
        .stdin
        .take()
        .expect("We should be able to take stdin");
    std::thread::spawn(move || {
        stdin
            .write_all(input.as_bytes())
            .expect("Should be able to write to stdin");
    });

    let output = clang_format
        .wait_with_output()
        .expect("Should be able to read stdout");
    String::from_utf8(output.stdout).expect("The input was utf8, it should not have changed")
}

pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
    let c_code = c::generate(trixy, config);
    let c_code = format_c(c_code);

    format!(
        "\
{}\n\
{}",
        c_code, VIM_LINE_C
    )
}