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
#[macro_export]
macro_rules! match_method {
    ($method:expr, {$($body:tt)*}) => {
        match_method!{@match $method, {}, $($body)*}
    };
    (@match $method:expr, {$($body:tt)*}, $(,)*) => {
        match $method {
            $($body)*
        }
    };
    // matches block with comma
    (@match $method:expr, {$($body:tt)*}, $p:literal => $e:expr, $($tail:tt)*) => {
        match_method! {
            @match
            $method,
            {
                $($body)*
                $crate::method_hash!($p) => $e,
            },
            $($tail)*
        }
    };
    // matches block without comma
    (@match $method:expr, {$($body:tt)*}, $p:literal => $e:block $($tail:tt)*) => {
        match_method! {
            @match
            $method,
            {
                $($body)*
                $crate::method_hash!($p) => $e,
            },
            $($tail)*
        }
    };
    // matches _ with a trailing comma
    (@match $method:expr, {$($body:tt)*}, _ => $e:expr, $($tail:tt)*) => {
        match_method! {
            @match
            $method,
            {
                $($body)*
                _ => $e,
            },
            $($tail)*
        }
    };
    // matches _ without a trailing comma (common if it's the last item)
    (@match $method:expr, {$($body:tt)*}, _ => $e:expr) => {
        match_method! {
            @match
            $method,
            {
                $($body)*
                _ => $e,
            },
        }
    };
}

#[cfg(test)]
mod tests {
    #[test]
    fn handle_constructor() {
        let method_num = 1u64; // constructor should always hash to 1
        let ret = match_method!(method_num, {
            "Constructor" => Some(1),
            _ => None,
        });

        assert_eq!(ret, Some(1));
    }

    #[test]
    fn handle_unknown_method() {
        let method_num = 12345u64; // not a method we know about
        let ret = match_method!(method_num, {
            "Constructor" => Some(1),
            _ => None,
        });

        assert_eq!(ret, None);
    }

    #[test]
    fn handle_user_method() {
        let method_num = crate::method_hash!("TokensReceived");
        let ret = match_method!(method_num, {
            "Constructor" => Some(1),
            "TokensReceived" => Some(2),
            _ => None,
        });

        assert_eq!(ret, Some(2));
    }

    #[test]
    fn handle_optional_commas() {
        let method_num = crate::method_hash!("TokensReceived");
        let ret = match_method!(method_num, {
            "Constructor" => Some(1),
            "TokensReceived" => {
                Some(2)
            }
            _ => {
                None
            }
        });

        assert_eq!(ret, Some(2));
    }
}