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
//! ink! contract IR.

use ink_analyzer_macro::{FromInkAttribute, FromSyntax};
use ra_ap_syntax::ast;

use crate::traits::{FromInkAttribute, FromSyntax};
use crate::tree::utils;
use crate::{
    Constructor, Event, InkArg, InkArgKind, InkAttrData, InkAttribute, InkE2ETest, InkImpl,
    InkTest, Message, Storage,
};

/// An ink! contract.
#[derive(Debug, Clone, PartialEq, Eq, FromInkAttribute, FromSyntax)]
pub struct Contract {
    /// ink! attribute IR data.
    #[macro_kind(Contract)]
    ink_attr: InkAttrData<ast::Module>,
    /// ink! storage definition.
    #[arg_kind(Storage)]
    storage: Option<Storage>,
    /// ink! events.
    #[arg_kind(Event)]
    events: Vec<Event>,
    /// ink! impl items.
    #[arg_kind(Impl)]
    impls: Vec<InkImpl>,
    /// ink! constructors.
    #[arg_kind(Constructor)]
    constructors: Vec<Constructor>,
    /// ink! messages.
    #[arg_kind(Message)]
    messages: Vec<Message>,
    /// ink! tests.
    #[macro_kind(Test)]
    tests: Vec<InkTest>,
    /// ink! e2e tests.
    #[macro_kind(E2ETest)]
    e2e_tests: Vec<InkE2ETest>,
}

impl Contract {
    /// Returns the `mod` item (if any) for the ink! contract.
    pub fn module(&self) -> Option<&ast::Module> {
        self.ink_attr.parent_ast()
    }

    /// Returns the ink! env argument (if any) for the ink! contract.
    pub fn env_arg(&self) -> Option<InkArg> {
        utils::ink_arg_by_kind(self.syntax(), InkArgKind::Env)
    }

    /// Returns the ink! `keep_attr` argument (if any) for the ink! contract.
    pub fn keep_attr_arg(&self) -> Option<InkArg> {
        utils::ink_arg_by_kind(self.syntax(), InkArgKind::KeepAttr)
    }

    /// Returns the ink! storage definition for the ink! contract.
    pub fn storage(&self) -> Option<&Storage> {
        self.storage.as_ref()
    }

    /// Returns the ink! events for the ink! contract.
    pub fn events(&self) -> &[Event] {
        &self.events
    }

    /// Returns the ink! impl blocks for the ink! contract.
    pub fn impls(&self) -> &[InkImpl] {
        &self.impls
    }

    /// Returns the ink! constructors for the ink! contract.
    pub fn constructors(&self) -> &[Constructor] {
        &self.constructors
    }

    /// Returns the ink! messages for the ink! contract.
    pub fn messages(&self) -> &[Message] {
        &self.messages
    }

    /// Returns the ink! tests for the ink! contract.
    pub fn tests(&self) -> &[InkTest] {
        &self.tests
    }

    /// Returns the ink! e2e tests for the ink! contract.
    pub fn e2e_tests(&self) -> &[InkE2ETest] {
        &self.e2e_tests
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::*;
    use test_utils::quote_as_str;

    #[test]
    fn cast_works() {
        let ink_attr = parse_first_ink_attribute(quote_as_str! {
            #[ink::contract(env=my::env::Types, keep_attr="foo,bar")]
            mod MyContract {
                #[ink(storage)]
                pub struct MyContract {}

                #[ink(event)]
                pub struct MyEvent {
                }

                #[ink(event, anonymous)]
                pub struct MyEvent2 {
                }

                impl MyContract {
                    #[ink(constructor, payable, default, selector=_)]
                    pub fn my_constructor() -> Self {}

                    #[ink(message, payable, default, selector=_)]
                    pub fn my_message(&self) {}
                }

                impl MyTrait for MyContract {
                    #[ink(constructor, payable, default, selector=1)]
                    fn my_constructor() -> Self {}

                    #[ink(message, payable, default, selector=1)]
                    fn my_message(&self) {}
                }

                impl ::my_full::long_path::MyTrait for MyContract {
                    #[ink(constructor, payable, default, selector=0x2)]
                    fn my_constructor() -> Self {}

                    #[ink(message, payable, default, selector=0x2)]
                    fn my_message(&self) {}
                }

                impl relative_path::MyTrait for MyContract {
                    #[ink(constructor)]
                    fn my_constructor() -> Self {}

                    #[ink(message)]
                    fn my_message(&self) {}
                }

                #[ink(namespace="my_namespace")]
                impl MyContract {
                    #[ink(constructor)]
                    pub fn my_constructor() -> Self {}

                    #[ink(message)]
                    pub fn my_message(&self) {}
                }

                #[ink(impl)]
                impl MyContract {
                    #[ink(constructor)]
                    pub fn my_constructor() -> Self {}

                    #[ink(message)]
                    pub fn my_message(&self) {}
                }

                #[ink(impl, namespace="my_namespace")]
                impl MyContract {
                    #[ink(constructor)]
                    pub fn my_constructor() -> Self {}

                    #[ink(message)]
                    pub fn my_message(&self) {}
                }

                #[ink(impl)]
                impl MyContract {
                }

                #[cfg(test)]
                mod tests {
                    #[ink::test]
                    fn it_works {
                    }

                    #[ink::test]
                    fn it_works2 {
                    }
                }
            }
        });

        let contract = Contract::cast(ink_attr).unwrap();

        // `env` argument exists.
        assert!(contract.env_arg().is_some());

        // `keep_attr` argument exists.
        assert!(contract.keep_attr_arg().is_some());

        // storage definition exists.
        assert!(contract.storage().is_some());

        // 2 events.
        assert_eq!(contract.events().len(), 2);

        // 8 impls.
        assert_eq!(contract.impls().len(), 8);

        // 7 constructors.
        assert_eq!(contract.constructors().len(), 7);

        // 7 messages.
        assert_eq!(contract.messages().len(), 7);

        // 2 tests.
        assert_eq!(contract.tests().len(), 2);

        // `mod` item exists.
        assert!(contract.module().is_some());
    }
}