use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
use crate::party_traits::find_fields_in_struct;
pub const HASHER_STR: &str = "hasher";
pub fn derive_has_tweakable_hasher(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let hasher_fields = match find_fields_in_struct(&input, [HASHER_STR.to_string()]) {
Ok(mut results) => results.remove(HASHER_STR).unwrap_or_default(),
Err(e) => return e,
};
let (hasher_ident, hasher_ty) = match hasher_fields.as_slice() {
[(ident, ty)] => (ident, ty),
[] => return quote! { compile_error!("Found no field marked with #[hasher] or a field named `hasher`.") }.into(),
_ => return quote! { compile_error!("Found multiple fields marked with #[hasher] or named `hasher`. Only one is allowed.") }.into(),
};
let struct_name = &input.ident;
let generics = &input.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
impl #impl_generics primitives::hashing::tweakable::HasTweakableHasher for #struct_name #ty_generics #where_clause {
type Hasher = #hasher_ty;
fn get_hasher(&mut self) -> &mut Self::Hasher {
&mut self.#hasher_ident
}
}
}.into()
}