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,32 +207,6 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
} }
} }
} }
params.push(Param::Nested(match (nested_array, nested_id_prefix) {
(true, None) => NestedParams::Array {
field: field_name.clone(),
group: nested_group,
},
(false, Some(id_prefix)) => NestedParams::Prefixed {
field: field_name.clone(),
id_prefix,
group: nested_group,
},
(false, None) => NestedParams::Inline {
field: field_name.clone(),
group: nested_group,
},
(true, Some(_)) => {
return syn::Error::new(
attr.span(),
"'array' cannot be used together with 'id_prefix'",
)
.to_compile_error()
.into()
}
}));
processed_attribute = true;
} }
_ => { _ => {
return syn::Error::new( return syn::Error::new(
@ -242,6 +218,32 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
.into() .into()
} }
}; };
params.push(Param::Nested(match (nested_array, nested_id_prefix) {
(true, None) => NestedParams::Array {
field: field_name.clone(),
group: nested_group,
},
(false, Some(id_prefix)) => NestedParams::Prefixed {
field: field_name.clone(),
id_prefix,
group: nested_group,
},
(false, None) => NestedParams::Inline {
field: field_name.clone(),
group: nested_group,
},
(true, Some(_)) => {
return syn::Error::new(
attr.span(),
"'array' cannot be used together with 'id_prefix'",
)
.to_compile_error()
.into()
}
}));
processed_attribute = true;
} }
} }
} }
@ -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();