2022-01-26 05:55:01 +11:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
use syn::spanned::Spanned;
|
|
|
|
|
2022-01-31 02:14:52 +11:00
|
|
|
#[proc_macro_derive(Params, attributes(id, persist))]
|
2022-01-26 05:55:01 +11:00
|
|
|
pub fn derive_params(input: TokenStream) -> TokenStream {
|
|
|
|
let ast = syn::parse_macro_input!(input as syn::DeriveInput);
|
|
|
|
|
2022-01-26 06:02:58 +11:00
|
|
|
let struct_name = &ast.ident;
|
2022-01-26 05:55:01 +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()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-31 02:14:52 +11:00
|
|
|
// We only care about fields with `id` and `persist` 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.
|
|
|
|
let mut param_mapping_insert_tokens = Vec::new();
|
|
|
|
let mut field_serialize_tokens = Vec::new();
|
|
|
|
let mut field_deserialize_tokens = Vec::new();
|
2022-01-26 05:55:01 +11:00
|
|
|
for field in fields.named {
|
2022-01-26 06:02:58 +11:00
|
|
|
let field_name = match &field.ident {
|
2022-01-26 05:55:01 +11:00
|
|
|
Some(ident) => ident,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
2022-01-31 02:14:52 +11:00
|
|
|
// These two attributes are mutually exclusive
|
|
|
|
let mut id_attr: Option<String> = None;
|
|
|
|
let mut persist_attr: Option<String> = None;
|
|
|
|
for attr in &field.attrs {
|
2022-01-30 06:40:14 +11:00
|
|
|
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()
|
2022-01-28 23:00:36 +11:00
|
|
|
}
|
2022-01-26 05:55:01 +11:00
|
|
|
};
|
2022-01-31 02:14:52 +11:00
|
|
|
} 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()
|
|
|
|
}
|
|
|
|
};
|
2022-01-30 06:40:14 +11:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 05:55:01 +11:00
|
|
|
|
2022-01-31 02:14:52 +11:00
|
|
|
match (id_attr, persist_attr) {
|
|
|
|
(Some(param_id), None) => {
|
|
|
|
// The specific parameter types know how to convert themselves into the correct ParamPtr
|
|
|
|
// variant
|
|
|
|
param_mapping_insert_tokens
|
|
|
|
.push(quote! { param_map.insert(#param_id, self.#field_name.as_ptr()); });
|
|
|
|
}
|
|
|
|
(None, Some(stable_name)) => {
|
|
|
|
// 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::params::serialize_field(&self.#field_name) {
|
|
|
|
Ok(data) => {
|
|
|
|
serialized.insert(String::from(#stable_name), data);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
::nih_plug::nih_log!(
|
|
|
|
"Could not serialize '{}': {}",
|
|
|
|
#stable_name,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
field_deserialize_tokens.push(quote! {
|
|
|
|
#stable_name => {
|
|
|
|
match ::nih_plug::params::deserialize_field(&data) {
|
|
|
|
Ok(deserialized) => {
|
|
|
|
self.#field_name = deserialized;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
::nih_plug::nih_log!(
|
|
|
|
"Could not deserialize '{}': {}",
|
|
|
|
#stable_name,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(Some(_), Some(_)) => {
|
|
|
|
return syn::Error::new(
|
|
|
|
field.span(),
|
|
|
|
"The id and persist attributes are mutually exclusive",
|
|
|
|
)
|
|
|
|
.to_compile_error()
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
(None, None) => (),
|
2022-01-26 05:55:01 +11:00
|
|
|
}
|
2022-01-25 22:29:00 +11:00
|
|
|
}
|
2022-01-26 05:55:01 +11:00
|
|
|
|
|
|
|
quote! {
|
2022-01-26 06:02:58 +11:00
|
|
|
impl Params for #struct_name {
|
2022-01-26 05:55:01 +11:00
|
|
|
fn param_map(
|
|
|
|
self: std::pin::Pin<&Self>,
|
|
|
|
) -> std::collections::HashMap<&'static str, nih_plug::params::ParamPtr> {
|
|
|
|
let mut param_map = std::collections::HashMap::new();
|
|
|
|
|
2022-01-31 02:14:52 +11:00
|
|
|
#(#param_mapping_insert_tokens)*
|
2022-01-26 05:55:01 +11:00
|
|
|
|
|
|
|
param_map
|
|
|
|
}
|
2022-01-31 02:14:52 +11:00
|
|
|
|
|
|
|
fn serialize_fields(&self) -> ::std::collections::HashMap<String, Vec<u8>> {
|
|
|
|
let mut serialized = ::std::collections::HashMap::new();
|
|
|
|
|
|
|
|
#(#field_serialize_tokens)*
|
|
|
|
|
|
|
|
serialized
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_fields(&mut self, serialized: ::std::collections::HashMap<&str, Vec<u8>>) {
|
|
|
|
for (field_name, data) in serialized {
|
|
|
|
match field_name {
|
|
|
|
#(#field_deserialize_tokens)*
|
|
|
|
_ => nih_log!("Unknown field name: {}", field_name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 05:55:01 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.into()
|
2022-01-25 22:29:00 +11:00
|
|
|
}
|