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
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
use syn;
|
|
|
|
use proc_macro2::{TokenStream, Span};
|
2017-08-20 06:01:46 +02:00
|
|
|
|
|
|
|
struct ParseQuotes {
|
2018-09-13 21:37:28 +02:00
|
|
|
single: TokenStream,
|
|
|
|
list: TokenStream,
|
2017-08-20 06:01:46 +02:00
|
|
|
takes_index: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decodable_parse_quotes() -> ParseQuotes {
|
|
|
|
ParseQuotes {
|
|
|
|
single: quote! { rlp.val_at },
|
|
|
|
list: quote! { rlp.list_at },
|
|
|
|
takes_index: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decodable_wrapper_parse_quotes() -> ParseQuotes {
|
|
|
|
ParseQuotes {
|
|
|
|
single: quote! { rlp.as_val },
|
|
|
|
list: quote! { rlp.as_list },
|
|
|
|
takes_index: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
pub fn impl_decodable(ast: &syn::DeriveInput) -> TokenStream {
|
2018-03-13 12:43:24 +01:00
|
|
|
let body = match ast.data {
|
|
|
|
syn::Data::Struct(ref s) => s,
|
2017-08-20 06:01:46 +02:00
|
|
|
_ => panic!("#[derive(RlpDecodable)] is only defined for structs."),
|
|
|
|
};
|
|
|
|
|
2018-03-13 12:43:24 +01:00
|
|
|
let stmts: Vec<_> = body.fields.iter().enumerate().map(decodable_field_map).collect();
|
2017-08-20 06:01:46 +02:00
|
|
|
let name = &ast.ident;
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
let dummy_const = syn::Ident::new(&format!("_IMPL_RLP_DECODABLE_FOR_{}", name), Span::call_site());
|
2017-08-20 06:01:46 +02:00
|
|
|
let impl_block = quote! {
|
|
|
|
impl rlp::Decodable for #name {
|
2018-04-16 15:52:12 +02:00
|
|
|
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
|
2017-08-20 06:01:46 +02:00
|
|
|
let result = #name {
|
|
|
|
#(#stmts)*
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
|
|
|
const #dummy_const: () = {
|
|
|
|
extern crate rlp;
|
|
|
|
#impl_block
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
pub fn impl_decodable_wrapper(ast: &syn::DeriveInput) -> TokenStream {
|
2018-03-13 12:43:24 +01:00
|
|
|
let body = match ast.data {
|
|
|
|
syn::Data::Struct(ref s) => s,
|
2017-08-20 06:01:46 +02:00
|
|
|
_ => panic!("#[derive(RlpDecodableWrapper)] is only defined for structs."),
|
|
|
|
};
|
|
|
|
|
2018-03-13 12:43:24 +01:00
|
|
|
let stmt = {
|
|
|
|
let fields: Vec<_> = body.fields.iter().collect();
|
|
|
|
if fields.len() == 1 {
|
|
|
|
let field = fields.first().expect("fields.len() == 1; qed");
|
|
|
|
decodable_field(0, field, decodable_wrapper_parse_quotes())
|
|
|
|
} else {
|
|
|
|
panic!("#[derive(RlpEncodableWrapper)] is only defined for structs with one field.")
|
|
|
|
}
|
2017-08-20 06:01:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let name = &ast.ident;
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
let dummy_const = syn::Ident::new(&format!("_IMPL_RLP_DECODABLE_FOR_{}", name), Span::call_site());
|
2017-08-20 06:01:46 +02:00
|
|
|
let impl_block = quote! {
|
|
|
|
impl rlp::Decodable for #name {
|
2018-04-16 15:52:12 +02:00
|
|
|
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
|
2017-08-20 06:01:46 +02:00
|
|
|
let result = #name {
|
|
|
|
#stmt
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
|
|
|
const #dummy_const: () = {
|
|
|
|
extern crate rlp;
|
|
|
|
#impl_block
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
fn decodable_field_map(tuple: (usize, &syn::Field)) -> TokenStream {
|
2017-08-20 06:01:46 +02:00
|
|
|
decodable_field(tuple.0, tuple.1, decodable_parse_quotes())
|
|
|
|
}
|
|
|
|
|
2018-09-13 21:37:28 +02:00
|
|
|
fn decodable_field(index: usize, field: &syn::Field, quotes: ParseQuotes) -> TokenStream {
|
2018-03-13 12:43:24 +01:00
|
|
|
let id = match field.ident {
|
|
|
|
Some(ref ident) => quote! { #ident },
|
|
|
|
None => {
|
|
|
|
let index: syn::Index = index.into();
|
|
|
|
quote! { #index }
|
|
|
|
}
|
2017-08-20 06:01:46 +02:00
|
|
|
};
|
|
|
|
|
2018-03-13 12:43:24 +01:00
|
|
|
let index = quote! { #index };
|
2017-08-20 06:01:46 +02:00
|
|
|
|
|
|
|
let single = quotes.single;
|
|
|
|
let list = quotes.list;
|
|
|
|
|
|
|
|
match field.ty {
|
2018-03-13 12:43:24 +01:00
|
|
|
syn::Type::Path(ref path) => {
|
|
|
|
let ident = &path.path.segments.first().expect("there must be at least 1 segment").value().ident;
|
2017-08-20 06:01:46 +02:00
|
|
|
if &ident.to_string() == "Vec" {
|
|
|
|
if quotes.takes_index {
|
|
|
|
quote! { #id: #list(#index)?, }
|
|
|
|
} else {
|
|
|
|
quote! { #id: #list()?, }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if quotes.takes_index {
|
|
|
|
quote! { #id: #single(#index)?, }
|
|
|
|
} else {
|
|
|
|
quote! { #id: #single()?, }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => panic!("rlp_derive not supported"),
|
|
|
|
}
|
|
|
|
}
|