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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
mod event;
mod storage;
#[cfg(test)]
mod tests;
pub use self::{
event::Event,
storage::Storage,
};
use crate::{
error::ExtError as _,
ir,
ir::attrs::Attrs as _,
};
use core::convert::TryFrom;
use syn::spanned::Spanned as _;
#[derive(Debug, PartialEq, Eq)]
pub enum Item {
Ink(InkItem),
Rust(syn::Item),
}
impl quote::ToTokens for Item {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
Self::Ink(ink_item) => ink_item.to_tokens(tokens),
Self::Rust(rust_item) => rust_item.to_tokens(tokens),
}
}
}
impl TryFrom<syn::Item> for Item {
type Error = syn::Error;
fn try_from(item: syn::Item) -> Result<Self, Self::Error> {
match item {
syn::Item::Struct(item_struct) => {
if !ir::contains_ink_attributes(&item_struct.attrs) {
return Ok(Self::Rust(item_struct.into()))
}
let attr = ir::first_ink_attribute(&item_struct.attrs)?
.expect("missing expected ink! attribute for struct");
match attr.first().kind() {
ir::AttributeArg::Storage => {
<ir::Storage as TryFrom<_>>::try_from(item_struct)
.map(Into::into)
.map(Self::Ink)
}
ir::AttributeArg::Event => {
<ir::Event as TryFrom<_>>::try_from(item_struct)
.map(Into::into)
.map(Self::Ink)
}
_invalid => {
Err(format_err!(
attr.span(),
"encountered unsupported ink! attribute argument on struct",
))
}
}
}
syn::Item::Impl(item_impl) => {
if !ir::ItemImpl::is_ink_impl_block(&item_impl)? {
return Ok(Self::Rust(item_impl.into()))
}
<ir::ItemImpl as TryFrom<_>>::try_from(item_impl)
.map(Into::into)
.map(Self::Ink)
}
item => {
if ir::contains_ink_attributes(item.attrs()) {
let (ink_attrs, _) =
ir::partition_attributes(item.attrs().iter().cloned())?;
assert!(!ink_attrs.is_empty());
fn into_err(attr: &ir::InkAttribute) -> syn::Error {
format_err!(attr.span(), "encountered unexpected ink! attribute",)
}
return Err(ink_attrs[1..]
.iter()
.map(into_err)
.fold(into_err(&ink_attrs[0]), |fst, snd| fst.into_combine(snd)))
}
Ok(Self::Rust(item))
}
}
}
}
impl Item {
pub fn is_ink_item(&self) -> bool {
self.map_ink_item().is_some()
}
pub fn is_rust_item(&self) -> bool {
self.map_rust_item().is_some()
}
pub fn map_ink_item(&self) -> Option<&InkItem> {
match self {
Item::Ink(ink_item) => Some(ink_item),
_ => None,
}
}
pub fn map_rust_item(&self) -> Option<&syn::Item> {
match self {
Item::Rust(rust_item) => Some(rust_item),
_ => None,
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum InkItem {
Storage(ir::Storage),
Event(ir::Event),
ImplBlock(ir::ItemImpl),
}
impl quote::ToTokens for InkItem {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
Self::Storage(storage) => storage.to_tokens(tokens),
Self::Event(event) => event.to_tokens(tokens),
Self::ImplBlock(impl_block) => impl_block.to_tokens(tokens),
}
}
}
impl InkItem {
pub fn is_ink_item(item: &syn::Item) -> Result<bool, syn::Error> {
match item {
syn::Item::Struct(item_struct) => {
if ir::Storage::is_ink_storage(item_struct)?
|| ir::Event::is_ink_event(item_struct)?
{
return Ok(true)
}
}
syn::Item::Impl(item_impl) => {
return ir::ItemImpl::is_ink_impl_block(item_impl)
}
_ => (),
}
Ok(false)
}
}
impl From<ir::Storage> for InkItem {
fn from(storage: ir::Storage) -> Self {
Self::Storage(storage)
}
}
impl From<ir::Event> for InkItem {
fn from(event: ir::Event) -> Self {
Self::Event(event)
}
}
impl From<ir::ItemImpl> for InkItem {
fn from(impl_block: ir::ItemImpl) -> Self {
Self::ImplBlock(impl_block)
}
}
impl InkItem {
pub fn filter_map_storage_item(&self) -> Option<&ir::Storage> {
match self {
InkItem::Storage(storage) => Some(storage),
_ => None,
}
}
pub fn is_storage_item(&self) -> bool {
self.filter_map_storage_item().is_some()
}
pub fn filter_map_event_item(&self) -> Option<&ir::Event> {
match self {
InkItem::Event(event) => Some(event),
_ => None,
}
}
pub fn is_event_item(&self) -> bool {
self.filter_map_event_item().is_some()
}
pub fn filter_map_impl_block(&self) -> Option<&ir::ItemImpl> {
match self {
InkItem::ImplBlock(impl_block) => Some(impl_block),
_ => None,
}
}
pub fn is_impl_block(&self) -> bool {
self.filter_map_impl_block().is_some()
}
}