Skip to content

Commit

Permalink
exclude working as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-ant committed Oct 25, 2024
1 parent e3ca8a8 commit a6623cb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
3 changes: 2 additions & 1 deletion rust/psibase-macros-test/services/add-check-init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub mod service {
}

#[pre_action(exclude = "init")]
fn check_init() {
// #[pre_action(exclude)]
fn check_initialize() {
// name is not important
let table = InitTable::new();
check(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use darling::FromMeta;
use darling::{ast::NestedMeta, FromMeta};
use proc_macro_error::abort;
use quote::{quote, ToTokens};
use syn::{AttrStyle, Attribute, FnArg, Ident, Item, ItemFn, Pat, ReturnType};
use syn::{AttrStyle, Attribute, FnArg, Ident, Item, ItemFn, Meta, Pat, ReturnType};

#[derive(Debug, FromMeta)]
#[darling(default)]
Expand Down Expand Up @@ -192,13 +192,15 @@ pub fn process_action_schema(
pub struct PreAction {
pub exists: bool,
pub fn_name: Option<Ident>,
pub exclude: Option<String>,
}

impl Default for PreAction {
fn default() -> Self {
Self {
PreAction {
exists: false,
fn_name: None,
exclude: None,
}
}
}
Expand All @@ -212,12 +214,66 @@ fn is_pre_action_attr(attr: &Attribute) -> bool {
false
}

// #[derive(Debug, FromMeta)]
// enum PreActionExclude {
// String,
// Vec(String),
// }
#[derive(Debug, FromMeta)]
struct PreActionOptions {
exclude: String, // PreActionExclude,
}

pub fn check_for_pre_action(pre_action_info: &mut PreAction, items: &mut Vec<Item>) {
println!("check_for_pre_action().top");
for (_item_index, item) in items.iter_mut().enumerate() {
if let Item::Fn(f) = item {
if f.attrs.iter().any(is_pre_action_attr) {
let temp = f.attrs.clone();
let pre_action_attr = temp.iter().find(|el| is_pre_action_attr(el)).unwrap();
// println!("pre_action_attr {:?}", pre_action_attr);
pre_action_info.exists = true;
pre_action_info.fn_name = Some(f.sig.ident.clone());
//TODO: decode attrs and get `exclude` arg
// println!("1: processing pre_action_attr args");
let attr_args_ts = match pre_action_attr.meta.clone() {
Meta::List(args) => {
// println!("List: {:?}", args);
args.tokens
}
Meta::Path(args) => panic!("expected list; got path."),
Meta::NameValue(args) => {
// println!("NameValue: {:?}", args);
args.value.to_token_stream()
}
};
let attr_args = match NestedMeta::parse_meta_list(attr_args_ts) {
Ok(v) => {
// println!("v in match: {:#?}", v);
v
}
Err(e) => {
// TODO: how to handle macro errors/reporting better?
println!(
"about to panic; error in parse_meta_list(); err {}",
e.to_string()
);
panic!("Error parsing pre_action arguments");
}
};
println!("2: parsing attr args into options...");
// println!("attr_args: {:#?}", attr_args);

let mut options: PreActionOptions = match PreActionOptions::from_list(&attr_args) {
Ok(val) => val,
Err(err) => {
panic!("Error parsing pre_action arguments");
// return err.write_errors().into();
}
};
println!("pre-action options: {:?}", options);
pre_action_info.exclude = Some(options.exclude);

if let Some(pre_action_pos) = f.attrs.iter().position(is_pre_action_attr) {
f.attrs.remove(pre_action_pos);
}
Expand All @@ -228,6 +284,13 @@ pub fn check_for_pre_action(pre_action_info: &mut PreAction, items: &mut Vec<Ite

pub fn add_pre_action_call(pre_action_info: &PreAction, f: &mut ItemFn) {
// println!("adding check_init to {}", f.sig.ident.to_string());
if Some(f.sig.ident.to_string()) == pre_action_info.exclude {
println!(
"{} is in exclude list; skipping adding pre_action call",
f.sig.ident.to_string()
);
return;
}
let fn_name = pre_action_info.fn_name.clone();
let new_line_ts = quote! {
#fn_name();
Expand Down
28 changes: 15 additions & 13 deletions rust/psibase_macros/psibase-macros-lib/src/service_macro/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,31 @@ fn process_mod(
}
}

let mut has_check_init = false;
// let mut has_check_init = false;
let mut action_structs = proc_macro2::TokenStream::new();
let mut action_schema_init = quote! {};
let mut action_callers = proc_macro2::TokenStream::new();
let mut dispatch_body = proc_macro2::TokenStream::new();
let mut with_action_struct = proc_macro2::TokenStream::new();
for fn_index in non_action_fns.iter() {
if let Item::Fn(f) = &mut items[*fn_index] {
let fn_name = f.sig.ident.to_string();
if fn_name == "check_init" {
// println!("found check_init(): {:#?}", fn_name);
has_check_init = true;
} else {
// println!("not check_init(): {:#?}", fn_name);
}
}
}
// for fn_index in non_action_fns.iter() {
// if let Item::Fn(f) = &mut items[*fn_index] {
// let fn_name = f.sig.ident.to_string();
// if fn_name == "check_init" {
// // println!("found check_init(): {:#?}", fn_name);
// // TODO: Replace this with PreAction mechanism
// has_check_init = true;
// } else {
// // println!("not check_init(): {:#?}", fn_name);
// }
// }
// }

for fn_index in action_fns.iter() {
if let Item::Fn(f) = &mut items[*fn_index] {
let mut invoke_args = quote! {};
let mut invoke_struct_args = quote! {};
if has_check_init {
if pre_action_info.exists {
// TODO: add only-if-not-excluded
add_pre_action_call(&pre_action_info, f);
// println!(
// "1 : 1st line of {} is {:#?}",
Expand Down

0 comments on commit a6623cb

Please sign in to comment.