1
0
Fork 0

Fix plain #[nested] in Params proc macro

This is a regression from 727d88c4d7.
Fixes #51.
This commit is contained in:
Robbert van der Helm 2023-01-15 02:12:07 +01:00
parent 1c80331829
commit b8cb2837d5
2 changed files with 55 additions and 27 deletions

View file

@ -139,6 +139,8 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
let mut nested_id_prefix: Option<syn::LitStr> = None; let mut nested_id_prefix: Option<syn::LitStr> = None;
let mut nested_group: Option<syn::LitStr> = None; let mut nested_group: Option<syn::LitStr> = None;
match attr.parse_meta() { match attr.parse_meta() {
// In this case it's a plain `#[nested]` attribute without parameters
Ok(syn::Meta::Path(..)) => (),
Ok(syn::Meta::List(syn::MetaList { Ok(syn::Meta::List(syn::MetaList {
nested: nested_attrs, nested: nested_attrs,
.. ..
@ -205,6 +207,17 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
} }
} }
} }
}
_ => {
return syn::Error::new(
attr.span(),
"The nested attribute should be a list in the following format: \
#[nested([array | id_prefix = \"foo\"], [group = \"group name\"])]",
)
.to_compile_error()
.into()
}
};
params.push(Param::Nested(match (nested_array, nested_id_prefix) { params.push(Param::Nested(match (nested_array, nested_id_prefix) {
(true, None) => NestedParams::Array { (true, None) => NestedParams::Array {
@ -232,17 +245,6 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
processed_attribute = true; processed_attribute = true;
} }
_ => {
return syn::Error::new(
attr.span(),
"The nested attribute should be a list in the following format: \
#[nested([array | id_prefix = \"foo\"], [group = \"group name\"])]",
)
.to_compile_error()
.into()
}
};
}
} }
} }
@ -517,7 +519,7 @@ impl NestedParams {
}) })
}, },
NestedParams::Inline { field, group: None } => quote! { NestedParams::Inline { field, group: None } => quote! {
self.#field.param_map(); self.#field.param_map()
}, },
NestedParams::Prefixed { NestedParams::Prefixed {
field, field,

View file

@ -48,6 +48,13 @@ impl Default for GroupedParams {
} }
} }
// This should result in the same `.param_map()` as `GroupedParams`
#[derive(Default, Params)]
struct PlainNestedParams {
#[nested]
pub inner: GroupedParams,
}
#[derive(Default, Params)] #[derive(Default, Params)]
struct GroupedGroupedParams { struct GroupedGroupedParams {
#[nested(group = "Top-level group")] #[nested(group = "Top-level group")]
@ -134,6 +141,25 @@ mod param_order {
); );
} }
#[test]
fn plain_nested() {
let plain_nested = PlainNestedParams::default();
let grouped = GroupedParams::default();
let plain_nested_ids_groups: Vec<(String, String)> = plain_nested
.param_map()
.into_iter()
.map(|(id, _, group)| (id, group))
.collect();
let grouped_param_ids_groups: Vec<(String, String)> = grouped
.param_map()
.into_iter()
.map(|(id, _, group)| (id, group))
.collect();
assert_eq!(plain_nested_ids_groups, grouped_param_ids_groups);
}
#[test] #[test]
fn grouped_groups() { fn grouped_groups() {
let p = GroupedGroupedParams::default(); let p = GroupedGroupedParams::default();