numext_constructor/
lib.rs

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
// Copyright 2018-2019 Cryptape Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![recursion_limit = "1024"]

extern crate proc_macro;
#[macro_use]
extern crate quote;

#[macro_use]
mod utils;

mod definition;
mod fixed_hash;
mod fixed_uint;

use quote::quote;
use syn::parse_macro_input;

#[proc_macro]
pub fn construct_fixed_uints(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let inputs = parse_macro_input!(input as definition::Definitions);
    let expanded = {
        inputs
            .inner
            .into_iter()
            .map(|input| {
                let parsed: fixed_uint::parsed::UintDefinition = input.into();
                fixed_uint::core::UintConstructor::new(parsed)
            })
            .fold((quote!(), Vec::new()), |(uints, mut ucs), uc| {
                let (uint, public) = uc.construct_all(&ucs[..]);
                let uints = quote!(#uints #public #uint);
                ucs.push(uc);
                (uints, ucs)
            })
            .0
    };
    expanded.into()
}

#[proc_macro]
pub fn construct_fixed_hashes(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let inputs = parse_macro_input!(input as definition::Definitions);
    let expanded = {
        inputs
            .inner
            .into_iter()
            .map(|input| {
                let parsed: fixed_hash::parsed::HashDefinition = input.into();
                fixed_hash::core::HashConstructor::new(parsed)
            })
            .fold((quote!(), Vec::new()), |(hashes, mut ucs), uc| {
                let (hash, public) = uc.construct_all(&ucs[..]);
                let hashes = quote!(#hashes #public #hash);
                ucs.push(uc);
                (hashes, ucs)
            })
            .0
    };
    expanded.into()
}