rlp_derive/
lib.rs

1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Derive macro for `#[derive(RlpEncodable, RlpDecodable)]`.
10//!
11//! For example of usage see `./tests/rlp.rs`.
12//!
13//! This library also supports up to 1 `#[rlp(default)]` in a struct,
14//! which is similar to [`#[serde(default)]`](https://serde.rs/field-attrs.html#default)
15//! with the caveat that we use the `Default` value if
16//! the field deserialization fails, as we don't serialize field
17//! names and there is no way to tell if it is present or not.
18
19#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
20
21extern crate proc_macro;
22
23mod de;
24mod en;
25
26use de::{impl_decodable, impl_decodable_wrapper};
27use en::{impl_encodable, impl_encodable_wrapper};
28use proc_macro::TokenStream;
29
30#[proc_macro_derive(RlpEncodable, attributes(rlp))]
31pub fn encodable(input: TokenStream) -> TokenStream {
32	let ast = syn::parse(input).unwrap();
33	let gen = impl_encodable(&ast);
34	gen.into()
35}
36
37#[proc_macro_derive(RlpEncodableWrapper)]
38pub fn encodable_wrapper(input: TokenStream) -> TokenStream {
39	let ast = syn::parse(input).unwrap();
40	let gen = impl_encodable_wrapper(&ast);
41	gen.into()
42}
43
44#[proc_macro_derive(RlpDecodable, attributes(rlp))]
45pub fn decodable(input: TokenStream) -> TokenStream {
46	let ast = syn::parse(input).unwrap();
47	let gen = impl_decodable(&ast);
48	gen.into()
49}
50
51#[proc_macro_derive(RlpDecodableWrapper)]
52pub fn decodable_wrapper(input: TokenStream) -> TokenStream {
53	let ast = syn::parse(input).unwrap();
54	let gen = impl_decodable_wrapper(&ast);
55	gen.into()
56}