From 9ec0c22e4ebcc20428fee515a69a806a82045454 Mon Sep 17 00:00:00 2001 From: Soispha Date: Sun, 24 Mar 2024 21:03:33 +0100 Subject: [PATCH] feat(macros/c_api/header): Add structs and enums to the c header --- .../src/generate/c_api/header/pure_header.rs | 18 +++++++++++++++--- .../src/generate/c_api/header/structs_init.rs | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/trixy-macros/src/generate/c_api/header/pure_header.rs b/trixy-macros/src/generate/c_api/header/pure_header.rs index 0137b35..7885019 100644 --- a/trixy-macros/src/generate/c_api/header/pure_header.rs +++ b/trixy-macros/src/generate/c_api/header/pure_header.rs @@ -29,7 +29,7 @@ use trixy_parser::command_spec::{ use crate::generate::{ c_api::{ header::{attribute_to_doc_comment, named_type_to_c, type_to_c}, - identifier_to_c, mangle_c_function_ident, + identifier_to_c, mangle_c_function_identifier, }, identifier_to_rust, }; @@ -138,7 +138,7 @@ fn function_to_header(function: &Function, namespaces: &[&Identifier]) -> String .iter() .map(attribute_to_doc_comment) .collect::(); - let ident = mangle_c_function_ident(function, namespaces); + let ident = mangle_c_function_identifier(&function.identifier, namespaces); let inputs: Vec = function.inputs.iter().map(named_type_to_c).collect(); let function_output = if let Some(out) = &function.output { @@ -166,6 +166,18 @@ fn namespace_to_header(nasp: &Namespace, namespaces: &Vec<&Identifier>) -> Strin let mut nasps = namespaces.clone(); nasps.push(&nasp.name); + let structures: String = nasp + .structures + .iter() + .map(|r#fn| structure_to_header(r#fn)) + .collect::>() + .join("\n"); + let enumerations: String = nasp + .enumerations + .iter() + .map(|r#fn| enumeration_to_header(r#fn)) + .collect::>() + .join("\n"); let functions: String = nasp .functions .iter() @@ -178,5 +190,5 @@ fn namespace_to_header(nasp: &Namespace, namespaces: &Vec<&Identifier>) -> Strin .map(|nasp| namespace_to_header(nasp, &nasps)) .collect(); - format! {"{}\n{}", functions, namespaces} + format! {"{}\n{}\n{}\n{}", enumerations, structures, functions, namespaces} } diff --git a/trixy-macros/src/generate/c_api/header/structs_init.rs b/trixy-macros/src/generate/c_api/header/structs_init.rs index 8b974cd..8aa1482 100644 --- a/trixy-macros/src/generate/c_api/header/structs_init.rs +++ b/trixy-macros/src/generate/c_api/header/structs_init.rs @@ -24,7 +24,7 @@ use quote::format_ident; use quote::quote; use trixy_parser::command_spec::{CommandSpec, Function, Identifier, Namespace}; -use crate::generate::{c_api::mangle_c_function_ident, identifier_to_rust}; +use crate::generate::{c_api::mangle_c_function_identifier, identifier_to_rust}; pub fn generate(trixy: &CommandSpec) -> String { let struct_initializer: TokenStream2 = trixy @@ -69,7 +69,7 @@ fn namespace_to_full_struct_init(nasp: &Namespace, namespaces: &Vec<&Identifier> } fn function_to_struct_init(function: &Function, namespaces: &[&Identifier]) -> TokenStream2 { let ident = identifier_to_rust(&function.identifier); - let full_ident = mangle_c_function_ident(function, namespaces); + let full_ident = mangle_c_function_identifier(&function.identifier, namespaces); quote! { . #ident = #full_ident,