#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Literal;
use quote::{quote, quote_spanned};
use sha3::{Digest, Keccak256};
use syn::{
parse_macro_input, spanned::Spanned, Attribute, Expr, ExprLit, Ident, ItemEnum, Lit, LitStr,
};
struct Bytes(Vec<u8>);
impl ::std::fmt::Debug for Bytes {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
let data = &self.0;
write!(f, "[")?;
if !data.is_empty() {
write!(f, "{:#04x}u8", data[0])?;
for unit in data.iter().skip(1) {
write!(f, ", {:#04x}", unit)?;
}
}
write!(f, "]")
}
}
#[proc_macro]
pub fn keccak256(input: TokenStream) -> TokenStream {
let lit_str = parse_macro_input!(input as LitStr);
let hash = Keccak256::digest(&lit_str.value());
let bytes = Bytes(hash.to_vec());
let eval_str = format!("{:?}", bytes);
let eval_ts: proc_macro2::TokenStream = eval_str.parse().unwrap_or_else(|_| {
panic!(
"Failed to parse the string \"{}\" to TokenStream.",
eval_str
);
});
quote!(#eval_ts).into()
}
#[proc_macro_attribute]
pub fn generate_function_selector(_: TokenStream, input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as ItemEnum);
let ItemEnum {
attrs,
vis,
enum_token,
ident,
variants,
..
} = item;
let mut ident_expressions: Vec<Ident> = vec![];
let mut variant_expressions: Vec<Expr> = vec![];
let mut variant_attrs: Vec<Vec<Attribute>> = vec![];
for variant in variants {
match variant.discriminant {
Some((_, Expr::Lit(ExprLit { lit, .. }))) =>
if let Lit::Str(lit_str) = lit {
let digest = Keccak256::digest(&lit_str.value());
let selector = u32::from_be_bytes([digest[0], digest[1], digest[2], digest[3]]);
ident_expressions.push(variant.ident);
variant_expressions.push(Expr::Lit(ExprLit {
lit: Lit::Verbatim(Literal::u32_suffixed(selector)),
attrs: Default::default(),
}));
variant_attrs.push(variant.attrs);
} else {
return quote_spanned! {
lit.span() => compile_error("Expected literal string");
}
.into()
},
Some((_eg, expr)) =>
return quote_spanned! {
expr.span() => compile_error("Expected literal");
}
.into(),
None =>
return quote_spanned! {
variant.span() => compile_error("Each variant must have a discriminant");
}
.into(),
}
}
(quote! {
#(#attrs)*
#[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive)]
#[repr(u32)]
#vis #enum_token #ident {
#(
#(#variant_attrs)*
#ident_expressions = #variant_expressions,
)*
}
})
.into()
}