mirror of
https://github.com/robbert-vdh/nih-plug.git
synced 2026-07-01 02:36:54 +00:00
Add ID renaming superpowers to #[nested(...)]
This can now be used for most common use cases where you previously had to do a manual `Params` implementation, like arrays of parameter objects and duplicate parameter objects.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use std::collections::HashSet;
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
pub fn derive_params(input: TokenStream) -> TokenStream {
|
||||
@@ -28,29 +27,22 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
|
||||
// parameters. For the `persist` function we'll create functions that serialize and deserialize
|
||||
// those fields individually (so they can be added and removed independently of eachother) using
|
||||
// JSON. The `nested` fields should also implement the `Params` trait and their fields will be
|
||||
// inherited and added to this field's lists.
|
||||
let mut param_mapping_self_tokens = Vec::new();
|
||||
let mut field_serialize_tokens = Vec::new();
|
||||
let mut field_deserialize_tokens = Vec::new();
|
||||
let mut nested_params_field_idents: Vec<syn::Ident> = Vec::new();
|
||||
let mut nested_params_group_names: Vec<String> = Vec::new();
|
||||
|
||||
// We'll also enforce that there are no duplicate keys at compile time
|
||||
// TODO: This doesn't work for nested fields since we don't know anything about the fields on
|
||||
// the nested structs
|
||||
let mut param_ids = HashSet::new();
|
||||
let mut persist_ids = HashSet::new();
|
||||
// inherited and added to this field's lists. We'll also enforce that there are no duplicate
|
||||
// keys at compile time.
|
||||
// TODO: This duplication check doesn't work for nested fields since we don't know anything
|
||||
// about the fields on the nested structs
|
||||
let mut params: Vec<Param> = Vec::new();
|
||||
let mut persistent_fields: Vec<PersistentField> = Vec::new();
|
||||
let mut nested_params: Vec<NestedParams> = Vec::new();
|
||||
for field in fields.named {
|
||||
let field_name = match &field.ident {
|
||||
Some(ident) => ident,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
// These two attributes are mutually exclusive
|
||||
let mut id_attr: Option<String> = None;
|
||||
let mut persist_attr: Option<String> = None;
|
||||
// And the `#[nested = "..."]` attribute contains a group name we should use
|
||||
let mut nested_attr: Option<String> = None;
|
||||
// All attributes are mutually exclusive. If we encounter multiple or duplicate attributes,
|
||||
// then we'll error out.
|
||||
let mut processed_attribute = false;
|
||||
for attr in &field.attrs {
|
||||
if attr.path.is_ident("id") {
|
||||
match attr.parse_meta() {
|
||||
@@ -58,13 +50,33 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
|
||||
lit: syn::Lit::Str(s),
|
||||
..
|
||||
})) => {
|
||||
if id_attr.is_none() {
|
||||
id_attr = Some(s.value());
|
||||
} else {
|
||||
return syn::Error::new(attr.span(), "Duplicate id attribute")
|
||||
.to_compile_error()
|
||||
.into();
|
||||
if processed_attribute {
|
||||
return syn::Error::new(
|
||||
attr.span(),
|
||||
"Duplicate or incompatible attribute found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
// This is a vector since we want to preserve the order. If structs get
|
||||
// large enough to the point where a linear search starts being expensive,
|
||||
// then the plugin should probably start splitting up their parameters.
|
||||
if params.iter().any(|p| p.id == s) {
|
||||
return syn::Error::new(
|
||||
field.span(),
|
||||
"Multiple parameters with the same ID found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
params.push(Param {
|
||||
id: s,
|
||||
field: field_name.clone(),
|
||||
});
|
||||
|
||||
processed_attribute = true;
|
||||
}
|
||||
_ => {
|
||||
return syn::Error::new(
|
||||
@@ -82,13 +94,30 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
|
||||
lit: syn::Lit::Str(s),
|
||||
..
|
||||
})) => {
|
||||
if persist_attr.is_none() {
|
||||
persist_attr = Some(s.value());
|
||||
} else {
|
||||
return syn::Error::new(attr.span(), "Duplicate persist attribute")
|
||||
.to_compile_error()
|
||||
.into();
|
||||
if processed_attribute {
|
||||
return syn::Error::new(
|
||||
attr.span(),
|
||||
"Duplicate or incompatible attribute found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
if persistent_fields.iter().any(|p| p.key == s) {
|
||||
return syn::Error::new(
|
||||
field.span(),
|
||||
"Multiple persistent fields with the same key found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
persistent_fields.push(PersistentField {
|
||||
key: s,
|
||||
field: field_name.clone(),
|
||||
});
|
||||
|
||||
processed_attribute = true;
|
||||
}
|
||||
_ => {
|
||||
return syn::Error::new(
|
||||
@@ -101,36 +130,111 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
|
||||
}
|
||||
};
|
||||
} else if attr.path.is_ident("nested") {
|
||||
// This one is more complicated. Support an `array` attribute, an `id_prefix =
|
||||
// "foo"` attribute, and a `group = "group name"` attribute. All are optional, and
|
||||
// the first two are mutually exclusive.
|
||||
let mut nested_array = false;
|
||||
let mut nested_id_prefix: Option<syn::LitStr> = None;
|
||||
let mut nested_group: Option<syn::LitStr> = None;
|
||||
match attr.parse_meta() {
|
||||
Ok(syn::Meta::NameValue(syn::MetaNameValue {
|
||||
lit: syn::Lit::Str(s),
|
||||
Ok(syn::Meta::List(syn::MetaList {
|
||||
nested: nested_attrs,
|
||||
..
|
||||
})) => {
|
||||
let s = s.value();
|
||||
if s.is_empty() {
|
||||
return syn::Error::new(attr.span(), "Group names cannot be empty")
|
||||
.to_compile_error()
|
||||
.into();
|
||||
} else if s.contains('/') {
|
||||
if processed_attribute {
|
||||
return syn::Error::new(
|
||||
attr.span(),
|
||||
"Group names may not contain slashes",
|
||||
"Duplicate or incompatible attribute found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
} else if nested_attr.is_some() {
|
||||
return syn::Error::new(attr.span(), "Duplicate nested attribute")
|
||||
.to_compile_error()
|
||||
.into();
|
||||
} else {
|
||||
nested_attr = Some(s);
|
||||
}
|
||||
|
||||
for nested_attr in nested_attrs {
|
||||
match nested_attr {
|
||||
syn::NestedMeta::Meta(syn::Meta::Path(p))
|
||||
if p.is_ident("array") =>
|
||||
{
|
||||
nested_array = true;
|
||||
}
|
||||
syn::NestedMeta::Meta(syn::Meta::NameValue(
|
||||
syn::MetaNameValue {
|
||||
path,
|
||||
lit: syn::Lit::Str(s),
|
||||
..
|
||||
},
|
||||
)) if path.is_ident("id_prefix") => {
|
||||
nested_id_prefix = Some(s.clone());
|
||||
}
|
||||
syn::NestedMeta::Meta(syn::Meta::NameValue(
|
||||
syn::MetaNameValue {
|
||||
path,
|
||||
lit: syn::Lit::Str(s),
|
||||
..
|
||||
},
|
||||
)) if path.is_ident("group") => {
|
||||
let group_name = s.value();
|
||||
if group_name.is_empty() {
|
||||
return syn::Error::new(
|
||||
attr.span(),
|
||||
"Group names cannot be empty",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
} else if group_name.contains('/') {
|
||||
return syn::Error::new(
|
||||
attr.span(),
|
||||
"Group names may not contain slashes",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
} else {
|
||||
nested_group = Some(s.clone());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return syn::Error::new(
|
||||
nested_attr.span(),
|
||||
"Unknown attribute. See the Params trait documentation \
|
||||
for more information.",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nested_params.push(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(
|
||||
attr.span(),
|
||||
"The nested attribute should be a key-value pair with a string \
|
||||
argument: #[nested = \"Group Name\"]",
|
||||
"The nested attribute should be a list in the following format: \
|
||||
#[nested([array | id_prefix = \"foo\"], [group = \"group name\"])]",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into()
|
||||
@@ -138,159 +242,269 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
match (id_attr, persist_attr) {
|
||||
(Some(param_id), None) => {
|
||||
if !param_ids.insert(param_id.clone()) {
|
||||
return syn::Error::new(
|
||||
field.span(),
|
||||
"Multiple fields with the same parameter ID found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
// These are pairs of `(parameter_id, param_ptr, param_group)`. The specific
|
||||
// parameter types know how to convert themselves into the correct ParamPtr variant.
|
||||
// Top-level parameters have no group, and we'll prefix the group name specified in
|
||||
// the `#[nested = "..."]` attribute to fields coming from nested groups
|
||||
param_mapping_self_tokens.push(
|
||||
quote! { (String::from(#param_id), self.#field_name.as_ptr(), String::new()) },
|
||||
);
|
||||
}
|
||||
(None, Some(persist_key)) => {
|
||||
if !persist_ids.insert(persist_key.clone()) {
|
||||
return syn::Error::new(
|
||||
field.span(),
|
||||
"Multiple persisted fields with the same ID found",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
// We don't know anything about the field types, but because we can generate this
|
||||
// function we get type erasure for free since we only need to worry about byte
|
||||
// vectors
|
||||
field_serialize_tokens.push(quote! {
|
||||
match ::nih_plug::param::internals::PersistentField::map(
|
||||
&self.#field_name,
|
||||
::nih_plug::param::internals::serialize_field,
|
||||
) {
|
||||
Ok(data) => {
|
||||
serialized.insert(String::from(#persist_key), data);
|
||||
}
|
||||
Err(err) => {
|
||||
::nih_plug::nih_debug_assert_failure!(
|
||||
"Could not serialize '{}': {}",
|
||||
#persist_key,
|
||||
err
|
||||
)
|
||||
}
|
||||
};
|
||||
});
|
||||
field_deserialize_tokens.push(quote! {
|
||||
#persist_key => {
|
||||
match ::nih_plug::param::internals::deserialize_field(&data) {
|
||||
Ok(deserialized) => {
|
||||
::nih_plug::param::internals::PersistentField::set(
|
||||
&self.#field_name,
|
||||
deserialized,
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
::nih_plug::nih_debug_assert_failure!(
|
||||
"Could not deserialize '{}': {}",
|
||||
#persist_key,
|
||||
err
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
(Some(_), Some(_)) => {
|
||||
return syn::Error::new(
|
||||
field.span(),
|
||||
"The id and persist attributes are mutually exclusive",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
(None, None) => (),
|
||||
}
|
||||
|
||||
if let Some(nested_group_name) = nested_attr {
|
||||
nested_params_field_idents.push(field_name.clone());
|
||||
// FIXME: Generate the insertion code here
|
||||
nested_params_group_names.push(nested_group_name);
|
||||
}
|
||||
}
|
||||
|
||||
// The next step is build the gathered information into tokens that can be spliced into a
|
||||
// `Params` implementation
|
||||
let param_map_tokens = {
|
||||
// `param_map` adds the parameters from this struct, and then handles the nested tokens.
|
||||
let param_mapping_self_tokens = params.into_iter().map(
|
||||
|Param {field, id}| quote! { (String::from(#id), self.#field.as_ptr(), String::new()) },
|
||||
);
|
||||
|
||||
// How nested parameters are handled depends on the `NestedParams` variant.
|
||||
// These are pairs of `(parameter_id, param_ptr, param_group)`. The specific
|
||||
// parameter types know how to convert themselves into the correct ParamPtr variant.
|
||||
// Top-level parameters have no group, and we'll prefix the group name specified in
|
||||
// the `#[nested(...)]` attribute to fields coming from nested groups
|
||||
let param_mapping_nested_tokens = nested_params.iter().map(|nested| match nested {
|
||||
// TODO: No idea how to splice this as an `Option<&str>`, so this involves some
|
||||
// copy-pasting
|
||||
NestedParams::Inline { field, group: Some(group) } => quote! {
|
||||
param_map.extend(self.#field.param_map().into_iter().map(|(param_id, param_ptr, nested_group_name)| {
|
||||
if nested_group_name.is_empty() {
|
||||
(param_id, param_ptr, String::from(#group))
|
||||
} else {
|
||||
(param_id, param_ptr, format!("{}/{}", #group, nested_group_name))
|
||||
}
|
||||
}));
|
||||
},
|
||||
NestedParams::Inline { field, group: None } => quote! {
|
||||
param_map.extend(self.#field.param_map());
|
||||
},
|
||||
NestedParams::Prefixed {
|
||||
field,
|
||||
id_prefix,
|
||||
group: Some(group),
|
||||
} => quote! {
|
||||
param_map.extend(self.#field.param_map().into_iter().map(|(param_id, param_ptr, nested_group_name)| {
|
||||
let param_id = format!("{}_{}", #id_prefix, param_id);
|
||||
|
||||
if nested_group_name.is_empty() {
|
||||
(param_id, param_ptr, String::from(#group))
|
||||
} else {
|
||||
(param_id, param_ptr, format!("{}/{}", #group, nested_group_name))
|
||||
}
|
||||
}));
|
||||
},
|
||||
NestedParams::Prefixed {
|
||||
field,
|
||||
id_prefix,
|
||||
group: None,
|
||||
} => quote! {
|
||||
param_map.extend(self.#field.param_map().into_iter().map(|(param_id, param_ptr, nested_group_name)| {
|
||||
let param_id = format!("{}_{}", #id_prefix, param_id);
|
||||
|
||||
(param_id, param_ptr, nested_group_name)
|
||||
}));
|
||||
},
|
||||
// We'll start at index 1 for display purposes. Both the group and the parameter ID get
|
||||
// a suffix matching the array index.
|
||||
NestedParams::Array { field, group: Some(group) } => quote! {
|
||||
param_map.extend(self.#field.iter().enumerate().flat_map(|(idx, params)| {
|
||||
let idx = idx + 1;
|
||||
|
||||
params.param_map().into_iter().map(move |(param_id, param_ptr, nested_group_name)| {
|
||||
let param_id = format!("{}_{}", param_id, idx);
|
||||
let group = format!("{} {}", #group, idx);
|
||||
|
||||
// Note that this is different from the other variants
|
||||
if nested_group_name.is_empty() {
|
||||
(param_id, param_ptr, group)
|
||||
} else {
|
||||
(param_id, param_ptr, format!("{}/{}", group, nested_group_name))
|
||||
}
|
||||
})
|
||||
}));
|
||||
},
|
||||
NestedParams::Array { field, group: None } => quote! {
|
||||
param_map.extend(self.#field.iter().enumerate().flat_map(|(idx, params)| {
|
||||
let idx = idx + 1;
|
||||
|
||||
params.param_map().into_iter().map(move |(param_id, param_ptr, nested_group_name)| {
|
||||
let param_id = format!("{}_{}", param_id, idx);
|
||||
|
||||
(param_id, param_ptr, nested_group_name)
|
||||
})
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
quote! {
|
||||
// This may not be in scope otherwise, used to call .as_ptr()
|
||||
use ::nih_plug::param::Param;
|
||||
|
||||
#[allow(unused_mut)]
|
||||
let mut param_map = vec![#(#param_mapping_self_tokens),*];
|
||||
|
||||
#(#param_mapping_nested_tokens);*
|
||||
|
||||
param_map
|
||||
}
|
||||
};
|
||||
|
||||
let (serialize_fields_tokens, deserialize_fields_tokens) = {
|
||||
// Like with `param_map()`, we'll try to do the serialization for this struct and then
|
||||
// recursively call the child parameter structs. We don't know anything about the actual
|
||||
// field types, but because we can generate this function we can get type erasure for free
|
||||
// since we only need to worry about byte vectors.
|
||||
let (serialize_fields_self_tokens, deserialize_fields_match_self_tokens): (Vec<_>, Vec<_>) =
|
||||
persistent_fields
|
||||
.into_iter()
|
||||
.map(|PersistentField { field, key }| {
|
||||
(
|
||||
quote! {
|
||||
match ::nih_plug::param::internals::PersistentField::map(
|
||||
&self.#field,
|
||||
::nih_plug::param::internals::serialize_field,
|
||||
) {
|
||||
Ok(data) => {
|
||||
serialized.insert(String::from(#key), data);
|
||||
}
|
||||
Err(err) => {
|
||||
::nih_plug::nih_debug_assert_failure!(
|
||||
"Could not serialize '{}': {}",
|
||||
#key,
|
||||
err
|
||||
)
|
||||
}
|
||||
};
|
||||
},
|
||||
quote! {
|
||||
#key => {
|
||||
match ::nih_plug::param::internals::deserialize_field(&data) {
|
||||
Ok(deserialized) => {
|
||||
::nih_plug::param::internals::PersistentField::set(
|
||||
&self.#field,
|
||||
deserialized,
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
::nih_plug::nih_debug_assert_failure!(
|
||||
"Could not deserialize '{}': {}",
|
||||
#key,
|
||||
err
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
.unzip();
|
||||
|
||||
let (serialize_fields_nested_tokens, deserialize_fields_nested_tokens): (Vec<_>, Vec<_>) =
|
||||
nested_params
|
||||
.iter()
|
||||
.map(|nested| match nested {
|
||||
NestedParams::Inline { field, .. } | NestedParams::Prefixed { field, .. } => (
|
||||
// TODO: For some reason the macro won't parse correctly if you inline this
|
||||
quote! {
|
||||
let inlineme = self.#field.serialize_fields();
|
||||
serialized.extend(inlineme);
|
||||
},
|
||||
quote! { self.#field.deserialize_fields(serialized) },
|
||||
),
|
||||
NestedParams::Array { field, .. } => (
|
||||
quote! {
|
||||
for field in self.#field.iter() {
|
||||
serialized.extend(field.serialize_fields());
|
||||
}
|
||||
},
|
||||
quote! {
|
||||
for field in self.#field.iter() {
|
||||
field.deserialize_fields(serialized);
|
||||
}
|
||||
},
|
||||
),
|
||||
})
|
||||
.unzip();
|
||||
|
||||
let serialize_fields_tokens = quote! {
|
||||
#[allow(unused_mut)]
|
||||
let mut serialized = ::std::collections::BTreeMap::new();
|
||||
#(#serialize_fields_self_tokens);*
|
||||
|
||||
#(#serialize_fields_nested_tokens);*
|
||||
|
||||
serialized
|
||||
};
|
||||
|
||||
let deserialize_fields_tokens = quote! {
|
||||
for (field_name, data) in serialized {
|
||||
match field_name.as_str() {
|
||||
#(#deserialize_fields_match_self_tokens)*
|
||||
_ => ::nih_plug::nih_debug_assert_failure!("Unknown serialized field name: {} (this may not be accurate)", field_name),
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: The above warning will course give false postiives when using nested
|
||||
// parameter structs. An easy fix would be to use
|
||||
// https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.drain_filter
|
||||
// once that gets stabilized.
|
||||
#(#deserialize_fields_nested_tokens);*
|
||||
};
|
||||
|
||||
(serialize_fields_tokens, deserialize_fields_tokens)
|
||||
};
|
||||
|
||||
quote! {
|
||||
unsafe impl #impl_generics Params for #struct_name #ty_generics #where_clause {
|
||||
fn param_map(&self) -> Vec<(String, nih_plug::prelude::ParamPtr, String)> {
|
||||
// This may not be in scope otherwise, used to call .as_ptr()
|
||||
use ::nih_plug::param::Param;
|
||||
|
||||
let mut param_map = vec![#(#param_mapping_self_tokens),*];
|
||||
|
||||
let nested_params_fields: &[&dyn Params] = &[#(&self.#nested_params_field_idents),*];
|
||||
let nested_params_groups: &[String] = &[#(String::from(#nested_params_group_names)),*];
|
||||
for (nested_params, group_name) in
|
||||
nested_params_fields.into_iter().zip(nested_params_groups)
|
||||
{
|
||||
let nested_param_map = nested_params.param_map();
|
||||
let prefixed_nested_param_map =
|
||||
nested_param_map
|
||||
.into_iter()
|
||||
.map(|(param_id, param_ptr, nested_group_name)| {
|
||||
(
|
||||
param_id,
|
||||
param_ptr,
|
||||
if nested_group_name.is_empty() {
|
||||
group_name.to_string()
|
||||
} else {
|
||||
format!("{}/{}", group_name, nested_group_name)
|
||||
}
|
||||
)
|
||||
});
|
||||
|
||||
param_map.extend(prefixed_nested_param_map);
|
||||
}
|
||||
|
||||
param_map
|
||||
#param_map_tokens
|
||||
}
|
||||
|
||||
fn serialize_fields(&self) -> ::std::collections::BTreeMap<String, String> {
|
||||
let mut serialized = ::std::collections::BTreeMap::new();
|
||||
#(#field_serialize_tokens)*
|
||||
|
||||
let nested_params_fields: &[&dyn Params] = &[#(&self.#nested_params_field_idents),*];
|
||||
for nested_params in nested_params_fields {
|
||||
serialized.extend(nested_params.serialize_fields());
|
||||
}
|
||||
|
||||
serialized
|
||||
#serialize_fields_tokens
|
||||
}
|
||||
|
||||
fn deserialize_fields(&self, serialized: &::std::collections::BTreeMap<String, String>) {
|
||||
for (field_name, data) in serialized {
|
||||
match field_name.as_str() {
|
||||
#(#field_deserialize_tokens)*
|
||||
_ => ::nih_plug::nih_debug_assert_failure!("Unknown serialized field name: {} (this may not be accurate)", field_name),
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: The above warning will course give false postiives when using nested
|
||||
// parameter structs. An easy fix would be to use
|
||||
// https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.drain_filter
|
||||
// once that gets stabilized.
|
||||
let nested_params_fields: &[&dyn Params] = &[#(&self.#nested_params_field_idents),*];
|
||||
for nested_params in nested_params_fields {
|
||||
nested_params.deserialize_fields(serialized);
|
||||
}
|
||||
#deserialize_fields_tokens
|
||||
}
|
||||
}
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// A parameter that should be added to the parameter map.
|
||||
#[derive(Debug)]
|
||||
struct Param {
|
||||
/// The name of the parameter's field on the struct.
|
||||
field: syn::Ident,
|
||||
/// The parameter's unique ID.
|
||||
id: syn::LitStr,
|
||||
}
|
||||
|
||||
/// A field containing data that must be stored in the plugin's state.
|
||||
#[derive(Debug)]
|
||||
struct PersistentField {
|
||||
/// The name of the field on the struct.
|
||||
field: syn::Ident,
|
||||
/// The field's unique key.
|
||||
key: syn::LitStr,
|
||||
}
|
||||
|
||||
/// A field containing another object whose parameters and persistent fields should be added to this
|
||||
/// struct's.
|
||||
#[derive(Debug)]
|
||||
enum NestedParams {
|
||||
/// The nested struct's parameters are taken as is.
|
||||
Inline {
|
||||
field: syn::Ident,
|
||||
group: Option<syn::LitStr>,
|
||||
},
|
||||
/// The nested struct's parameters will get an ID prefix. The original parmaeter with ID `foo`
|
||||
/// will become `{id_prefix}_foo`.
|
||||
Prefixed {
|
||||
field: syn::Ident,
|
||||
id_prefix: syn::LitStr,
|
||||
group: Option<syn::LitStr>,
|
||||
},
|
||||
/// This field is an array-like data structure containing nested parameter structs. The
|
||||
/// parameter `foo` will get the new parameter ID `foo_{array_idx + 1}`, and if the group name
|
||||
/// is set then the group will be `{group_name} {array_idx + 1}`.
|
||||
Array {
|
||||
field: syn::Ident,
|
||||
group: Option<syn::LitStr>,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user