2022-03-17 01:53:25 +11:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use syn::spanned::Spanned;
|
|
|
|
|
|
|
|
pub fn derive_params(input: TokenStream) -> TokenStream {
|
|
|
|
let ast = syn::parse_macro_input!(input as syn::DeriveInput);
|
|
|
|
|
|
|
|
let struct_name = &ast.ident;
|
2022-03-19 00:09:17 +11:00
|
|
|
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
|
2022-03-17 01:53:25 +11:00
|
|
|
let fields = match ast.data {
|
|
|
|
syn::Data::Struct(syn::DataStruct {
|
|
|
|
fields: syn::Fields::Named(named_fields),
|
|
|
|
..
|
|
|
|
}) => named_fields,
|
|
|
|
_ => {
|
|
|
|
return syn::Error::new(
|
|
|
|
ast.span(),
|
|
|
|
"Deriving Params is only supported on structs with named fields",
|
|
|
|
)
|
|
|
|
.to_compile_error()
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// We only care about fields with `id`, `persist`, and `nested` attributes. For the `id` fields
|
|
|
|
// we'll build a mapping function that creates a hashmap containing pointers to those
|
|
|
|
// parmaeters. 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.
|
2022-03-20 23:03:03 +11:00
|
|
|
let mut param_mapping_self_tokens = Vec::new();
|
2022-03-17 01:53:25 +11:00
|
|
|
let mut field_serialize_tokens = Vec::new();
|
|
|
|
let mut field_deserialize_tokens = Vec::new();
|
2022-03-17 03:04:38 +11:00
|
|
|
let mut nested_params_field_idents: Vec<syn::Ident> = Vec::new();
|
|
|
|
let mut nested_params_group_names: Vec<String> = Vec::new();
|
2022-03-17 01:53:25 +11:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
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;
|
2022-03-17 03:04:38 +11:00
|
|
|
// And the `#[nested = "..."]` attribute contains a group name we should use
|
|
|
|
let mut nested_attr: Option<String> = None;
|
2022-03-17 01:53:25 +11:00
|
|
|
for attr in &field.attrs {
|
|
|
|
if attr.path.is_ident("id") {
|
|
|
|
match attr.parse_meta() {
|
|
|
|
Ok(syn::Meta::NameValue(syn::MetaNameValue {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if attr.path.is_ident("persist") {
|
|
|
|
match attr.parse_meta() {
|
|
|
|
Ok(syn::Meta::NameValue(syn::MetaNameValue {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return syn::Error::new(
|
|
|
|
attr.span(),
|
|
|
|
"The persist attribute should be a key-value pair with a string argument: #[persist = \"foo_bar\"]",
|
|
|
|
)
|
|
|
|
.to_compile_error()
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else if attr.path.is_ident("nested") {
|
|
|
|
match attr.parse_meta() {
|
2022-03-17 03:04:38 +11:00
|
|
|
Ok(syn::Meta::NameValue(syn::MetaNameValue {
|
|
|
|
lit: syn::Lit::Str(s),
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
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('/') {
|
|
|
|
return syn::Error::new(attr.span(), "Group names may not contain slashes")
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
} else if nested_attr.is_some() {
|
2022-03-17 01:53:25 +11:00
|
|
|
return syn::Error::new(attr.span(), "Duplicate nested attribute")
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
2022-03-17 03:04:38 +11:00
|
|
|
} else {
|
|
|
|
nested_attr = Some(s);
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return syn::Error::new(
|
|
|
|
attr.span(),
|
2022-03-17 03:04:38 +11:00
|
|
|
"The nested attribute should be a key-value pair with a string argument: #[nested = \"Group Name\"]",
|
2022-03-17 01:53:25 +11:00
|
|
|
)
|
|
|
|
.to_compile_error()
|
2022-03-17 03:04:38 +11:00
|
|
|
.into()
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:03:03 +11:00
|
|
|
// These are pairs of `(parameter_id, param_ptr, param_group)`. The specific
|
|
|
|
// parameter types know how to convert themselves into the correct ParamPtr variant.
|
2022-03-17 03:04:38 +11:00
|
|
|
// Top-level parameters have no group, and we'll prefix the group name specified in
|
|
|
|
// the `#[nested = "..."]` attribute to fields coming from nested groups
|
2022-03-20 23:03:03 +11:00
|
|
|
param_mapping_self_tokens.push(
|
|
|
|
quote! { (String::from(#param_id), self.#field_name.as_ptr(), String::new()) },
|
|
|
|
);
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
2022-03-20 23:03:03 +11:00
|
|
|
(None, Some(persist_key)) => {
|
|
|
|
if !persist_ids.insert(persist_key.clone()) {
|
2022-03-17 01:53:25 +11:00
|
|
|
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) => {
|
2022-03-20 23:03:03 +11:00
|
|
|
serialized.insert(String::from(#persist_key), data);
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2022-04-12 01:47:32 +10:00
|
|
|
::nih_plug::nih_debug_assert_failure!(
|
|
|
|
"Could not serialize '{}': {}",
|
|
|
|
#persist_key,
|
|
|
|
err
|
|
|
|
)
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
field_deserialize_tokens.push(quote! {
|
2022-03-20 23:03:03 +11:00
|
|
|
#persist_key => {
|
2022-03-17 01:53:25 +11:00
|
|
|
match ::nih_plug::param::internals::deserialize_field(&data) {
|
|
|
|
Ok(deserialized) => {
|
|
|
|
::nih_plug::param::internals::PersistentField::set(
|
|
|
|
&self.#field_name,
|
|
|
|
deserialized,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2022-04-12 01:47:32 +10:00
|
|
|
::nih_plug::nih_debug_assert_failure!(
|
2022-03-17 01:53:25 +11:00
|
|
|
"Could not deserialize '{}': {}",
|
2022-03-20 23:03:03 +11:00
|
|
|
#persist_key,
|
2022-03-17 01:53:25 +11:00
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(Some(_), Some(_)) => {
|
|
|
|
return syn::Error::new(
|
|
|
|
field.span(),
|
|
|
|
"The id and persist attributes are mutually exclusive",
|
|
|
|
)
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
(None, None) => (),
|
|
|
|
}
|
|
|
|
|
2022-03-17 03:04:38 +11:00
|
|
|
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);
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
quote! {
|
2022-03-20 23:03:03 +11:00
|
|
|
unsafe impl #impl_generics Params for #struct_name #ty_generics #where_clause {
|
2022-04-07 23:31:46 +10:00
|
|
|
fn param_map(&self) -> Vec<(String, nih_plug::prelude::ParamPtr, String)> {
|
2022-03-17 03:04:38 +11:00
|
|
|
// This may not be in scope otherwise, used to call .as_ptr()
|
2022-03-17 01:53:25 +11:00
|
|
|
use ::nih_plug::param::Param;
|
|
|
|
|
2022-03-20 23:03:03 +11:00
|
|
|
let mut param_map = vec![#(#param_mapping_self_tokens),*];
|
2022-03-17 03:04:38 +11:00
|
|
|
|
|
|
|
let nested_params_fields: &[&dyn Params] = &[#(&self.#nested_params_field_idents),*];
|
2022-03-20 12:13:19 +11:00
|
|
|
let nested_params_groups: &[String] = &[#(String::from(#nested_params_group_names)),*];
|
2022-03-17 03:04:38 +11:00
|
|
|
for (nested_params, group_name) in
|
|
|
|
nested_params_fields.into_iter().zip(nested_params_groups)
|
|
|
|
{
|
2022-04-07 23:31:46 +10:00
|
|
|
let nested_param_map = nested_params.param_map();
|
2022-03-20 23:03:03 +11:00
|
|
|
let prefixed_nested_param_map =
|
|
|
|
nested_param_map
|
2022-03-17 03:04:38 +11:00
|
|
|
.into_iter()
|
2022-03-20 23:03:03 +11:00
|
|
|
.map(|(param_id, param_ptr, nested_group_name)| {
|
2022-03-17 03:04:38 +11:00
|
|
|
(
|
|
|
|
param_id,
|
2022-03-20 23:03:03 +11:00
|
|
|
param_ptr,
|
2022-03-17 03:04:38 +11:00
|
|
|
if nested_group_name.is_empty() {
|
|
|
|
group_name.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{}/{}", group_name, nested_group_name)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2022-03-20 23:03:03 +11:00
|
|
|
param_map.extend(prefixed_nested_param_map);
|
2022-03-17 03:04:38 +11:00
|
|
|
}
|
|
|
|
|
2022-03-20 23:03:03 +11:00
|
|
|
param_map
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_fields(&self) -> ::std::collections::HashMap<String, String> {
|
|
|
|
let mut serialized = ::std::collections::HashMap::new();
|
|
|
|
#(#field_serialize_tokens)*
|
|
|
|
|
2022-03-17 03:04:38 +11:00
|
|
|
let nested_params_fields: &[&dyn Params] = &[#(&self.#nested_params_field_idents),*];
|
|
|
|
for nested_params in nested_params_fields {
|
2022-04-07 23:31:46 +10:00
|
|
|
serialized.extend(nested_params.serialize_fields());
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
serialized
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_fields(&self, serialized: &::std::collections::HashMap<String, String>) {
|
|
|
|
for (field_name, data) in serialized {
|
|
|
|
match field_name.as_str() {
|
|
|
|
#(#field_deserialize_tokens)*
|
2022-04-12 01:47:32 +10:00
|
|
|
_ => ::nih_plug::nih_debug_assert_failure!("Unknown serialized field name: {} (this may not be accurate)", field_name),
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-03-17 03:04:38 +11:00
|
|
|
let nested_params_fields: &[&dyn Params] = &[#(&self.#nested_params_field_idents),*];
|
|
|
|
for nested_params in nested_params_fields {
|
2022-04-07 23:31:46 +10:00
|
|
|
nested_params.deserialize_fields(serialized);
|
2022-03-17 01:53:25 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.into()
|
|
|
|
}
|