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
use proc_macro::TokenStream;

mod assertions;
mod config;
mod errors;
mod handler;
mod store;

#[proc_macro_attribute]
pub fn map(_args: TokenStream, item: TokenStream) -> TokenStream {
    handler::main(item.into(), config::ModuleType::Map).into()
}

#[proc_macro_attribute]
pub fn store(_args: TokenStream, item: TokenStream) -> TokenStream {
    handler::main(item.into(), config::ModuleType::Store).into()
}

// todo: remove this once satisfied with implementation of StoreDelete
#[proc_macro_derive(StoreWriter)]
pub fn derive(input: TokenStream) -> TokenStream {
    store::main(input)
}

#[cfg(test)]
mod test {
    use crate::{assertions::assert_ast_eq, config::ModuleType, handler::main};
    use quote::quote;

    #[test]
    fn test_map_plain() {
        let item = quote! {
            fn map_transfers(blk: eth::Block) -> pb::Custom {
                unimplemented!("do something");
            }
        };

        assert_ast_eq(
            main(item, ModuleType::Map).into(),
            quote! {
                #[no_mangle]
                pub extern "C" fn map_transfers(blk_ptr: *mut u8, blk_len: usize) {
                    substreams::register_panic_hook();
                    let func = || -> pb::Custom {
                        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len)
                            .unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", blk_len, stringify!(eth::Block)));
                        let result = {
                            unimplemented!("do something");
                        };
                        result
                    };
                    let result = func();
                    substreams::output(result);
                }
            },
        );
    }

    #[test]
    fn test_map_mut() {
        let item = quote! {
            fn map_transfers(mut blk: eth::Block) -> pb::Custom {
                unimplemented!("do something");
            }
        };

        assert_ast_eq(
            main(item, ModuleType::Map).into(),
            quote! {
                #[no_mangle]
                pub extern "C" fn map_transfers(blk_ptr: *mut u8, blk_len: usize) {
                    substreams::register_panic_hook();
                    let func = || -> pb::Custom {
                        let mut blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len)
                            .unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", blk_len, stringify!(eth::Block)));
                        let result = {
                            unimplemented!("do something");
                        };
                        result
                    };
                    let result = func();
                    substreams::output(result);
                }
            },
        );
    }

    #[test]
    fn test_map_option() {
        let item = quote! {
            fn map_transfers(blk: eth::Block) -> Option<pb::Custom> {
                unimplemented!("do something");
            }
        };

        assert_ast_eq(
            main(item, ModuleType::Map).into(),
            quote! {
                #[no_mangle]
                pub extern "C" fn map_transfers(blk_ptr: *mut u8, blk_len: usize) {
                    substreams::register_panic_hook();
                    let func = || -> Option<pb::Custom> {
                        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len)
                            .unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", blk_len, stringify!(eth::Block)));
                        let result = { unimplemented!("do something"); };
                        result
                    };

                    let result = func();
                    if let Some(value) = result {
                        substreams::output(value);
                    }
                }
            },
        );
    }

    #[test]
    fn test_map_result() {
        let item = quote! {
            fn map_transfers(blk: eth::Block) -> Result<pb::Custom> {
                unimplemented!("do something");
            }
        };

        assert_ast_eq(
            main(item, ModuleType::Map).into(),
            quote! {
                #[no_mangle]
                pub extern "C" fn map_transfers(blk_ptr: *mut u8, blk_len: usize) {
                    substreams::register_panic_hook();
                    let func = || -> Result<pb::Custom> {
                        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len)
                            .unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", blk_len, stringify!(eth::Block)));
                        let result = { unimplemented!("do something"); };
                        result
                    };

                    let result = func();
                    if result.is_err() {
                        panic!("{:?}", result.unwrap_err())
                    }
                    substreams::output(result.expect("already checked that result is not an error"));
                }
            },
        );
    }

    #[test]
    fn test_map_result_option() {
        let item = quote! {
            fn map_transfers(blk: eth::Block) -> Result<Option<pb::Custom>> {
                unimplemented!("do something");
            }
        };

        assert_ast_eq(
            main(item, ModuleType::Map).into(),
            quote! {
                #[no_mangle]
                pub extern "C" fn map_transfers(blk_ptr: *mut u8, blk_len: usize) {
                    substreams::register_panic_hook();
                    let func = || -> Result<Option<pb::Custom> > {
                        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len)
                            .unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", blk_len, stringify!(eth::Block)));
                        let result = { unimplemented!("do something"); };
                        result
                    };

                    let result = func();
                    if result.is_err() {
                        panic!("{:?}", result.unwrap_err())
                    }
                    if let Some(inner) = result.expect("already checked that result is not an error") {
                        substreams::output(inner);
                    }
                }
            },
        );
    }
}