tugraph_plugin_util_proc_macros/
mod.rs1use proc_macro::{TokenStream, TokenTree::Ident};
16use quote::quote;
17
18#[proc_macro_attribute]
20pub fn tugraph_plugin(_attr: TokenStream, input: TokenStream) -> TokenStream {
21 let func_name = input
24 .clone()
25 .into_iter()
26 .find_map(|tt| match tt {
27 Ident(ref name) if name.to_string() != "fn" && name.to_string() != "pub" => Some(tt),
28 _ => None,
29 })
30 .unwrap();
31 let func_name = proc_macro2::TokenStream::from(TokenStream::from(func_name));
32 let user_process_func = proc_macro2::TokenStream::from(input);
33 let extern_c_process = quote! {
34 use tugraph_plugin_util::lgraph_api_graph_db_t;
35 use tugraph_plugin_util::CxxString;
36 use tugraph_plugin_util::Graph as TuGraph;
37 #[allow(clippy::missing_safety_doc)]
38 #[no_mangle]
39 pub unsafe extern "C" fn Process(graph_db: *mut lgraph_api_graph_db_t, request: *const CxxString, response: *mut CxxString) -> bool {
40 let mut graph = TuGraph::from_ptr(graph_db);
41 let request = if request.is_null() {
42 ""
43 } else {
44 (*request).to_str().unwrap()
45 };
46 #user_process_func
48
49 let result = #func_name(&mut graph, request);
50 ::std::mem::forget(graph);
51
52 match result {
53 Ok(val) => {
54 if !response.is_null() {
55 let mut reponse = ::std::pin::Pin::new_unchecked(&mut *response);
56 reponse.as_mut().clear();
57 reponse.as_mut().push_str(val.as_str())
58 }
59 true
60 }
61 Err(e) => {
62 eprintln!("run rust plugin failed: {:?}", e);
63 false
64 }
65 }
66 }
67 };
68 TokenStream::from(extern_c_process)
69}