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

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

use crate::traits::{FromInkAttribute, FromSyntax, IsInkCallable, IsInkFn};
use crate::{InkAttrData, InkAttribute};

/// An ink! constructor.
#[derive(Debug, Clone, PartialEq, Eq, FromInkAttribute, FromSyntax)]
pub struct Constructor {
    /// ink! attribute IR data.
    #[arg_kind(Constructor)]
    ink_attr: InkAttrData<ast::Fn>,
}

impl IsInkFn for Constructor {
    fn fn_item(&self) -> Option<&ast::Fn> {
        self.ink_attr.parent_ast()
    }
}

impl IsInkCallable for Constructor {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::*;
    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 ink_attr = parse_first_ink_attribute(code);

            let constructor = Constructor::cast(ink_attr).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());
        }
    }
}