1
0
Fork 0

Change the id attribute to be a key value pair

This commit is contained in:
Robbert van der Helm 2022-01-29 20:40:14 +01:00
parent 2a1fdb5d9f
commit 6fc4d80483
2 changed files with 25 additions and 24 deletions

View file

@ -37,32 +37,33 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
// exclusive with this id attribute // exclusive with this id attribute
let mut id_attr = None; let mut id_attr = None;
for attr in field.attrs { for attr in field.attrs {
match attr.parse_meta() { if attr.path.is_ident("id") {
Ok(syn::Meta::List(list)) if list.path.is_ident("id") => { match attr.parse_meta() {
if id_attr.is_none() { Ok(syn::Meta::NameValue(syn::MetaNameValue {
id_attr = Some(list); lit: syn::Lit::Str(s),
} else { ..
return syn::Error::new(attr.span(), "Duplicate id attribute") })) => {
.to_compile_error() if id_attr.is_none() {
.into(); id_attr = Some(s.value());
} else {
return syn::Error::new(attr.span(), "Duplicate id attribute")
.to_compile_error()
.into();
}
} }
} _ => {
_ => (), return syn::Error::new(
}; attr.span(),
"The id attribute should be a key-value pair with a string argument: #[id = \"foo_bar\"]",
)
.to_compile_error()
.into()
}
};
}
} }
if let Some(list) = id_attr { if let Some(param_id) = id_attr {
let param_id =
match list.nested.first() {
Some(syn::NestedMeta::Lit(syn::Lit::Str(s))) => s.value(),
_ => return syn::Error::new(
list.span(),
"The id attribute should have a single string argument: #[id(\"foo_bar\")]",
)
.to_compile_error()
.into(),
};
// The specific parameter types know how to convert themselves into the correct ParamPtr // The specific parameter types know how to convert themselves into the correct ParamPtr
// variant // variant
param_insert_tokens param_insert_tokens

View file

@ -31,7 +31,7 @@ struct Gain {
#[derive(Params)] #[derive(Params)]
struct GainParams { struct GainParams {
#[id("gain")] #[id = "gain"]
pub gain: FloatParam, pub gain: FloatParam,
} }