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
use std::convert::TryFrom;
use proc_macro2::Ident;
use super::impl_item::ImplItem;
pub struct ModuleImpl {
impl_items: Vec<ImplItem>,
ident: Ident
}
impl ModuleImpl {
pub fn impl_items(&self) -> &[ImplItem] {
self.impl_items.as_ref()
}
pub fn ident(&self) -> &Ident {
&self.ident
}
pub fn methods(&self) -> Vec<&ImplItem> {
self.impl_items
.iter()
.filter(|i| matches!(i, ImplItem::Method(_) | ImplItem::Constructor(_)))
.collect::<Vec<_>>()
}
pub fn public_methods(&self) -> Vec<&ImplItem> {
self.impl_items
.iter()
.filter(|item| match item {
ImplItem::Constructor(_) => true,
ImplItem::Method(m) => m.is_public(),
ImplItem::Other(_) => false
})
.collect::<Vec<_>>()
}
}
impl TryFrom<syn::ItemImpl> for ModuleImpl {
type Error = syn::Error;
fn try_from(item_impl: syn::ItemImpl) -> Result<Self, Self::Error> {
let path = match &*item_impl.self_ty {
syn::Type::Path(path) => path,
_ => todo!()
};
let contract_ident = path.path.segments.last().unwrap().clone().ident;
let items = item_impl
.items
.into_iter()
.map(<ImplItem as TryFrom<_>>::try_from)
.collect::<Result<Vec<_>, syn::Error>>()?;
Ok(Self {
impl_items: items,
ident: contract_ident
})
}
}
#[cfg(test)]
mod test {
use super::ModuleImpl;
#[test]
fn impl_items_filtering() {
let item_impl: syn::ItemImpl = syn::parse_quote! {
impl Contract {
#[odra(init)]
pub fn constructor() {}
pub(crate) fn crate_public_fn() {}
pub fn public_fn() {}
fn private_fn() {}
}
};
let module_impl = ModuleImpl::try_from(item_impl).unwrap();
assert_eq!(module_impl.methods().len(), 4);
assert_eq!(module_impl.public_methods().len(), 2);
}
}