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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use convert_case::{Case, Casing};
use gekko_metadata::{parse_hex_metadata, ModuleMetadataExt};
use proc_macro::TokenTree;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::collections::HashMap;
use std::fs::read_to_string;

#[proc_macro_attribute]
pub fn parse_from_hex_file(
    args: proc_macro::TokenStream,
    _: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    // Extract path.
    let tree = args
        .into_iter()
        .nth(0)
        .expect("Expected path literal as argument. E.g \"/path/to/file\"");

    let path = match tree {
        TokenTree::Literal(path) => path.to_string(),
        _ => panic!("Expected path literal as argument. E.g \"/path/to/file\""),
    };

    let path = path.replace("\"", "");

    // Read content from file.
    let content = read_to_string(&path).expect(&format!(
        "Failed to read runtime metadata from \"{}\"",
        path
    ));

    process_runtime_metadata(content.as_str()).into()
}

fn process_runtime_metadata(content: &str) -> TokenStream {
    // Parse runtime metadata
    let data = parse_hex_metadata(content)
        .map_err(|err| panic!("Failed to parse runtime metadata: {:?}", err))
        .unwrap()
        .into_inner();

    let mut final_extrinsics = TokenStream::new();
    let mut modules: HashMap<syn::Ident, TokenStream> = HashMap::new();
    let extrinsics = data.modules_extrinsics();

    for ext in extrinsics {
        if ext.args.len() > 25 {
            panic!("This macro does not support more than 25 generic variables");
        };

        // Create generics, assuming there any. E.g. `<A, B, C>`
        let generics: Vec<String> = ext
            .args
            .iter()
            .enumerate()
            .map(|(offset, _)| char::from_u32(65 + offset as u32).unwrap().into())
            .collect();

        let generics_wrapped = format!("<{}>", {
            let mut generics = generics
                .iter()
                .fold(String::new(), |a, b| format!("{}, {}", a, b));

            // Remove first comma, assuming generics are present.
            if !generics.is_empty() {
                generics.remove(0);
            }

            generics
        });

        // Prepare types.
        let generics_wrapped: syn::Generics = syn::parse_str(&generics_wrapped).unwrap();
        let ext_name = format_ident!("{}", Casing::to_case(ext.extrinsic_name, Case::Pascal));
        let ext_comments: Vec<String> = ext
            .documentation
            .iter()
            .map(|doc| doc.replace("[`", "`").replace("`]", "`"))
            .collect();

        // Create individual struct fields.
        let ext_args = ext
            .args
            .iter()
            .enumerate()
            .map(|(offset, (name, ty_desc))| {
                let msg = format!("Type description: `{}`", ty_desc);
                let name = format_ident!("{}", name);
                let ty = format_ident!("{}", char::from_u32(65 + offset as u32).unwrap());
                quote! {
                    #[doc = #msg]
                    pub #name: #ty,
                }
            });

        // Specialized struct field encoding used for the `parity_scale_codec::Encode` implementation.
        let ext_args_encode = ext.args.iter().map(|(name, _)| {
            let name = format_ident!("{}", name);
            quote! {
                self.#name.encode_to(&mut buffer);
            }
        });

        // Specialized struct field decoding used for the `parity_scale_codec::Decode` implementation.
        let ext_args_decode = ext.args.iter().map(|(name, _)| {
            let name = format_ident!("{}", name);
            quote! {
                #name: parity_scale_codec::Decode::decode(input)?,
            }
        });

        // Prepare documentation for type.
        let disclaimer = "# Type Disclaimer\nThis library makes no assumptions about parameter types and must be specified \
        manually as generic types. Each field contains a type description which can serve as a hint on what type is being expected, as \
        provided by the runtime meatadata. See the [`common`](crate::common) module for common types which can be used.\n";

        let docs = if !ext_comments.is_empty() {
            let intro = ext_comments.iter().nth(0).unwrap();
            let msg = "# Documentation (provided by the runtime metadata)";

            quote! {
                #[doc = #intro]
                #[doc = #msg]
                #(#[doc = #ext_comments])*
            }
        } else {
            let msg = "No documentation provided by the runtime metadata";
            quote! {
                #[doc = #msg]
            }
        };

        // Build the final type.
        let generics_idents: Vec<syn::Ident> =
            generics.iter().map(|v| format_ident!("{}", v)).collect();

        // Enums have a max size of 256. This is acknowledged in the SCALE specification.
        let ext_module_id = ext.module_id as u8;
        let ext_dispatch_id = ext.dispatch_id as u8;

        let type_stream: TokenStream = quote! {
            #docs
            #[doc = #disclaimer]
            #[derive(Debug, Clone, Eq, PartialEq)]
            pub struct #ext_name #generics_wrapped
            where
                #(#generics_idents: parity_scale_codec::Encode + parity_scale_codec::Decode, )*
            {
                #(#ext_args)*
            }

            impl #generics_wrapped parity_scale_codec::Encode for #ext_name #generics_wrapped
            where
                #(#generics_idents: parity_scale_codec::Encode + parity_scale_codec::Decode, )*
            {
                fn using_encoded<SR, SF: FnOnce(&[u8]) -> SR>(&self, f: SF) -> SR {
                    let mut buffer = vec![#ext_module_id, #ext_dispatch_id];
                    #(#ext_args_encode)*
                    f(&buffer)
                }
            }

            impl #generics_wrapped parity_scale_codec::Decode for #ext_name #generics_wrapped
            where
                #(#generics_idents: parity_scale_codec::Encode + parity_scale_codec::Decode, )*
            {
                fn decode<SI: parity_scale_codec::Input>(input: &mut SI) -> Result<Self, parity_scale_codec::Error> {
                    let mut buffer = [0; 2];
                    input.read(&mut buffer)?;

                    if buffer != [#ext_module_id, #ext_dispatch_id] {
                        return Err("Invalid identifier of the expected type.".into())
                    }

                    Ok(
                        #ext_name {
                            #(#ext_args_decode )*
                        }
                    )
                }
            }
        };

        // Add created type to the corresponding module.
        modules
            .entry(format_ident!(
                "{}",
                Casing::to_case(ext.module_name, Case::Snake)
            ))
            .and_modify(|stream| {
                stream.extend(type_stream.clone());
            })
            .or_insert(type_stream);
    }

    // Add all modules to the final stream.
    modules.iter().for_each(|(module, stream)| {
        let stream: TokenStream = quote! {
            pub mod #module {
                #stream
            }
        };

        final_extrinsics.extend(stream);
    });

    quote! {
        pub mod extrinsics {
            #final_extrinsics
        }

        /// TODO
        pub mod storage {}
        /// TODO
        pub mod events {}
        /// TODO
        pub mod constants {}
        /// TODO
        pub mod errors {}
    }
}