start of cbind proc_macro: just pulling out required elements from syn parse tree (name, fn name, ret type)

This commit is contained in:
Dan Ballard 2022-01-09 14:34:20 -05:00
commit 763bf69112
3 changed files with 73 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
Cargo.lock
.idea

17
Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "cbinder"
version = "0.1.0"
edition = "2021"
authors = ["Dan Ballard <dan@mindstab.net>"]
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"

53
src/lib.rs Normal file
View File

@ -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);
}
}