core_crypto_macros/
durable.rs

1use crate::{compile_error, doc_attributes, items};
2use proc_macro::TokenStream;
3
4pub(crate) fn durable(item: TokenStream) -> TokenStream {
5    const ASYNC_ERROR_MSG: &str = "Since a durable method requires persistence in the keystore, it has to be async";
6
7    let ast = match syn::parse2::<syn::ItemFn>(item.clone().into()) {
8        Ok(ast) => ast,
9        Err(e) => return compile_error(item, e),
10    };
11    if ast.sig.asyncness.is_none() {
12        return compile_error(item, syn::Error::new_spanned(ast, ASYNC_ERROR_MSG));
13    }
14
15    let doc_attributes = doc_attributes(&ast);
16    let (ret, name, inputs, body, attrs, vis) = items(&ast);
17
18    let func: proc_macro2::TokenStream = quote::quote! {
19        #(#doc_attributes)*
20        #(#attrs)*
21        #vis async fn #name(#inputs) #ret {
22            let _result = #body;
23            #[cfg(test)] {
24                self.drop_and_restore(backend).await;
25            }
26            _result
27        }
28    };
29    func.into()
30}