Skip to content

Commit

Permalink
Merge pull request #18 from Cakefish/fix-default-attribute
Browse files Browse the repository at this point in the history
Fix default attribute
  • Loading branch information
IsseW authored Nov 27, 2023
2 parents acb1973 + eabfbb1 commit ed39979
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
14 changes: 12 additions & 2 deletions bauble_macro_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ struct TypeInfo<'a> {
ty: TokenStream,
/// The type's generics
impl_generics: &'a ImplGenerics<'a>,
has_generics: bool,
where_clause: &'a WhereClause,
}

Expand All @@ -219,6 +220,7 @@ fn derive_fields(
TypeInfo {
ty,
impl_generics,
has_generics,
where_clause,
}: TypeInfo,
// The struct or variant's fields
Expand Down Expand Up @@ -395,9 +397,15 @@ fn derive_fields(
// Generate code that evaluates each field
// TODO The way `impl_generics` is used here prevents the user from adding bounds directly on
// the type parameters

let fields = fields.iter().map(|field| {
let ident = &field.name;
let default = format_ident!("default_{ident}");
let default_call = if has_generics {
quote! { #default::#impl_generics() }
} else {
quote! { #default() }
};
match (&field.ty, flatten) {
(
FieldTy::Val {
Expand Down Expand Up @@ -438,7 +446,7 @@ fn derive_fields(
allocator,
::bauble::FromBauble::from_bauble(value, allocator)?,
)?,
None => #default::#impl_generics(),
None => #default_call,
}
},
(
Expand All @@ -463,7 +471,7 @@ fn derive_fields(
::bauble::FromBauble::from_bauble(values.#index, allocator)?
)?
},
(FieldTy::AsDefault { .. }, _) => quote! { #ident: #default::#impl_generics() },
(FieldTy::AsDefault { .. }, _) => quote! { #ident: #default_call },
}
});

Expand Down Expand Up @@ -730,6 +738,7 @@ pub fn derive_bauble_derive_input(
TypeInfo {
ty: quote! { Self },
impl_generics: &impl_generics,
has_generics: generics.params.len() > 1,
where_clause: &where_clause,
},
&fields,
Expand Down Expand Up @@ -854,6 +863,7 @@ pub fn derive_bauble_derive_input(
TypeInfo {
ty: quote! { Self::#ident },
impl_generics: &impl_generics,
has_generics: generics.params.len() > 1,
where_clause: &where_clause,
},
&fields,
Expand Down
26 changes: 26 additions & 0 deletions bauble_macros/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,30 @@ fn test_flattened() {
Ok(Test::Bar { x: true }),
simple_convert("test = true", "test", &bauble::DefaultAllocator)
);

#[derive(FromBauble, PartialEq, Debug)]
#[bauble(flatten)]
struct Test2 {
#[bauble(attribute, default)]
count: u32,
name: String,
}
assert_eq!(
Ok(Test2 {
count: 10,
name: "foo".to_string()
}),
simple_convert(
"test = #[count = 10] \"foo\"",
"test",
&bauble::DefaultAllocator
)
);
assert_eq!(
Ok(Test2 {
count: 0,
name: "bar".to_string()
}),
simple_convert("test = \"bar\"", "test", &bauble::DefaultAllocator)
);
}

0 comments on commit ed39979

Please sign in to comment.