54 lines
1.3 KiB
Rust
54 lines
1.3 KiB
Rust
|
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);
|
||
|
}
|
||
|
}
|