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

//! This module is responsible for the config passed to trixy.
//! It works using the popular builder syntax:
//! ```
//! use crate::macros::config::{Language, TrixyConfig};
//!# fn main() {
//! let config = TrixyConfig::new()
//!     .trixy_path("path/to/trixy/api.tri")
//!     .output_path("api.rs")
//!     .languages(vec![Language::Rust, Language::C])
//!# }
//! ```

use std::{env, path::PathBuf};

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum Language {
    Rust,
    C,
    Lua,
    #[default]
    All,
}

#[derive(Default, Debug)]
pub struct TrixyConfig {
    /// The Path to the base command interface config file.
    pub trixy_path: PathBuf,

    /// The name of the outputted host code (rust and c bindings).
    /// This file is written in $OUT_DIR>
    pub host_code_name: String,
    /// The name of the c header.
    /// This file is written in $OUT_DIR.
    pub c_header_name: String,

    /// Whether to add the pre-written c headers to the output
    pub add_c_headers: bool,

    /// The path from the root to the distribution directory.
    /// Things like the c headers are copied in this dir.
    /// When this is [None] no dist dir will be generated.
    pub dist_dir_path: Option<PathBuf>,

    // /// Whether to check if the dist dir is empty before writing something to it.
    // pub check_dist_dir: bool,
    /// This function is executed whenever an API function is called.
    /// The only argument is the command (encoded as the `Commands`) enum
    /// which is represented by this function.
    pub callback_function: String,

    /// This path is used to place the outputted host code files.
    /// Normally this would be the `$OUT_DIR` environment variable set by cargo at build time.
    /// You have to set this, if you want to use trixy in an other context.
    pub out_dir_path: PathBuf,

    /// Generate auxiliary files
    pub generate_auxiliary: bool,

    /// Generate host files
    pub generate_host: bool,
}

impl TrixyConfig {
    pub fn new<T: Into<String>>(callback_function: T) -> Self {
        let out_dir;
        if let Ok(out_dir_new) = env::var("OUT_DIR") {
            let out_path = PathBuf::from(out_dir_new);
            out_dir = out_path;
        } else {
            out_dir = PathBuf::default();
        };

        Self {
            callback_function: callback_function.into(),
            host_code_name: "api.rs".into(),
            c_header_name: "interface.h".into(),
            out_dir_path: out_dir,
            add_c_headers: true,
            generate_auxiliary: true,
            generate_host: true,
            ..Default::default()
        }
    }

    pub fn trixy_path<T: Into<PathBuf>>(self, input_path: T) -> Self {
        Self {
            trixy_path: input_path.into(),
            ..self
        }
    }

    pub fn add_c_headers<T: Into<bool>>(self, add_c_headers: T) -> Self {
        Self {
            add_c_headers: add_c_headers.into(),
            ..self
        }
    }

    pub fn generate_host<T: Into<bool>>(self, host: T) -> Self {
        Self {
            generate_host: host.into(),
            ..self
        }
    }
    pub fn generate_auxiliary<T: Into<bool>>(self, auxiliary: T) -> Self {
        Self {
            generate_auxiliary: auxiliary.into(),
            ..self
        }
    }

    pub fn dist_dir_path<T: Into<PathBuf>>(self, dist_dir_path: T) -> Self {
        Self {
            dist_dir_path: Some(dist_dir_path.into()),
            ..self
        }
    }

    pub fn host_code_name<T: Into<String>>(self, output_path: T) -> Self {
        Self {
            host_code_name: output_path.into(),
            ..self
        }
    }

    pub fn out_dir_path<T: Into<PathBuf>>(self, out_dir: T) -> Self {
        Self {
            out_dir_path: out_dir.into(),
            ..self
        }
    }

    pub fn c_header_name<T: Into<String>>(self, output_path: T) -> Self {
        Self {
            c_header_name: output_path.into(),
            ..self
        }
    }

    pub fn callback_function<T: Into<String>>(self, callback_function: T) -> Self {
        Self {
            callback_function: callback_function.into(),
            ..self
        }
    }
}