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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use quote::{quote, ToTokens};
use crate::attrs::{partition_attributes, OdraAttribute};
use super::utils;
pub struct Method {
pub attrs: Vec<OdraAttribute>,
pub impl_item: syn::ImplItemMethod,
pub ident: syn::Ident,
pub args: syn::punctuated::Punctuated<syn::PatType, syn::token::Comma>,
pub ret: syn::ReturnType,
pub full_sig: syn::Signature,
pub visibility: syn::Visibility
}
impl Method {
pub fn is_public(&self) -> bool {
matches!(self.visibility, syn::Visibility::Public(_))
}
pub fn is_payable(&self) -> bool {
self.attrs.iter().any(|attr| attr.is_payable())
}
}
impl ToTokens for Method {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let name = &self.ident.to_string();
let args = &self
.args
.iter()
.map(|arg| {
let name = &*arg.pat;
let ty = &*arg.ty;
let ty = quote!(<#ty as odra::types::Typed>::ty());
quote! {
odra::types::contract_def::Argument {
ident: String::from(stringify!(#name)),
ty: #ty,
},
}
})
.collect::<proc_macro2::TokenStream>();
let ret = match &self.ret {
syn::ReturnType::Default => quote!(odra::types::Type::Unit),
syn::ReturnType::Type(_, ty) => quote!(<#ty as odra::types::Typed>::ty())
};
let ty = match self.attrs.iter().any(|attr| attr.is_payable()) {
true => quote!(odra::types::contract_def::EntrypointType::PublicPayable),
false => quote!(odra::types::contract_def::EntrypointType::Public)
};
let is_mut = utils::is_mut(&self.full_sig);
let ep = quote! {
odra::types::contract_def::Entrypoint {
ident: String::from(#name),
args: vec![#args],
is_mut: #is_mut,
ret: #ret,
ty: #ty,
},
};
tokens.extend(ep)
}
}
impl From<syn::ImplItemMethod> for Method {
fn from(method: syn::ImplItemMethod) -> Self {
let (odra_attrs, attrs) = partition_attributes(method.clone().attrs).unwrap();
let ident = method.sig.ident.to_owned();
let args = method
.sig
.inputs
.iter()
.filter_map(|arg| match arg {
syn::FnArg::Receiver(_) => None,
syn::FnArg::Typed(pat) => Some(pat.clone())
})
.collect::<syn::punctuated::Punctuated<_, _>>();
let ret = method.clone().sig.output;
let full_sig = method.clone().sig;
let visibility = method.vis.clone();
Self {
attrs: odra_attrs,
impl_item: syn::ImplItemMethod { attrs, ..method },
ident,
args,
ret,
full_sig,
visibility
}
}
}