From: Wolfgang Bumiller Date: Fri, 19 Jul 2019 13:17:25 +0000 (+0200) Subject: macro: partial support for automatic Default derivation X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=8523e03486cc5e87a32ac59a880a6e5881ba9f06;p=proxmox.git macro: partial support for automatic Default derivation Signed-off-by: Wolfgang Bumiller --- diff --git a/proxmox-api-macro/src/api_macro.rs b/proxmox-api-macro/src/api_macro.rs index 27720f3e..7f5170b8 100644 --- a/proxmox-api-macro/src/api_macro.rs +++ b/proxmox-api-macro/src/api_macro.rs @@ -488,6 +488,12 @@ fn handle_struct_named( .ok_or_else(|| c_format_err!(definition.span(), "missing 'fields' entry"))? .expect_object()?; + let derive_default = definition + .remove("derive_default") + .map(|e| e.expect_lit_bool_direct()) + .transpose()? + .unwrap_or(false); + let field_count = item.named.len(); let type_s = type_ident.to_string(); @@ -506,6 +512,7 @@ fn handle_struct_named( let mut field_name_matches = TokenStream::new(); // ` "member0" => 0, "member1" => 1, ` let mut field_value_matches = TokenStream::new(); let mut auto_methods = TokenStream::new(); + let mut default_impl = TokenStream::new(); let mut mem_id: isize = 0; for field in item.named.iter() { @@ -576,9 +583,31 @@ fn handle_struct_named( ::proxmox::api::meta::OrDefault::set(&mut self.#field_ident, value) } }); + + default_impl.extend(quote_spanned! { field_span => + #field_ident: #default.into(), + }); + } else { + if derive_default { + default_impl.extend(quote_spanned! { field_span => + #field_ident: Default::default(), + }); + } } } + if derive_default { + default_impl = quote_spanned! { item.span() => + impl ::std::default::Default for #type_ident { + fn default() -> Self { + Self { + #default_impl + } + } + } + }; + } + let description = common.description; let parse_cli = common.cli.quote(&type_ident); Ok(quote_spanned! { item.span() => @@ -693,6 +722,8 @@ fn handle_struct_named( } } + #default_impl + impl #type_ident { #auto_methods } diff --git a/proxmox-api-macro/src/parsing.rs b/proxmox-api-macro/src/parsing.rs index bb8dacef..00042317 100644 --- a/proxmox-api-macro/src/parsing.rs +++ b/proxmox-api-macro/src/parsing.rs @@ -246,6 +246,10 @@ impl Expression { } } + pub fn expect_lit_bool_direct(self) -> Result { + Ok(self.expect_lit_bool()?.value) + } + pub fn expect_expr(self) -> Result { match self { Expression::Expr(expr) => Ok(expr),