numext_constructor/
lib.rs

1// Copyright 2018-2019 Cryptape Technologies LLC.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#![recursion_limit = "1024"]
10
11extern crate proc_macro;
12#[macro_use]
13extern crate quote;
14
15#[macro_use]
16mod utils;
17
18mod definition;
19mod fixed_hash;
20mod fixed_uint;
21
22use quote::quote;
23use syn::parse_macro_input;
24
25#[proc_macro]
26pub fn construct_fixed_uints(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
27    let inputs = parse_macro_input!(input as definition::Definitions);
28    let expanded = {
29        inputs
30            .inner
31            .into_iter()
32            .map(|input| {
33                let parsed: fixed_uint::parsed::UintDefinition = input.into();
34                fixed_uint::core::UintConstructor::new(parsed)
35            })
36            .fold((quote!(), Vec::new()), |(uints, mut ucs), uc| {
37                let (uint, public) = uc.construct_all(&ucs[..]);
38                let uints = quote!(#uints #public #uint);
39                ucs.push(uc);
40                (uints, ucs)
41            })
42            .0
43    };
44    expanded.into()
45}
46
47#[proc_macro]
48pub fn construct_fixed_hashes(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
49    let inputs = parse_macro_input!(input as definition::Definitions);
50    let expanded = {
51        inputs
52            .inner
53            .into_iter()
54            .map(|input| {
55                let parsed: fixed_hash::parsed::HashDefinition = input.into();
56                fixed_hash::core::HashConstructor::new(parsed)
57            })
58            .fold((quote!(), Vec::new()), |(hashes, mut ucs), uc| {
59                let (hash, public) = uc.construct_all(&ucs[..]);
60                let hashes = quote!(#hashes #public #hash);
61                ucs.push(uc);
62                (hashes, ucs)
63            })
64            .0
65    };
66    expanded.into()
67}