core_crypto_macros/
lib.rs

1extern crate proc_macro;
2
3use crate::entity_derive::KeyStoreEntity;
4use proc_macro::TokenStream;
5use proc_macro2::Ident;
6use quote::quote;
7use syn::{
8    Attribute, Block, FnArg, ItemFn, ReturnType, Visibility, parse_macro_input, punctuated::Punctuated, token::Comma,
9};
10
11mod durable;
12mod entity_derive;
13mod idempotent;
14
15/// Implements the `Entity` trait for the given struct.
16/// To be used internally inside the `core-crypto-keystore` crate only.
17#[proc_macro_derive(Entity, attributes(entity, id))]
18pub fn derive_entity(input: TokenStream) -> TokenStream {
19    let parsed = parse_macro_input!(input as KeyStoreEntity).flatten();
20    TokenStream::from(quote! { #parsed })
21}
22
23/// Will drop current MLS group in memory and replace it with the one in the keystore.
24/// This simulates an application crash. Once restarted, everything has to be loaded from the
25/// keystore, memory is lost.
26///
27/// Requires the `MlsConversation` method to have a parameter exactly like `backend: &MlsCryptoProvider`
28///
29/// This helps spotting:
30/// * when one has forgotten to call `persist_group_when_changed`
31/// * if persisted fields are sufficient to pursue normally after a crash
32///
33/// **IF** you mark a method `#[durable]`, remove its call to
34/// `persist_group_when_changed` and tests still pass, you either:
35/// * have unit tests not covering the method enough
36/// * do not require this method to be durable
37#[proc_macro_attribute]
38pub fn durable(_args: TokenStream, item: TokenStream) -> TokenStream {
39    durable::durable(item)
40}
41
42/// !!! Not literally idempotent !!!
43///
44/// Marker for methods on 'core_crypto::Client' which leave the number of entities in the keystore even.
45/// They can create/destroy some but always compensate.
46/// So they are not idempotent, they cannot be safely replayed and they might leave the keystore in
47/// a different state.
48#[proc_macro_attribute]
49pub fn idempotent(_args: TokenStream, item: TokenStream) -> TokenStream {
50    idempotent::idempotent(item)
51}
52
53/// Neologism to mean the opposite of idempotent. Methods of 'core_crypto::Client' marked with this have to
54/// insert/delete an entity in the keystore and change the number of entities persisted.
55#[proc_macro_attribute]
56pub fn dispotent(_args: TokenStream, item: TokenStream) -> TokenStream {
57    idempotent::dispotent(item)
58}
59
60pub(crate) fn doc_attributes(ast: &ItemFn) -> Vec<Attribute> {
61    ast.attrs
62        .iter()
63        .filter(|attr| attr.path().is_ident("doc"))
64        .cloned()
65        .collect::<Vec<syn::Attribute>>()
66}
67
68pub(crate) fn compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream {
69    let compile_err = TokenStream::from(err.to_compile_error());
70    item.extend(compile_err);
71    item
72}
73
74#[allow(clippy::type_complexity)]
75pub(crate) fn items(
76    ast: &ItemFn,
77) -> (
78    &ReturnType,
79    &Ident,
80    &Punctuated<FnArg, Comma>,
81    &Box<Block>,
82    &Vec<Attribute>,
83    &Visibility,
84) {
85    let ret = &ast.sig.output;
86    let name = &ast.sig.ident;
87    let inputs = &ast.sig.inputs;
88    let body = &ast.block;
89    let attrs = &ast.attrs;
90    let vis = &ast.vis;
91    (ret, name, inputs, body, attrs, vis)
92}