refactor: Replace Vec in options metadata with static array (#3433)

This commit is contained in:
Micha Reiser 2023-03-11 10:03:56 +01:00 committed by GitHub
parent 1e081cf9a6
commit cc8b13d3a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 204 additions and 81 deletions

View file

@ -44,15 +44,17 @@ pub fn derive_impl(input: DeriveInput) -> syn::Result<proc_macro2::TokenStream>
};
}
Ok(quote! {
use crate::settings::options_base::{OptionEntry, OptionField, ConfigurationOptions};
let options_len = output.len();
#[automatically_derived]
impl ConfigurationOptions for #ident {
fn get_available_options() -> Vec<(&'static str, OptionEntry)> {
vec![#(#output),*]
}
}
Ok(quote! {
use crate::settings::options_base::{OptionEntry, OptionField, OptionGroup};
impl #ident {
pub const fn metadata() -> OptionGroup {
const OPTIONS: [(&'static str, OptionEntry); #options_len] = [#(#output),*];
OptionGroup::new(&OPTIONS)
}
}
})
}
_ => Err(syn::Error::new(
@ -86,7 +88,7 @@ fn handle_option_group(field: &Field) -> syn::Result<proc_macro2::TokenStream> {
let kebab_name = LitStr::new(&ident.to_string().replace('_', "-"), ident.span());
Ok(quote_spanned!(
ident.span() => (#kebab_name, OptionEntry::Group(#path::get_available_options()))
ident.span() => (#kebab_name, OptionEntry::Group(#path::metadata()))
))
}
_ => Err(syn::Error::new(