1
0
Fork 0

Drop the singular Param struct

This commit is contained in:
Robbert van der Helm 2022-11-17 17:28:12 +01:00
parent 6036db45e5
commit 055d649f7c

View file

@ -63,7 +63,7 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
// large enough to the point where a linear search starts being expensive, // large enough to the point where a linear search starts being expensive,
// then the plugin should probably start splitting up their parameters. // then the plugin should probably start splitting up their parameters.
if params.iter().any(|p| match p { if params.iter().any(|p| match p {
AnyParam::Single(param) => param.id == s, AnyParam::Single { id, .. } => &s == id,
_ => false, _ => false,
}) { }) {
return syn::Error::new( return syn::Error::new(
@ -74,10 +74,10 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
.into(); .into();
} }
params.push(AnyParam::Single(Param { params.push(AnyParam::Single {
id: s, id: s,
field: field_name.clone(), field: field_name.clone(),
})); });
processed_attribute = true; processed_attribute = true;
} }
@ -402,16 +402,27 @@ pub fn derive_params(input: TokenStream) -> TokenStream {
.into() .into()
} }
/// A parameter defined on this struct using the `#[id = "..."]` attribute, or another object that
/// also implements `Params` tagged with one of the variations on the `#[nested]` attribute.
// TODO: Rename to Param
#[derive(Debug)] #[derive(Debug)]
enum AnyParam { enum AnyParam {
Single(Param), /// A parameter that should be added to the parameter map.
Single {
/// The name of the parameter's field on the struct.
field: syn::Ident,
/// The parameter's unique ID.
id: syn::LitStr,
},
/// Another struct also implementing `Params`. This object's parameters are inlined in the
/// parameter list.
Nested(NestedParams), Nested(NestedParams),
} }
impl AnyParam { impl AnyParam {
fn to_token(&self) -> proc_macro2::TokenStream { fn to_token(&self) -> proc_macro2::TokenStream {
match self { match self {
AnyParam::Single(Param { field, id }) => { AnyParam::Single { field, id } => {
quote! { [(String::from(#id), self.#field.as_ptr(), String::new())] } quote! { [(String::from(#id), self.#field.as_ptr(), String::new())] }
} }
AnyParam::Nested(params) => params.to_token(), AnyParam::Nested(params) => params.to_token(),
@ -419,15 +430,6 @@ impl AnyParam {
} }
} }
/// 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. /// A field containing data that must be stored in the plugin's state.
#[derive(Debug)] #[derive(Debug)]
struct PersistentField { struct PersistentField {