Add field idents support to ssz_derive.
- Adds idents to skip ser, deser and tree hashing
This commit is contained in:
parent
563304c8d7
commit
dffc26a466
@ -56,10 +56,46 @@ fn get_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn::
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
|
||||||
/// Implements `ssz::Encodable` for some `struct`.
|
/// Implements `ssz::Encodable` for some `struct`.
|
||||||
///
|
///
|
||||||
/// Fields are encoded in the order they are defined.
|
/// Fields are encoded in the order they are defined.
|
||||||
#[proc_macro_derive(Encode)]
|
#[proc_macro_derive(Encode, attributes(ssz))]
|
||||||
pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
|
pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
|
||||||
let item = parse_macro_input!(input as DeriveInput);
|
let item = parse_macro_input!(input as DeriveInput);
|
||||||
|
|
||||||
@ -70,7 +106,7 @@ pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
|
|||||||
_ => panic!("ssz_derive only supports structs."),
|
_ => panic!("ssz_derive only supports structs."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let field_idents = get_named_field_idents(&struct_data);
|
let field_idents = get_serializable_named_field_idents(&struct_data);
|
||||||
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
impl ssz::Encodable for #name {
|
impl ssz::Encodable for #name {
|
||||||
@ -84,6 +120,18 @@ pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
|
|||||||
output.into()
|
output.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
|
||||||
/// Implements `ssz::Decodable` for some `struct`.
|
/// Implements `ssz::Decodable` for some `struct`.
|
||||||
///
|
///
|
||||||
/// Fields are decoded in the order they are defined.
|
/// Fields are decoded in the order they are defined.
|
||||||
@ -98,26 +146,39 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
|
|||||||
_ => panic!("ssz_derive only supports structs."),
|
_ => panic!("ssz_derive only supports structs."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let field_idents = get_named_field_idents(&struct_data);
|
let all_idents = get_named_field_idents(&struct_data);
|
||||||
|
|
||||||
// Using a var in an iteration always consumes the var, therefore we must make a `fields_a` and
|
// Build quotes for fields that should be deserialized and those that should be built from
|
||||||
// a `fields_b` in order to perform two loops.
|
// `Default`.
|
||||||
//
|
let mut quotes = vec![];
|
||||||
// https://github.com/dtolnay/quote/issues/8
|
for field in &struct_data.fields {
|
||||||
let field_idents_a = &field_idents;
|
match &field.ident {
|
||||||
let field_idents_b = &field_idents;
|
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."),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
impl ssz::Decodable for #name {
|
impl ssz::Decodable for #name {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), ssz::DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), ssz::DecodeError> {
|
||||||
#(
|
#(
|
||||||
let (#field_idents_a, i) = <_>::ssz_decode(bytes, i)?;
|
#quotes
|
||||||
)*
|
)*
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Self {
|
Self {
|
||||||
#(
|
#(
|
||||||
#field_idents_b,
|
#all_idents,
|
||||||
)*
|
)*
|
||||||
},
|
},
|
||||||
i
|
i
|
||||||
@ -128,10 +189,46 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
|
|||||||
output.into()
|
output.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a Vec of `syn::Ident` for each named field in the struct, whilst filtering out fields
|
||||||
|
/// that should not be tree hashed.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
|
||||||
|
fn get_tree_hashable_named_field_idents<'a>(
|
||||||
|
struct_data: &'a syn::DataStruct,
|
||||||
|
) -> Vec<&'a syn::Ident> {
|
||||||
|
struct_data
|
||||||
|
.fields
|
||||||
|
.iter()
|
||||||
|
.filter_map(|f| {
|
||||||
|
if should_skip_tree_hash(&f) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(match &f.ident {
|
||||||
|
Some(ref ident) => ident,
|
||||||
|
_ => panic!("ssz_derive only supports named struct fields."),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if some field has an attribute declaring it should not be tree-hashed.
|
||||||
|
///
|
||||||
|
/// The field attribute is: `#[tree_hash(skip_hashing)]`
|
||||||
|
fn should_skip_tree_hash(field: &syn::Field) -> bool {
|
||||||
|
for attr in &field.attrs {
|
||||||
|
if attr.tts.to_string() == "( skip_hashing )" {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
/// Implements `ssz::TreeHash` for some `struct`.
|
/// Implements `ssz::TreeHash` for some `struct`.
|
||||||
///
|
///
|
||||||
/// Fields are processed in the order they are defined.
|
/// Fields are processed in the order they are defined.
|
||||||
#[proc_macro_derive(TreeHash)]
|
#[proc_macro_derive(TreeHash, attributes(tree_hash))]
|
||||||
pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream {
|
pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream {
|
||||||
let item = parse_macro_input!(input as DeriveInput);
|
let item = parse_macro_input!(input as DeriveInput);
|
||||||
|
|
||||||
@ -142,7 +239,7 @@ pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream {
|
|||||||
_ => panic!("ssz_derive only supports structs."),
|
_ => panic!("ssz_derive only supports structs."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let field_idents = get_named_field_idents(&struct_data);
|
let field_idents = get_tree_hashable_named_field_idents(&struct_data);
|
||||||
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
impl ssz::TreeHash for #name {
|
impl ssz::TreeHash for #name {
|
||||||
|
Loading…
Reference in New Issue
Block a user