2019-02-19 02:53:05 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
2019-04-17 11:21:07 +00:00
|
|
|
use quote::quote;
|
2019-02-19 02:53:05 +00:00
|
|
|
use syn::{parse_macro_input, DeriveInput};
|
|
|
|
|
2019-05-04 04:11:48 +00:00
|
|
|
/*
|
2019-02-19 22:23:35 +00:00
|
|
|
/// Returns a Vec of `syn::Ident` for each named field in the struct.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
|
2019-02-19 02:53:05 +00:00
|
|
|
fn get_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn::Ident> {
|
2019-02-19 05:04:29 +00:00
|
|
|
struct_data
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.map(|f| match &f.ident {
|
2019-02-19 02:53:05 +00:00
|
|
|
Some(ref ident) => ident,
|
2019-02-19 05:04:29 +00:00
|
|
|
_ => panic!("ssz_derive only supports named struct fields."),
|
|
|
|
})
|
|
|
|
.collect()
|
2019-02-19 02:53:05 +00:00
|
|
|
}
|
2019-05-04 04:11:48 +00:00
|
|
|
*/
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
/// Returns a Vec of `syn::Ident` for each named field in the struct, whilst filtering out fields
|
|
|
|
/// that should not be serialized.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
|
|
|
|
fn get_serializable_named_field_idents<'a>(
|
|
|
|
struct_data: &'a syn::DataStruct,
|
|
|
|
) -> Vec<&'a syn::Ident> {
|
|
|
|
struct_data
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| {
|
|
|
|
if should_skip_serializing(&f) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(match &f.ident {
|
|
|
|
Some(ref ident) => ident,
|
|
|
|
_ => panic!("ssz_derive only supports named struct fields."),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-05-04 04:11:48 +00:00
|
|
|
/// Returns a Vec of `syn::Ident` for each named field in the struct, whilst filtering out fields
|
|
|
|
/// that should not be serialized.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
|
|
|
|
fn get_serializable_named_field_types<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn::Type> {
|
|
|
|
struct_data
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| {
|
|
|
|
if should_skip_serializing(&f) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(&f.ty)
|
|
|
|
/*
|
|
|
|
Some(match &f.ident {
|
|
|
|
Some(ref ident) => ident,
|
|
|
|
_ => panic!("ssz_derive only supports named struct fields."),
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
/// Returns true if some field has an attribute declaring it should not be serialized.
|
|
|
|
///
|
|
|
|
/// The field attribute is: `#[ssz(skip_serializing)]`
|
|
|
|
fn should_skip_serializing(field: &syn::Field) -> bool {
|
|
|
|
for attr in &field.attrs {
|
|
|
|
if attr.tts.to_string() == "( skip_serializing )" {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:12:18 +00:00
|
|
|
/// Implements `ssz::Encodable` for some `struct`.
|
2019-02-19 07:43:09 +00:00
|
|
|
///
|
|
|
|
/// Fields are encoded in the order they are defined.
|
2019-03-15 07:33:32 +00:00
|
|
|
#[proc_macro_derive(Encode, attributes(ssz))]
|
2019-02-19 02:53:05 +00:00
|
|
|
pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
|
|
|
|
let item = parse_macro_input!(input as DeriveInput);
|
|
|
|
|
|
|
|
let name = &item.ident;
|
|
|
|
|
|
|
|
let struct_data = match &item.data {
|
|
|
|
syn::Data::Struct(s) => s,
|
2019-02-19 05:04:29 +00:00
|
|
|
_ => panic!("ssz_derive only supports structs."),
|
2019-02-19 02:53:05 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
let field_idents = get_serializable_named_field_idents(&struct_data);
|
2019-05-04 04:11:48 +00:00
|
|
|
let field_types_a = get_serializable_named_field_types(&struct_data);
|
|
|
|
let field_types_b = field_types_a.clone();
|
2019-02-19 02:53:05 +00:00
|
|
|
|
|
|
|
let output = quote! {
|
2019-02-19 22:06:03 +00:00
|
|
|
impl ssz::Encodable for #name {
|
2019-05-04 04:11:48 +00:00
|
|
|
fn is_ssz_fixed_len() -> bool {
|
|
|
|
#(
|
|
|
|
<#field_types_a as ssz::Encodable>::is_ssz_fixed_len() &&
|
|
|
|
)*
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ssz_fixed_len() -> usize {
|
|
|
|
if Self::is_ssz_fixed_len() {
|
|
|
|
#(
|
|
|
|
<#field_types_b as ssz::Encodable>::ssz_fixed_len() +
|
|
|
|
)*
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
ssz::BYTES_PER_LENGTH_OFFSET
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_ssz_bytes(&self) -> Vec<u8> {
|
|
|
|
let mut stream = ssz::SszStream::new();
|
|
|
|
|
2019-02-19 02:53:05 +00:00
|
|
|
#(
|
2019-05-04 04:11:48 +00:00
|
|
|
stream.append(&self.#field_idents);
|
2019-02-19 02:53:05 +00:00
|
|
|
)*
|
2019-05-04 04:11:48 +00:00
|
|
|
|
|
|
|
stream.drain()
|
2019-02-19 02:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.into()
|
|
|
|
}
|
|
|
|
|
2019-05-04 04:11:48 +00:00
|
|
|
/*
|
2019-03-15 07:33:32 +00:00
|
|
|
/// Returns true if some field has an attribute declaring it should not be deserialized.
|
|
|
|
///
|
|
|
|
/// The field attribute is: `#[ssz(skip_deserializing)]`
|
|
|
|
fn should_skip_deserializing(field: &syn::Field) -> bool {
|
|
|
|
for attr in &field.attrs {
|
|
|
|
if attr.tts.to_string() == "( skip_deserializing )" {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:12:18 +00:00
|
|
|
/// Implements `ssz::Decodable` for some `struct`.
|
2019-02-19 07:43:09 +00:00
|
|
|
///
|
|
|
|
/// Fields are decoded in the order they are defined.
|
2019-02-19 02:53:05 +00:00
|
|
|
#[proc_macro_derive(Decode)]
|
|
|
|
pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
|
|
|
|
let item = parse_macro_input!(input as DeriveInput);
|
|
|
|
|
|
|
|
let name = &item.ident;
|
|
|
|
|
|
|
|
let struct_data = match &item.data {
|
|
|
|
syn::Data::Struct(s) => s,
|
2019-02-19 05:04:29 +00:00
|
|
|
_ => panic!("ssz_derive only supports structs."),
|
2019-02-19 02:53:05 +00:00
|
|
|
};
|
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
let all_idents = get_named_field_idents(&struct_data);
|
2019-02-19 02:53:05 +00:00
|
|
|
|
2019-03-15 07:33:32 +00:00
|
|
|
// Build quotes for fields that should be deserialized and those that should be built from
|
|
|
|
// `Default`.
|
|
|
|
let mut quotes = vec![];
|
|
|
|
for field in &struct_data.fields {
|
|
|
|
match &field.ident {
|
|
|
|
Some(ref ident) => {
|
|
|
|
if should_skip_deserializing(field) {
|
|
|
|
quotes.push(quote! {
|
|
|
|
let #ident = <_>::default();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
quotes.push(quote! {
|
|
|
|
let (#ident, i) = <_>::ssz_decode(bytes, i)?;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!("ssz_derive only supports named struct fields."),
|
|
|
|
};
|
|
|
|
}
|
2019-02-19 02:53:05 +00:00
|
|
|
|
|
|
|
let output = quote! {
|
2019-02-19 22:06:03 +00:00
|
|
|
impl ssz::Decodable for #name {
|
|
|
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), ssz::DecodeError> {
|
2019-02-19 02:53:05 +00:00
|
|
|
#(
|
2019-03-15 07:33:32 +00:00
|
|
|
#quotes
|
2019-02-19 02:53:05 +00:00
|
|
|
)*
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Self {
|
|
|
|
#(
|
2019-03-15 07:33:32 +00:00
|
|
|
#all_idents,
|
2019-02-19 02:53:05 +00:00
|
|
|
)*
|
|
|
|
},
|
|
|
|
i
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
output.into()
|
|
|
|
}
|
2019-05-04 04:11:48 +00:00
|
|
|
*/
|