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
/*
    Appellation: decanter-derive <library>
    Creator: FL03 <jo3mccain@icloud.com>
*/
extern crate proc_macro;
extern crate quote;
extern crate syn;

pub(crate) mod internal;

use internal::*;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Result};

#[proc_macro_derive(Hashable, attributes(decanter))]
pub fn hashable(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);

    let gen = impl_hashable(&ast);

    gen.expect("").into()
}

/// This function is used to generate the implementation of the Hashable trait.
fn impl_hashable(ast: &DeriveInput) -> Result<proc_macro2::TokenStream> {
    let ctxt = Ctxt::new();
    let cont = match ast::Container::from_ast(&ctxt, ast) {
        Some(cont) => cont,
        None => return Err(ctxt.check().unwrap_err()),
    };
    ctxt.check()?;
    derive_hash(&cont)
}

fn derive_hash(cont: &ast::Container) -> Result<proc_macro2::TokenStream> {
    let attr = cont.attrs();
    let res = match attr.uses() {
        attr::HashType::Serde => hash_serde_body(&cont),
        attr::HashType::String => hash_string_body(&cont),
    };
    Ok(res)
}

/// This function is used to generate the implementation of the Hashable trait.
fn hash_serde_body(cont: &ast::Container) -> proc_macro2::TokenStream {
    let attr = cont.attrs();
    let decanter = attr.crate_path();
    let ident = cont.ident();
    quote! {
        impl #decanter::hash::Hashable for #ident {
            fn hash(&self) -> #decanter::hash::H256 {
                #decanter::hash::hash_serialize(&self)
            }
        }
    }
}

// This function is used to generate the implementation of the Hashable trait.
fn hash_string_body(cont: &ast::Container) -> proc_macro2::TokenStream {
    let attr = cont.attrs();
    let decanter = attr.crate_path();
    let ident = cont.ident();

    quote! {
        impl #decanter::hash::Hashable for #ident {
            fn hash(&self) -> #decanter::hash::H256 {
                #decanter::hash::hasher(self.to_string()).into()
            }
        }
    }
}