1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![doc = include_str!("../README.md")]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
    html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![warn(missing_docs, unreachable_pub, rustdoc::all)]
#![deny(unused_must_use, unused_crate_dependencies)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

extern crate proc_macro;

mod de;
mod en;
mod utils;

use de::*;
use en::*;
use proc_macro::TokenStream;

/// Derives `Encodable` for the type which encodes the all fields as list:
/// `<rlp-header, fields...>`
#[proc_macro_derive(RlpEncodable, attributes(rlp))]
pub fn encodable(input: TokenStream) -> TokenStream {
    syn::parse(input)
        .and_then(|ast| impl_encodable(&ast))
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Derives `Encodable` for the type which encodes the fields as-is, without a
/// header: `<fields...>`
#[proc_macro_derive(RlpEncodableWrapper, attributes(rlp))]
pub fn encodable_wrapper(input: TokenStream) -> TokenStream {
    syn::parse(input)
        .and_then(|ast| impl_encodable_wrapper(&ast))
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Derives `MaxEncodedLen` for types of constant size.
#[proc_macro_derive(RlpMaxEncodedLen, attributes(rlp))]
pub fn max_encoded_len(input: TokenStream) -> TokenStream {
    syn::parse(input)
        .and_then(|ast| impl_max_encoded_len(&ast))
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Derives `Decodable` for the type whose implementation expects an rlp-list
/// input: `<rlp-header, fields...>`
///
/// This is the inverse of `RlpEncodable`.
#[proc_macro_derive(RlpDecodable, attributes(rlp))]
pub fn decodable(input: TokenStream) -> TokenStream {
    syn::parse(input)
        .and_then(|ast| impl_decodable(&ast))
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}

/// Derives `Decodable` for the type whose implementation expects only the
/// individual fields encoded: `<fields...>`
///
/// This is the inverse of `RlpEncodableWrapper`.
#[proc_macro_derive(RlpDecodableWrapper, attributes(rlp))]
pub fn decodable_wrapper(input: TokenStream) -> TokenStream {
    syn::parse(input)
        .and_then(|ast| impl_decodable_wrapper(&ast))
        .unwrap_or_else(|err| err.to_compile_error())
        .into()
}