Browse Source

lang/attribute/access-control: Allow multiple modifiers

Armani Ferrante 4 years ago
parent
commit
845df6d196
3 changed files with 18 additions and 3 deletions
  1. 3 0
      CHANGELOG.md
  2. 2 1
      lang/attribute/access-control/Cargo.toml
  3. 13 2
      lang/attribute/access-control/src/lib.rs

+ 3 - 0
CHANGELOG.md

@@ -11,9 +11,12 @@ incremented for features.
 
 ## [Unreleased]
 
+### Features
+
 * cli: Embed workspace programs into local validator genesis when testing.
 * cli: Stream program logs to `.anchor/program-logs` directory when testing.
 * spl: Add shared memory api.
+* lang/attribute/access-control: Allow specifying multiple modifier functions.
 
 ## [0.2.0] - 2021-02-08
 

+ 2 - 1
lang/attribute/access-control/Cargo.toml

@@ -15,4 +15,5 @@ proc-macro2 = "1.0"
 quote = "1.0"
 syn = { version = "=1.0.57", features = ["full"] }
 anyhow = "1.0.32"
-anchor-syn = { path = "../../syn", version = "0.2.0" }
+anchor-syn = { path = "../../syn", version = "0.2.0" }
+regex = "1.0"

+ 13 - 2
lang/attribute/access-control/src/lib.rs

@@ -50,7 +50,18 @@ pub fn access_control(
     args: proc_macro::TokenStream,
     input: proc_macro::TokenStream,
 ) -> proc_macro::TokenStream {
-    let access_control: proc_macro2::TokenStream = args.to_string().parse().unwrap();
+    let mut args = args.to_string();
+    args.retain(|c| !c.is_whitespace());
+    let access_control: Vec<proc_macro2::TokenStream> = args
+        .split(')')
+        .filter_map(|ac| match ac {
+            "" => None,
+            _ => Some(ac),
+        })
+        .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);
 
@@ -63,7 +74,7 @@ pub fn access_control(
     proc_macro::TokenStream::from(quote! {
         #fn_vis #fn_sig {
 
-            #access_control?;
+            #(#access_control)*
 
             #(#fn_stmts)*
         }