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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use indexmap::IndexSet;
use quote::{format_ident, quote, ToTokens};
use syn::{Data, DeriveInput, GenericParam, Generics, Lifetime};
use crate::model::{PermissiveCompanionType, ShadowType};
impl ToTokens for ShadowType {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let Self(input) = self;
quote! {
#[derive(::bronzerde::_serde::Deserialize)]
#[serde(crate = "bronzerde::_serde")]
#input
}
.to_tokens(tokens);
}
}
impl ToTokens for PermissiveCompanionType {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let Self { ty_, impl_, .. } = self;
quote! {
#[derive(::bronzerde::_serde::Deserialize)]
#[serde(crate = "bronzerde::_serde")]
#ty_
#impl_
}
.to_tokens(tokens);
}
}
pub struct ImplDeserGenerics<'a> {
deser_generics: Generics,
input_generics: &'a Generics,
}
impl<'a> ImplDeserGenerics<'a> {
pub fn new(
input: &'a DeriveInput,
bronzerde_aware_generics: &IndexSet<syn::Ident>,
) -> ImplDeserGenerics<'a> {
let mut deser_generics = input.generics.clone();
deser_generics.make_where_clause();
if let Some(where_clause) = &mut deser_generics.where_clause {
// Each type parameter must implement `Deserialize` for the
// type to implement `Deserialize`.
//
// TODO: Take into account the `#[serde(bound)]` attribute https://serde.rs/container-attrs.html#bound
for ty_param in input.generics.type_params() {
let ident = &ty_param.ident;
let predicate = if bronzerde_aware_generics.contains(ident) {
syn::parse_quote! { #ident: ::bronzerde::EDeserialize<'de> }
} else {
syn::parse_quote! { #ident: ::bronzerde::_serde::Deserialize<'de> }
};
where_clause.predicates.push(predicate);
}
// Each lifetime parameter must be outlived by `'de`, the lifetime of the `Deserialize` trait.
for lifetime_param in input.generics.lifetimes() {
let lifetime = &lifetime_param.lifetime;
where_clause
.predicates
.push(syn::parse_quote! { 'de: #lifetime });
}
} else {
unreachable!()
}
// The `'de` lifetime of the `Deserialize` trait.
// There is no way to add a lifetime to the `impl_generics` returned by `split_for_impl`, so we
// have to create a new set of generics with the lifetime added and then split again.
let param = GenericParam::Lifetime(syn::LifetimeParam::new(Lifetime::new(
"'de",
proc_macro2::Span::call_site(),
)));
deser_generics.params.push(param);
Self {
deser_generics,
input_generics: &input.generics,
}
}
pub fn split_for_impl(
&self,
) -> (
syn::ImplGenerics<'_>,
syn::TypeGenerics,
Option<&syn::WhereClause>,
) {
let (impl_generics, _, where_clause) = self.deser_generics.split_for_impl();
let (_, ty_generics, _) = self.input_generics.split_for_impl();
(impl_generics, ty_generics, where_clause)
}
}
/// Initialize the target type from the shadow type, assigning each field from the shadow type to the
/// corresponding field on the target type.
pub fn initialize_from_shadow(
input: &Data,
type_ident: &syn::Ident,
shadow_binding: &syn::Ident,
shadow_type_ident: &syn::Ident,
) -> proc_macro2::TokenStream {
match input {
Data::Struct(data) => {
let fields = data.fields.members().map(|field| {
quote! {
#field: #shadow_binding.#field
}
});
quote! {
#type_ident {
#(#fields),*
}
}
}
Data::Enum(e) => {
let variants = e.variants.iter().map(|variant| {
let variant_ident = &variant.ident;
match &variant.fields {
syn::Fields::Named(fields) => {
let fields: Vec<_> = fields.named.iter().map(|field| {
let field = field.ident.as_ref().unwrap();
quote! {
#field
}
}).collect();
quote! {
#shadow_type_ident::#variant_ident { #(#fields),* } => #type_ident::#variant_ident { #(#fields),* }
}
}
syn::Fields::Unnamed(fields) => {
let fields: Vec<_> = fields.unnamed.iter().enumerate().map(|(i, _)| {
let i = format_ident!("__v{i}");
quote! {
#i
}
}).collect();
quote! {
#shadow_type_ident::#variant_ident(#(#fields),*) => #type_ident::#variant_ident(#(#fields),*)
}
}
syn::Fields::Unit => {
quote! {
#shadow_type_ident::#variant_ident => #type_ident::#variant_ident
}
}
}
});
quote! {
match #shadow_binding {
#(#variants),*
}
}
}
Data::Union(_) => unimplemented!(),
}
}
/// Walk all fields on the companion types to report errors about missing values, if any.
pub fn collect_missing_errors(
input: &Data,
companion_type: &syn::Ident,
companion_binding: &syn::Ident,
n_errors: &syn::Ident,
) -> proc_macro2::TokenStream {
match input {
Data::Struct(data) => {
let accumulate = data.fields.members().map(|field| {
let field_str = match &field {
syn::Member::Named(ident) => ident.to_string(),
// TODO: Improve naming for unnamed fields
syn::Member::Unnamed(index) => format!("{}", index.index),
};
quote! {
#companion_binding.#field.push_error_if_missing(#field_str);
}
});
quote! {
#(#accumulate)*
let __n_new_errors = ::bronzerde::reporter::ErrorReporter::n_errors();
if __n_new_errors > #n_errors {
Err(())
} else {
Ok(())
}
}
}
Data::Enum(e) => {
let variants = e.variants.iter().map(|variant| {
let variant_ident = &variant.ident;
if matches!(variant.fields, syn::Fields::Unit) {
return quote! {
#companion_type::#variant_ident => Ok(())
};
}
let bindings: Vec<_> = variant
.fields
.members()
.enumerate()
.map(|(i, _)| format_ident!("__v{}", i))
.collect();
let destructure =
variant
.fields
.members()
.zip(bindings.iter())
.map(|(field, v)| {
quote! {
#field: #v
}
});
let accumulate = variant
.fields
.members()
.zip(bindings.iter())
.map(|(field, v)| {
let field_str = match &field {
syn::Member::Named(ident) => ident.to_string(),
// TODO: Improve naming for unnamed fields
syn::Member::Unnamed(index) => format!("{}", index.index),
};
quote! {
#v.push_error_if_missing(#field_str);
}
});
quote! {
#companion_type::#variant_ident { #(#destructure),* } => {
#(#accumulate)*
let __n_new_errors = ::bronzerde::reporter::ErrorReporter::n_errors();
if __n_new_errors > #n_errors {
Err(())
} else {
Ok(())
}
}
}
});
quote! {
match #companion_binding {
#(#variants),*
}
}
}
Data::Union(_) => unreachable!(),
}
}