feat(macros/c_api/header): Add structs and enums to the c header

This commit is contained in:
Benedikt Peetz 2024-03-24 21:03:33 +01:00
parent 1c72309677
commit 9ec0c22e4e
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
2 changed files with 17 additions and 5 deletions

View File

@ -29,7 +29,7 @@ use trixy_parser::command_spec::{
use crate::generate::{ use crate::generate::{
c_api::{ c_api::{
header::{attribute_to_doc_comment, named_type_to_c, type_to_c}, 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, identifier_to_rust,
}; };
@ -138,7 +138,7 @@ fn function_to_header(function: &Function, namespaces: &[&Identifier]) -> String
.iter() .iter()
.map(attribute_to_doc_comment) .map(attribute_to_doc_comment)
.collect::<String>(); .collect::<String>();
let ident = mangle_c_function_ident(function, namespaces); let ident = mangle_c_function_identifier(&function.identifier, namespaces);
let inputs: Vec<TokenStream2> = function.inputs.iter().map(named_type_to_c).collect(); let inputs: Vec<TokenStream2> = function.inputs.iter().map(named_type_to_c).collect();
let function_output = if let Some(out) = &function.output { 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(); let mut nasps = namespaces.clone();
nasps.push(&nasp.name); nasps.push(&nasp.name);
let structures: String = nasp
.structures
.iter()
.map(|r#fn| structure_to_header(r#fn))
.collect::<Vec<String>>()
.join("\n");
let enumerations: String = nasp
.enumerations
.iter()
.map(|r#fn| enumeration_to_header(r#fn))
.collect::<Vec<String>>()
.join("\n");
let functions: String = nasp let functions: String = nasp
.functions .functions
.iter() .iter()
@ -178,5 +190,5 @@ fn namespace_to_header(nasp: &Namespace, namespaces: &Vec<&Identifier>) -> Strin
.map(|nasp| namespace_to_header(nasp, &nasps)) .map(|nasp| namespace_to_header(nasp, &nasps))
.collect(); .collect();
format! {"{}\n{}", functions, namespaces} format! {"{}\n{}\n{}\n{}", enumerations, structures, functions, namespaces}
} }

View File

@ -24,7 +24,7 @@ use quote::format_ident;
use quote::quote; use quote::quote;
use trixy_parser::command_spec::{CommandSpec, Function, Identifier, Namespace}; 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 { pub fn generate(trixy: &CommandSpec) -> String {
let struct_initializer: TokenStream2 = trixy 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 { fn function_to_struct_init(function: &Function, namespaces: &[&Identifier]) -> TokenStream2 {
let ident = identifier_to_rust(&function.identifier); 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! { quote! {
. #ident = #full_ident, . #ident = #full_ident,