anchor-attribute-access-control 1.2.2

Anchor attribute macro for instruction access control
Documentation
extern crate proc_macro;

use {
    quote::quote,
    syn::{parse::Parser, parse_macro_input, punctuated::Punctuated, Expr, Token},
};

/// Executes the given access control methods before running the decorated
/// instruction handler. Any method in scope of the attribute can be invoked
/// with any arguments from the associated instruction handler. Each method
/// must return `Result<()>`.
///
/// # Example
///
/// ```ignore
/// use anchor_lang::prelude::*;
///
/// #[program]
/// mod errors {
///     use super::*;
///
///     #[access_control(Create::accounts(&ctx, bump_seed))]
///     pub fn create(ctx: Context<Create>, bump_seed: u8) -> Result<()> {
///       let my_account = &mut ctx.accounts.my_account;
///       my_account.bump_seed = bump_seed;
///       Ok(())
///     }
/// }
///
/// #[derive(Accounts)]
/// pub struct Create<'info> {
///   #[account(init, payer = payer, space = 8 + 1)]
///   my_account: Account<'info, MyAccount>,
///   #[account(mut)]
///   payer: Signer<'info>,
///   system_program: Program<'info, System>,
/// }
///
/// #[account]
/// pub struct MyAccount {
///     bump_seed: u8,
/// }
///
/// impl Create<'_> {
///   pub fn accounts(ctx: &Context<Create>, bump_seed: u8) -> Result<()> {
///     let seeds = &[ctx.accounts.my_account.to_account_info().key.as_ref(), &[bump_seed]];
///     Pubkey::create_program_address(seeds, ctx.program_id)
///       .map_err(|_| error!(ErrorCode::InvalidNonce))?;
///     Ok(())
///   }
/// }
/// ```
///
/// This example demonstrates a useful pattern. Not only can you use
/// `#[access_control]` to ensure any invariants or preconditions hold prior to
/// executing an instruction, but also it can be used to finish any validation
/// on the `Accounts` struct, particularly when instruction arguments are
/// needed. Here, we use the given `bump_seed` to verify it creates a valid
/// program-derived address.
///
/// Multiple methods may be provided, separated by commas; they run in order:
///
/// ```ignore
/// #[access_control(is_admin(&ctx), not_paused(&ctx.accounts.config))]
/// pub fn set_fee(ctx: Context<SetFee>, new_fee: u64) -> Result<()> {
///     ctx.accounts.config.fee = new_fee;
///     Ok(())
/// }
/// ```
#[proc_macro_attribute]
pub fn access_control(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let parser = Punctuated::<Expr, Token![,]>::parse_terminated;
    let exprs = match parser.parse(args) {
        Ok(exprs) => exprs,
        Err(err) => return err.to_compile_error().into(),
    };
    let access_control: Vec<proc_macro2::TokenStream> =
        exprs.into_iter().map(|expr| quote! { #expr?; }).collect();

    let item_fn = parse_macro_input!(input as syn::ItemFn);

    let fn_attrs = item_fn.attrs;
    let fn_vis = item_fn.vis;
    let fn_sig = item_fn.sig;
    let fn_block = item_fn.block;

    let fn_stmts = fn_block.stmts;

    proc_macro::TokenStream::from(quote! {
        #(#fn_attrs)*
        #fn_vis #fn_sig {

            #(#access_control)*

            #(#fn_stmts)*
        }
    })
}