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
use super::{
Constructor,
Message,
};
use crate::{
error::ExtError as _,
ir,
ir::attrs::Attrs as _,
};
use syn::spanned::Spanned as _;
#[derive(Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum ImplItem {
Constructor(Constructor),
Message(Message),
Other(syn::ImplItem),
}
impl quote::ToTokens for ImplItem {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
Self::Constructor(constructor) => constructor.to_tokens(tokens),
Self::Message(message) => message.to_tokens(tokens),
Self::Other(other) => other.to_tokens(tokens),
}
}
}
impl TryFrom<syn::ImplItem> for ImplItem {
type Error = syn::Error;
fn try_from(impl_item: syn::ImplItem) -> Result<Self, Self::Error> {
match impl_item {
syn::ImplItem::Method(method_item) => {
if !ir::contains_ink_attributes(&method_item.attrs) {
return Ok(Self::Other(method_item.into()))
}
let attr = ir::first_ink_attribute(&method_item.attrs)?
.expect("missing expected ink! attribute for struct");
match attr.first().kind() {
ir::AttributeArg::Message => {
<Message as TryFrom<_>>::try_from(method_item)
.map(Into::into)
.map(Self::Message)
}
ir::AttributeArg::Constructor => {
<Constructor as TryFrom<_>>::try_from(method_item)
.map(Into::into)
.map(Self::Constructor)
}
_ => Err(format_err_spanned!(
method_item,
"encountered invalid ink! attribute at this point, expected either \
#[ink(message)] or #[ink(constructor) attributes"
)),
}
}
other_item => {
if ir::contains_ink_attributes(other_item.attrs()) {
let (ink_attrs, _) =
ir::partition_attributes(other_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::Other(other_item))
}
}
}
}
impl ImplItem {
pub fn is_message(&self) -> bool {
self.filter_map_message().is_some()
}
pub fn filter_map_message(&self) -> Option<&Message> {
match self {
ImplItem::Message(message) => Some(message),
_ => None,
}
}
pub fn is_constructor(&self) -> bool {
self.filter_map_constructor().is_some()
}
pub fn filter_map_constructor(&self) -> Option<&Constructor> {
match self {
ImplItem::Constructor(constructor) => Some(constructor),
_ => None,
}
}
pub fn is_other_item(&self) -> bool {
self.filter_map_other_item().is_some()
}
pub fn filter_map_other_item(&self) -> Option<&syn::ImplItem> {
match self {
ImplItem::Other(rust_item) => Some(rust_item),
_ => None,
}
}
}