Skip to content

Commit

Permalink
generate enums with values for A2ML if possible
Browse files Browse the repository at this point in the history
If each item in the A2ML enum has a value (e.g. OPTIONAL_CMD), then the generated Rust enum also uses these values.
  • Loading branch information
DanielT committed Sep 2, 2024
1 parent e7b3a4e commit f18ab27
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions a2lmacros/src/codegenerator/data_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,33 @@ pub(crate) fn generate(typename: &str, dataitem: &DataItem) -> TokenStream {
fn generate_enum_data_structure(typename: &str, enumitems: &[EnumItem]) -> TokenStream {
let typeident = format_ident!("{}", typename);

let enumidents: Vec<proc_macro2::Ident> = enumitems
.iter()
.map(|enumitem| format_ident!("{}", ucname_to_typename(&enumitem.name)))
.collect();
// check if all enum items have a value assigned
let use_values = enumitems.iter().all(|item| item.value.is_some());
let enum_defs: Vec<TokenStream> = if !use_values {
// if no values are assigned, the enum items are just listed without values
enumitems
.iter()
.map(|enumitem| {
let ident = format_ident!("{}", ucname_to_typename(&enumitem.name));
quote! {#ident}
})
.collect()
} else {
// if values are assigned, the enum items are listed with values
enumitems
.iter()
.map(|enumitem| {
let ident = format_ident!("{}", ucname_to_typename(&enumitem.name));
let value = *enumitem.value.as_ref().unwrap() as isize;
quote! {#ident = #value}
})
.collect()
};

quote! {
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum #typeident {
#(#enumidents),*
#(#enum_defs),*
}
}
}
Expand Down

0 comments on commit f18ab27

Please sign in to comment.