1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use proc_macro::TokenStream;
use quote::quote;
use syn::{Error, LitStr};

#[proc_macro]
pub fn keccak(input_stream: TokenStream) -> TokenStream {
    let ast: LitStr = match syn::parse(input_stream) {
        Ok(ast) => ast,
        Err(err) => {
            return Error::new(
                err.span(),
                "only accept literature string input like \"foo\" as input",
            )
            .into_compile_error()
            .into();
        }
    };
    let hash: [u8; 32] = keccak_hash::keccak(ast.value().as_bytes()).0;
    let bytes = hash.as_ref().iter();
    let output = quote! {
        [#(#bytes,)*]
    };
    output.into()
}