2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2018-06-04 10:19:50 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-06-04 10:19:50 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-06-04 10:19:50 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-04 10:19:50 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use proc_macro2::{Span, TokenStream};
|
2018-09-13 21:37:28 +02:00
|
|
|
use syn;
|
2017-08-20 06:01:46 +02:00
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
pub fn impl_encodable(ast: &syn::DeriveInput) -> TokenStream {
|
2020-08-05 06:08:03 +02:00
|
|
|
let body = match ast.data {
|
|
|
|
syn::Data::Struct(ref s) => s,
|
|
|
|
_ => panic!("#[derive(RlpEncodable)] is only defined for structs."),
|
|
|
|
};
|
|
|
|
|
|
|
|
let stmts: Vec<_> = body
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(encodable_field_map)
|
|
|
|
.collect();
|
|
|
|
let name = &ast.ident;
|
|
|
|
|
|
|
|
let stmts_len = stmts.len();
|
|
|
|
let stmts_len = quote! { #stmts_len };
|
|
|
|
let dummy_const = syn::Ident::new(
|
|
|
|
&format!("_IMPL_RLP_ENCODABLE_FOR_{}", name),
|
|
|
|
Span::call_site(),
|
|
|
|
);
|
|
|
|
let impl_block = quote! {
|
|
|
|
impl rlp::Encodable for #name {
|
|
|
|
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
|
|
|
|
stream.begin_list(#stmts_len);
|
|
|
|
#(#stmts)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
|
|
|
const #dummy_const: () = {
|
|
|
|
extern crate rlp;
|
|
|
|
#impl_block
|
|
|
|
};
|
|
|
|
}
|
2017-08-20 06:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
pub fn impl_encodable_wrapper(ast: &syn::DeriveInput) -> TokenStream {
|
2020-08-05 06:08:03 +02:00
|
|
|
let body = match ast.data {
|
|
|
|
syn::Data::Struct(ref s) => s,
|
|
|
|
_ => panic!("#[derive(RlpEncodableWrapper)] is only defined for structs."),
|
|
|
|
};
|
|
|
|
|
|
|
|
let stmt = {
|
|
|
|
let fields: Vec<_> = body.fields.iter().collect();
|
|
|
|
if fields.len() == 1 {
|
|
|
|
let field = fields.first().expect("fields.len() == 1; qed");
|
|
|
|
encodable_field(0, field)
|
|
|
|
} else {
|
|
|
|
panic!("#[derive(RlpEncodableWrapper)] is only defined for structs with one field.")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = &ast.ident;
|
|
|
|
|
|
|
|
let dummy_const = syn::Ident::new(
|
|
|
|
&format!("_IMPL_RLP_ENCODABLE_FOR_{}", name),
|
|
|
|
Span::call_site(),
|
|
|
|
);
|
|
|
|
let impl_block = quote! {
|
|
|
|
impl rlp::Encodable for #name {
|
|
|
|
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
|
|
|
|
#stmt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
|
|
|
const #dummy_const: () = {
|
|
|
|
extern crate rlp;
|
|
|
|
#impl_block
|
|
|
|
};
|
|
|
|
}
|
2017-08-20 06:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
fn encodable_field_map(tuple: (usize, &syn::Field)) -> TokenStream {
|
2020-08-05 06:08:03 +02:00
|
|
|
encodable_field(tuple.0, tuple.1)
|
2017-08-20 06:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
fn encodable_field(index: usize, field: &syn::Field) -> TokenStream {
|
2020-08-05 06:08:03 +02:00
|
|
|
let ident = match field.ident {
|
|
|
|
Some(ref ident) => quote! { #ident },
|
|
|
|
None => {
|
|
|
|
let index: syn::Index = index.into();
|
|
|
|
quote! { #index }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let id = quote! { self.#ident };
|
|
|
|
|
|
|
|
match field.ty {
|
|
|
|
syn::Type::Path(ref path) => {
|
|
|
|
let top_segment = path
|
|
|
|
.path
|
|
|
|
.segments
|
|
|
|
.first()
|
|
|
|
.expect("there must be at least 1 segment");
|
|
|
|
let ident = &top_segment.value().ident;
|
|
|
|
if &ident.to_string() == "Vec" {
|
|
|
|
let inner_ident = match top_segment.value().arguments {
|
|
|
|
syn::PathArguments::AngleBracketed(ref angle) => {
|
|
|
|
let ty = angle
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.expect("Vec has only one angle bracketed type; qed");
|
|
|
|
match **ty.value() {
|
|
|
|
syn::GenericArgument::Type(syn::Type::Path(ref path)) => {
|
|
|
|
&path
|
|
|
|
.path
|
|
|
|
.segments
|
|
|
|
.first()
|
|
|
|
.expect("there must be at least 1 segment")
|
|
|
|
.value()
|
|
|
|
.ident
|
|
|
|
}
|
|
|
|
_ => panic!("rlp_derive not supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!("Vec has only one angle bracketed type; qed"),
|
|
|
|
};
|
|
|
|
quote! { stream.append_list::<#inner_ident, _>(&#id); }
|
|
|
|
} else {
|
|
|
|
quote! { stream.append(&#id); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!("rlp_derive not supported"),
|
|
|
|
}
|
2017-08-20 06:01:46 +02:00
|
|
|
}
|