-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathlib.rs
More file actions
90 lines (82 loc) · 2.65 KB
/
lib.rs
File metadata and controls
90 lines (82 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
extern crate proc_macro;
use quote::quote;
use syn::parse_macro_input;
/// Executes the given access control method before running the decorated
/// instruction handler. Any method in scope of the attribute can be invoked
/// with any arguments from the associated instruction handler.
///
/// # 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.
#[proc_macro_attribute]
pub fn access_control(
args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let mut args = args.to_string();
args.retain(|c| !c.is_whitespace());
let access_control: Vec<proc_macro2::TokenStream> = args
.split(')')
.filter(|ac| !ac.is_empty())
.map(|ac| format!("{ac})")) // Put back on the split char.
.map(|ac| format!("{ac}?;")) // Add `?;` syntax.
.map(|ac| ac.parse().unwrap())
.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)*
}
})
}