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
extern crate proc_macro;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate darling;
use syn::{Ident, Type};
use darling::{ast, FromDeriveInput};
use proc_macro::TokenStream;
#[derive(Debug, FromVariant)]
#[darling(from_ident, attributes(dummy))]
struct DummyVariant {
ident: Ident,
fields: darling::ast::Fields<DummyField>,
}
impl From<Ident> for DummyVariant {
fn from(ident: Ident) -> Self {
DummyVariant {
ident,
fields: darling::ast::Style::Unit.into(),
}
}
}
#[derive(Debug, FromField)]
#[darling(attributes(dummy))]
struct DummyField {
ident: Option<Ident>,
ty: Type,
#[darling(default)]
faker: Option<String>,
}
#[derive(Debug, FromDeriveInput)]
#[darling(attributes(dummy), supports(struct_any, enum_any))]
struct Dummy {
ident: Ident,
data: ast::Data<DummyVariant, DummyField>,
}
#[proc_macro_derive(Dummy, attributes(dummy))]
pub fn hello_world(input: TokenStream) -> TokenStream {
let parsed = syn::parse(input).expect("syn::parse ok");
let receiver = Dummy::from_derive_input(&parsed).expect("Dummy::from_derive_input ok");
let receiver_name = &receiver.ident;
let expanded = match receiver.data {
darling::ast::Data::Struct(darling::ast::Fields {
ref fields,
ref style,
..
}) => match style {
ast::Style::Unit => {
let impl_dummy = quote! {
impl fake::Dummy<fake::Faker> for #receiver_name {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
#receiver_name
}
}
};
impl_dummy
}
ast::Style::Tuple => {
let tuple_fields: Vec<_> = fields
.iter()
.map(|f| {
let field_ty = &f.ty;
let faker = if let Some(ref expr) = f.faker {
syn::parse_str::<syn::Expr>(expr).unwrap()
} else {
syn::parse_str::<syn::Expr>(&"fake::Faker").unwrap()
};
quote! {
(#faker).fake_with_rng::<#field_ty, _>(rng)
}
})
.collect();
let impl_dummy = quote! {
impl fake::Dummy<fake::Faker> for #receiver_name {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
#receiver_name(#(#tuple_fields),*)
}
}
};
impl_dummy
}
ast::Style::Struct => {
let struct_fields: Vec<_> =
fields.iter().map(|f| f.ident.as_ref().unwrap()).collect();
let let_statements: Vec<_> = fields
.iter()
.map(|f| {
let field_name = f.ident.as_ref().unwrap();
let field_ty = &f.ty;
let faker = if let Some(ref expr) = f.faker {
syn::parse_str::<syn::Expr>(expr).unwrap()
} else {
syn::parse_str::<syn::Expr>(&"fake::Faker").unwrap()
};
quote! {
let #field_name: #field_ty = (#faker).fake_with_rng(rng);
}
})
.collect();
let impl_dummy = quote! {
impl fake::Dummy<fake::Faker> for #receiver_name {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
#(#let_statements)*
#receiver_name {
#(#struct_fields),*
}
}
}
};
impl_dummy
}
},
darling::ast::Data::Enum(variants) => {
let variant_count = variants.len();
if variant_count > 0 {
let match_statements: Vec<_> = variants
.iter()
.enumerate()
.map(|(i, f)| {
let variant_name = &f.ident;
match f.fields.style {
ast::Style::Unit => {
quote! {
#i => { #receiver_name::#variant_name }
}
}
ast::Style::Tuple => {
let tuple_fields: Vec<_> = f
.fields
.fields
.iter()
.map(|f| {
let field_ty = &f.ty;
let faker = if let Some(ref expr) = f.faker {
syn::parse_str::<syn::Expr>(expr).unwrap()
} else {
syn::parse_str::<syn::Expr>(&"fake::Faker").unwrap()
};
quote! {
(#faker).fake_with_rng::<#field_ty, _>(rng)
}
})
.collect();
quote! {
#i => {
#receiver_name::#variant_name(#(#tuple_fields),*)
}
}
}
ast::Style::Struct => {
let struct_fields: Vec<_> = f
.fields
.fields
.iter()
.map(|f| f.ident.as_ref().unwrap())
.collect();
let let_statements: Vec<_> = f.fields.fields
.iter()
.map(|f| {
let field_name = f.ident.as_ref().unwrap();
let field_ty = &f.ty;
let faker = if let Some(ref expr) = f.faker {
syn::parse_str::<syn::Expr>(expr).unwrap()
} else {
syn::parse_str::<syn::Expr>(&"fake::Faker").unwrap()
};
quote! {
let #field_name: #field_ty = (#faker).fake_with_rng(rng);
}
})
.collect();
quote! {
#i => {
#(#let_statements)*
#receiver_name::#variant_name {
#(#struct_fields),*
}
}
}
}
}
})
.collect();
let impl_dummy = quote! {
impl fake::Dummy<fake::Faker> for #receiver_name {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
match rng.gen_range(0..#variant_count) {
#(#match_statements)*
_ => {
unreachable!()
}
}
}
}
};
impl_dummy
} else {
let impl_dummy = quote! {
impl fake::Dummy<fake::Faker> for #receiver_name {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
panic!("can not create an empty enum")
}
}
};
impl_dummy
}
}
};
expanded.into()
}