mpc-macros 0.2.12

Arcium MPC Macros
Documentation
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

use crate::party_traits::find_fields_in_struct;

const RNG_STR: &str = "rng";

pub fn derive_probabilistic(input: TokenStream) -> TokenStream {
    // Parsing
    let input = parse_macro_input!(input as DeriveInput);
    let rng_fields = match find_fields_in_struct(&input, [RNG_STR.to_string()]) {
        Ok(mut results) => results.remove(RNG_STR).unwrap_or_default(),
        Err(e) => return e,
    };

    // Validation
    let rng_field = match rng_fields.as_slice(){
        [(ident, _)] => ident,
        [] => return quote! { compile_error!("Found no field marked with #[rng] or a field named `rng`.") }.into(),
        _ => return quote! { compile_error!("Found multiple fields marked with #[rng] or named `rng`. Only one is allowed.") }.into(),
    };

    // Implementation
    let struct_name = &input.ident;
    let generics = &input.generics;
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    quote! {
        impl #impl_generics crate::types::party::Probabilistic for #struct_name #ty_generics #where_clause {
            fn rng(&mut self) -> impl primitives::random::CryptoRngCore {
                &mut self.#rng_field
            }
        }
    }.into()
}