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 proc_macro2::TokenStream as TokenStream2;
use quote::quote;

use crate::parser::command_spec::{Enumeration, Identifier, Namespace, Structure};

impl Namespace {
    pub fn to_rust_module(&self, namespaces: &Vec<&Identifier>) -> TokenStream2 {
        let mut namespaces = namespaces.clone();
        namespaces.push(&self.name);
        let ident = self.name.to_rust();
        let enum_ident = self.name.to_rust_pascalized();

        let doc_comments: TokenStream2 = self
            .attributes
            .iter()
            .map(|attr| attr.to_rust(&self.name))
            .collect();
        let structures: TokenStream2 = self.structures.iter().map(Structure::to_rust).collect();
        let enumerations: TokenStream2 =
            self.enumerations.iter().map(Enumeration::to_rust).collect();
        let functions: Vec<TokenStream2> = self
            .functions
            .iter()
            .map(|r#fn| r#fn.to_rust(&namespaces))
            .collect();
        let namespace_modules: Vec<TokenStream2> = self
            .namespaces
            .iter()
            .map(Self::to_rust_module_enum)
            .collect();
        let namespaces: TokenStream2 = self
            .namespaces
            .iter()
            .map(|nasp| nasp.to_rust_module(&namespaces))
            .collect();

        quote! {
            #doc_comments
            pub mod #ident {
                #structures
                #enumerations
                #[derive(Debug)]
                pub enum #enum_ident {
                    #(#functions,)*
                    #(#namespace_modules),*
                }
                #namespaces
            }
        }
    }

    pub fn to_rust_module_enum(&self) -> TokenStream2 {
        let pascal_ident = self.name.to_rust_pascalized();
        let ident = self.name.to_rust();
        quote! {
            #pascal_ident(#ident :: #pascal_ident)
        }
    }
}