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
use std::convert::TryFrom;

use proc_macro2::Ident;

use crate::attrs::partition_attributes;

use super::{constructor::Constructor, method::Method};

/// An item within an implementation block
///
/// At this point there is not difference between a [Method] and a default syn::ImplItem
pub enum ImplItem {
    /// A `#[odra(init)]` marked function.
    Constructor(Constructor),
    /// Unmarked function.
    Method(Method),
    /// Any other implementation block item.
    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::Method(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(value: syn::ImplItem) -> Result<Self, Self::Error> {
        match value {
            syn::ImplItem::Method(method) => {
                let (odra_attrs, _) = partition_attributes(method.attrs.clone())?;
                if odra_attrs.is_empty() {
                    return Ok(ImplItem::Method(method.into()));
                }
                let is_constructor = odra_attrs.iter().any(|attr| attr.is_constructor());
                match is_constructor {
                    true => Ok(ImplItem::Constructor(Constructor::try_from(method)?)),
                    false => Ok(ImplItem::Method(method.into()))
                }
            }
            other_item => Ok(ImplItem::Other(other_item))
        }
    }
}

pub struct ContractEntrypoint {
    pub ident: Ident,
    pub args: Vec<syn::PatType>,
    pub ret: syn::ReturnType,
    pub full_sig: syn::Signature
}

impl From<syn::ImplItemMethod> for ContractEntrypoint {
    fn from(method: syn::ImplItemMethod) -> Self {
        let ident = method.sig.ident.to_owned();
        let args = method
            .sig
            .inputs
            .iter()
            .filter_map(|arg| match arg {
                syn::FnArg::Receiver(_) => None,
                syn::FnArg::Typed(pat) => Some(pat.clone())
            })
            .collect::<Vec<_>>();
        let ret = method.clone().sig.output;
        let full_sig = method.sig;
        Self {
            ident,
            args,
            ret,
            full_sig
        }
    }
}

#[cfg(test)]
mod test {
    use std::convert::TryFrom;

    use super::ImplItem;

    macro_rules! assert_enum_variant {
        ($v:expr, $p:pat) => {
            assert!(if let $p = $v { true } else { false })
        };
    }

    #[test]
    fn test_parse_fn_without_odra_attr() {
        let item: syn::ImplItem = syn::parse_quote! {
            #[some(a)]
            pub fn set_initial_value(&self, value: u32) {
                self.set_value(value);
            }
        };
        let parsed = ImplItem::try_from(item);
        assert_enum_variant!(parsed.unwrap(), ImplItem::Method(_));
    }

    #[test]
    fn test_parse_fn_without_any_attr() {
        let item: syn::ImplItem = syn::parse_quote! {
            pub fn set_initial_value(&self, value: u32) {
                self.set_value(value);
            }
        };
        let parsed = ImplItem::try_from(item);
        assert_enum_variant!(parsed.unwrap(), ImplItem::Method(_));
    }

    #[test]
    fn test_parse_fn_with_odra_init_attr() {
        let item: syn::ImplItem = syn::parse_quote! {
            #[odra(init)]
            pub fn set_initial_value(&self, value: u32) {
                self.set_value(value);
            }
        };
        let parsed = ImplItem::try_from(item);
        assert_enum_variant!(parsed.unwrap(), ImplItem::Constructor(_));
    }

    #[test]
    fn test_parse_other_impl_item() {
        let item: syn::ImplItem = syn::parse_quote! {
            const A: i32 = 3;
        };
        let parsed = ImplItem::try_from(item);
        assert_enum_variant!(parsed.unwrap(), ImplItem::Other(_));
    }
}