soroban_builtin_sdk_macros/
lib.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
mod derive_fn;
mod derive_type;

extern crate proc_macro;

use crate::derive_fn::derive_contract_function_set;
use crate::derive_type::{derive_type_enum, derive_type_struct};
use proc_macro::TokenStream;
use quote::quote;
use syn::{
    parse_macro_input, spanned::Spanned, DeriveInput, Error, ImplItem, ImplItemFn, ItemImpl,
};

#[proc_macro_attribute]
pub fn contracttype(_metadata: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    quote! {
        #[derive(soroban_builtin_sdk_macros::ContractType)]
        #input
    }
    .into()
}

#[doc(hidden)]
#[proc_macro_derive(ContractType)]
pub fn derive_contract_type(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let ident = &input.ident;
    let derived = match &input.data {
        syn::Data::Struct(s) => derive_type_struct(ident, s),
        syn::Data::Enum(e) => derive_type_enum(ident, e),
        syn::Data::Union(u) => Error::new(
            u.union_token.span(),
            "unions are unsupported as contract types",
        )
        .to_compile_error(),
    };
    quote! { #derived }.into()
}

fn get_methods(imp: &ItemImpl) -> impl Iterator<Item = &ImplItemFn> {
    imp.items.iter().filter_map(|i| match i {
        ImplItem::Fn(m) => Some(m),
        _ => None,
    })
}

#[proc_macro_attribute]
pub fn contractimpl(_metadata: TokenStream, input: TokenStream) -> TokenStream {
    let imp = parse_macro_input!(input as ItemImpl);
    let ty = &imp.self_ty;
    let all_methods: Vec<_> = get_methods(&imp).collect();

    let cfs = derive_contract_function_set(ty, all_methods.into_iter());
    quote! {
        #imp
        #cfs
    }
    .into()
}