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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
use darling::FromAttributes;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse, ItemStruct};
#[derive(Debug, Default, FromAttributes)]
#[darling(attributes(ppproperly))]
#[darling(default)]
struct Args {
len_for: Option<String>,
discriminant_for: Option<String>,
}
#[proc_macro_derive(Serialize)]
pub fn derive_serialize(item: TokenStream) -> TokenStream {
let ast: ItemStruct = parse(item).unwrap();
let name = ast.ident;
let serializers = ast.fields.iter().map(|field| {
let field_name = field.ident.as_ref().expect("should be a names struct");
quote!(
self.#field_name.serialize(w)?;
)
});
quote!(
impl Serialize for #name {
fn serialize<W: std::io::Write>(&self, w: &mut W) -> Result<()> {
#(#serializers) *
Ok(())
}
}
)
.into()
}
#[proc_macro_derive(Deserialize, attributes(ppproperly))]
pub fn derive_deserialize(item: TokenStream) -> TokenStream {
let ast: ItemStruct = parse(item).unwrap();
let name = ast.ident;
let mut len_for = std::collections::HashMap::new();
let mut discriminant_for = std::collections::HashMap::new();
let has_len_annotations = ast.fields.iter().any(|field| {
let args = Args::from_attributes(&field.attrs).unwrap();
args.len_for.is_some()
});
let has_discriminant_annotations = ast.fields.iter().any(|field| {
let args = Args::from_attributes(&field.attrs).unwrap();
args.discriminant_for.is_some()
});
let mut map_declarations = TokenStream2::new();
if has_len_annotations {
map_declarations.extend(
vec![quote!(
let mut len_for = std::collections::HashMap::new();
)]
.into_iter(),
);
}
if has_discriminant_annotations {
map_declarations.extend(
vec![quote!(
let mut discriminant_for = std::collections::HashMap::new();
)]
.into_iter(),
);
}
let deserializers = ast.fields.iter().map(|field| {
let mut out = TokenStream2::new();
let field_name = field.ident.as_ref().expect("should be a names struct");
let args = Args::from_attributes(&field.attrs).unwrap();
if let Some(attr) = args.len_for {
out.extend(
vec![quote!(
let mut len = 0u16;
len.deserialize(r)?;
len_for.insert(#attr, len);
)]
.into_iter(),
);
len_for.insert(attr, ());
}
if let Some(attr) = args.discriminant_for {
out.extend(
vec![quote!(
let mut discriminant = 0u8;
discriminant.deserialize(r)?;
discriminant_for.insert(#attr, discriminant);
)]
.into_iter(),
);
discriminant_for.insert(attr, ());
}
if len_for.contains_key(&field_name.to_string()) {
out.extend(
vec![quote!(
let r = r.take(len_for.get("#field_name").unwrap());
)]
.into_iter(),
);
}
if discriminant_for.contains_key(&field_name.to_string()) {
out.extend(
vec![quote!(
let attr = discriminant_for.get("#field_name").unwrap();
self.#field_name.deserialize_with_discriminant(r, attr)?;
)]
.into_iter(),
);
} else {
out.extend(
vec![quote!(
self.#field_name.deserialize(r)?;
)]
.into_iter(),
);
}
out
});
quote!(
impl Deserialize for #name {
fn deserialize<R: std::io::Read>(&mut self, r: &mut R) -> Result<()> {
#map_declarations
#(#deserializers) *
Ok(())
}
}
)
.into()
}
|