core_crypto_macros/
durable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use crate::{compile_error, doc_attributes, items};
use proc_macro::TokenStream;

pub(crate) fn durable(item: TokenStream) -> TokenStream {
    const ASYNC_ERROR_MSG: &str = "Since a durable method requires persistence in the keystore, it has to be async";

    let ast = match syn::parse2::<syn::ItemFn>(item.clone().into()) {
        Ok(ast) => ast,
        Err(e) => return compile_error(item, e),
    };
    if ast.sig.asyncness.is_none() {
        return compile_error(item, syn::Error::new_spanned(ast, ASYNC_ERROR_MSG));
    }

    let doc_attributes = doc_attributes(&ast);
    let (ret, name, inputs, body, attrs, vis) = items(&ast);

    let func: proc_macro2::TokenStream = quote::quote! {
        #(#doc_attributes)*
        #(#attrs)*
        #vis async fn #name(#inputs) #ret {
            let _result = #body;
            #[cfg(test)] {
                self.drop_and_restore(backend).await;
            }
            _result
        }
    };
    func.into()
}