commit 763bf69112e64e08bbff8be81c27d5e3dd25e04f Author: Dan Ballard Date: Sun Jan 9 14:34:20 2022 -0500 start of cbind proc_macro: just pulling out required elements from syn parse tree (name, fn name, ret type) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b471067 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.lock +.idea diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f248707 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "cbinder" +version = "0.1.0" +edition = "2021" +authors = ["Dan Ballard "] +license = "MIT" +description = "proc-macro to generate memory safe rust native bindings from bindgen for Open Privacy c libraries" + +[lib] +proc-macro = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +syn = { version = "1.0", features = ["full"] } +quote = "1.0" +regex = "1" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..510cb1c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,53 @@ +use proc_macro::TokenStream; + +use quote::quote; +use syn; + +// cbind!(fn start_cwtch(app_dir: &str, tor_path: &str) -> i32 {c_StartCwtch}); +#[proc_macro] +pub fn cbind(_def: TokenStream) -> TokenStream { + //dbg!(&_def); + let ast: syn::ItemFn = syn::parse(_def).unwrap(); + + dbg!(&ast); + + let fn_name = ast.sig.ident; + let fn_call = match &ast.block.stmts[0] { + syn::Stmt::Expr(expr) => match expr { + syn::Expr::Path(expr) => Ok(expr.path.segments[0].ident.to_string()), + _ => Err("".to_string()), + } + _ => Err("".to_string()), + }; + let ret_type = match &ast.sig.output { + syn::ReturnType::Type(_, box_type) => { + let ty= &**box_type; + match ty { + syn::Type::Path(p) => { + if p.path.segments.len() == 1 { + Some(p.path.segments[0].ident.to_string()) + } else { + None + } + } + _ => None + } + } + _ => None + }; + + dbg!(fn_call); + dbg!(ret_type); + let args = ast.sig.inputs; + + quote!(fn #fn_name(&self) {}).into() +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +}