started default function
This commit is contained in:
@@ -83,6 +83,43 @@ pub fn gen_to_string(name: &Ident, fields: &Punctuated<Field, Comma>) -> TokenSt
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_default(fields: &Punctuated<Field, Comma>) -> TokenStream {
|
||||
let mut defaults: TokenStream = TokenStream::new();
|
||||
'fields: for field in fields.iter() {
|
||||
let attr = &field.attrs;
|
||||
let name = match &field.ident {
|
||||
Some(value) => value,
|
||||
// skip anonymous fields
|
||||
None => continue 'fields,
|
||||
};
|
||||
let ty = &field.ty;
|
||||
for attribute in attr {
|
||||
if let Attribute{ meta: Meta::Path( Path{segments: attr_name, ..} ), .. } = attribute {
|
||||
match attr_name.first() {
|
||||
Some(value) => if value.ident == "default_value" {
|
||||
let default_value = get_attribute_value(attribute);
|
||||
defaults.extend(quote! {
|
||||
#name: #default_value,
|
||||
});
|
||||
} else if value.ident == "nested_config" {
|
||||
defaults.extend(quote! {
|
||||
#name: #ty.default()?;
|
||||
});
|
||||
},
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
quote!{
|
||||
/// returns an instance with default values
|
||||
pub fn default(&mut self) -> std::io::Result<()> {
|
||||
#defaults
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_config_assignments(fields: &Punctuated<Field, Comma>, config_map_name: &syn::Ident) -> TokenStream {
|
||||
let mut assignments : TokenStream = TokenStream::new();
|
||||
'fields: for field in fields.iter() {
|
||||
@@ -127,3 +164,7 @@ pub fn gen_config_assignments(fields: &Punctuated<Field, Comma>, config_map_name
|
||||
assignments
|
||||
}
|
||||
|
||||
fn get_attribute_value(attribute: &Attribute) -> TokenStream {
|
||||
//if let Attribute{ Meta::List }
|
||||
TokenStream::new()
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use generator_functions::*;
|
||||
/// which parses a string and fills the fills recognised values into the struct
|
||||
/// - implements `write_default_config() -> Result<String>`
|
||||
/// which write a default configuration, in case the documentation is lacking
|
||||
#[proc_macro_derive(ConfigParser, attributes(nested_config, no_config, style_config))]
|
||||
#[proc_macro_derive(ConfigParser, attributes(default_value, nested_config, no_config, style_config))]
|
||||
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let name = &ast.ident;
|
||||
@@ -21,6 +21,9 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
} else {
|
||||
panic!("the macro `ConfigParser` applies only to structs!");
|
||||
};
|
||||
if name.to_string() == "GeneralSettings" {
|
||||
println!("ast = {:#?}", ast);
|
||||
}
|
||||
let assignments: TokenStream = gen_config_assignments(fields, &config_name);
|
||||
let func_parse_string: TokenStream = gen_parse_from_string(&config_name, &assignments);
|
||||
let func_parse_map: TokenStream = gen_parse_from_map(&config_name, &assignments);
|
||||
|
||||
Reference in New Issue
Block a user