ink_analyzer_ir/
constructor.rs

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

use ra_ap_syntax::ast;

use crate::traits::IsInkCallable;

/// An ink! constructor.
#[ink_analyzer_macro::entity(arg_kind = Constructor)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Constructor {
    // ASTNode type.
    ast: ast::Fn,
}

impl_ast_type_trait!(Constructor, IsInkFn);

impl IsInkCallable for Constructor {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::*;
    use crate::traits::{InkEntity, IsInkFn};
    use test_utils::quote_as_str;

    #[test]
    fn cast_works() {
        for (code, is_payable, has_selector, is_default) in [
            (
                quote_as_str! {
                    #[ink(constructor)]
                    pub fn my_constructor() -> Self {}
                },
                false,
                false,
                false,
            ),
            (
                quote_as_str! {
                    #[ink(constructor, payable, default, selector=1)]
                    pub fn my_constructor() -> Self {}
                },
                true,
                true,
                true,
            ),
            (
                quote_as_str! {
                    #[ink(constructor)]
                    #[ink(payable, default, selector=1)]
                    pub fn my_constructor() -> Self {}
                },
                true,
                true,
                true,
            ),
            (
                quote_as_str! {
                    #[ink(constructor)]
                    #[ink(payable)]
                    #[ink(default)]
                    #[ink(selector=1)]
                    pub fn my_constructor() -> Self {}
                },
                true,
                true,
                true,
            ),
            (
                quote_as_str! {
                    #[ink(constructor, selector=0xA)]
                    pub fn my_constructor() -> Self {}
                },
                false,
                true,
                false,
            ),
            (
                quote_as_str! {
                    #[ink(constructor, selector=_)]
                    pub fn my_constructor() -> Self {}
                },
                false,
                true,
                false,
            ),
        ] {
            let node = parse_first_syntax_node(code);

            let constructor = Constructor::cast(node).unwrap();

            // `payable` argument exists.
            assert_eq!(constructor.payable_arg().is_some(), is_payable);

            // `selector` argument exists.
            assert_eq!(constructor.selector_arg().is_some(), has_selector);

            // `default` argument exists.
            assert_eq!(constructor.default_arg().is_some(), is_default);

            // composed selector exists.
            assert!(constructor.composed_selector().is_some());

            // `fn` item exists.
            assert!(constructor.fn_item().is_some());
        }
    }
}