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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#[cfg(test)]
mod tests;
mod attributes;
mod context;
mod field;
mod keyword;
mod path;
mod utils;
use attributes::ContainerAttributes;
use context::Context;
use proc_macro::TokenStream;
use quote::quote;
use std::borrow::Cow;
use syn::parse_macro_input;
#[proc_macro_attribute]
pub fn binding(attr: TokenStream, item: TokenStream) -> TokenStream {
let container_attributes = parse_macro_input!(attr as ContainerAttributes);
let prefix: Option<Cow<str>> = container_attributes
.seek_val("prefix")
.map(|lit| Cow::Owned(lit.value()));
let rename_all = container_attributes
.seek_val("rename_all")
.map(|lit| {
let val = lit.value();
Some(quote! {#[serde(rename_all= #val)]})
})
.unwrap_or(None);
let ctx: Context = parse_macro_input!(item);
let (ident_original, ident_borrowed, _ident_owned) = ctx.path.split_self_for_impl();
let owned = ctx.clone().into_owned();
let impl_weight = ctx.impl_weight();
let impl_default = ctx.impl_default();
let impl_from_owned = ctx.impl_from_owned();
let impl_from_ref = ctx.impl_from_ref();
let binding_copy = ctx.binding_copy(prefix.clone());
let binding_init = ctx.binding_init(prefix.clone());
let binding_parse = ctx.binding_parse(prefix.clone());
let binding_print = ctx.binding_print(prefix.clone());
let binding_print_owned = ctx.binding_print_owned(prefix.clone());
let quoted = quote! {
#[no_mangle]
#impl_weight
#[no_mangle]
pub type #ident_borrowed = #ident_original;
#[repr(C)]
#[derive(serde::Deserialize)]
#[derive(serde::Serialize)]
#[serde(crate="self::serde")]
#rename_all
#ctx
#[repr(C)]
#owned
#impl_default
#impl_from_owned
#impl_from_ref
#binding_copy
#binding_init
#binding_parse
#binding_print
#binding_print_owned
};
proc_macro::TokenStream::from(quoted)
}