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

mod command_enum;
mod lua_wrapper;

pub use command_enum::command_enum;
pub use lua_wrapper::lua_wrapper;
use syn::{ReturnType, Type, TypeBareFn};

use crate::command_enum_parsing::FunctionDeclaration;

pub fn get_bare_fn_input_type(function: &TypeBareFn) -> Option<Type> {
    if function.inputs.len() == 1 {
        Some(
            function
                .inputs
                .first()
                .expect("Only one element exists, we checked the length above")
                .ty
                .clone(),
        )
    } else if function.inputs.len() == 0 {
        // No inputs, so we can't return a type
        None
    } else {
        unreachable!(
            "The Function can only take one or zero arguments.
             Use a tuple `(arg1, arg2)` if you want more"
        );
    }
}

pub fn get_input_type_of_bare_fn_field(field: &FunctionDeclaration) -> Option<Type> {
    match &field.ty {
        syn::Type::BareFn(function) => get_bare_fn_input_type(&function),
        _ => unimplemented!(
            "Please specify the type as a bare fn type.
            That is: `fn(<args>) -> <outputs>`"
        ),
    }
}
pub fn get_return_type_of_bare_fn_field(field: &FunctionDeclaration) -> Option<Type> {
    match &field.ty {
        syn::Type::BareFn(function) => get_bare_fn_return_type(&function),
        _ => unimplemented!(
            "Please specify the type as a bare fn type.
            That is: `fn(<args>) -> <outputs>`"
        ),
    }
}

pub fn get_bare_fn_return_type(function: &TypeBareFn) -> Option<Type> {
    let return_path: &ReturnType = &function.output;
    match return_path {
        ReturnType::Default => None,
        ReturnType::Type(_, return_type) => Some(*return_type.to_owned()),
    }
}