/
optfn.rs
451 lines (404 loc) · 13.9 KB
/
optfn.rs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::str::FromStr;
use syn::{
parse::{discouraged::Speculative, ParseBuffer},
GenericArgument, PatType, PathArguments,
};
use {
proc_macro::TokenStream,
proc_macro2::{Span, TokenStream as TokenStream2},
quote::{quote, ToTokens, TokenStreamExt},
syn::{
ext::IdentExt,
parse::{Parse, ParseStream},
token, Error, Expr, ExprClosure, Ident, LitBool, LitStr, Path, Result, Token,
},
};
use syn::{
parse_macro_input, Attribute, Block, FnArg, Generics, Item, ItemFn, ReturnType, Type,
Visibility,
};
type BuilderField = (Ident, Box<Type>);
pub struct OptFn {
original: ItemFn,
required_args: Vec<BuilderField>,
optional_args: Vec<BuilderField>,
generics: GenericGenerator,
vis: Visibility,
builder_name: Ident,
name: Ident,
return_type: ReturnType,
}
impl Parse for OptFn {
/*
We care about:
- all the fields
- that all "optional" fields come after required/positional fields
- the original
- any generics (need to be mapped to the builder)
- fields that are required
- fields that are optional
*/
fn parse(input: ParseStream) -> Result<Self> {
let orig: ItemFn = input.parse()?;
// start by parsing positionals
// optionals must come after positionals
let mut parsing_optionals = false;
let mut required_args = Vec::new();
let mut optional_args = Vec::new();
for arg in orig.sig.inputs.clone() {
let g = Ok(arg);
g.and_then(|f| match f {
FnArg::Typed(arg) => Ok(arg),
FnArg::Receiver(r) => Err(Error::new_spanned(r, "optfn cannot be used on methods")),
})
.and_then(|f| match (&f).pat.as_ref() {
syn::Pat::Ident(iden) => Ok((iden.clone(), f)),
other => Err(Error::new_spanned(other, "optfn cannot struct fields")),
})
.map(|(name, pat)| {
let is_optional = match pat.ty.as_ref() {
Type::Path(p) => {
if let Some(arg) = p.path.segments.first() {
arg.ident.to_string() == "Option"
} else {
false
}
}
_ => false,
};
(name, pat, is_optional)
})
.and_then(|(name, pat, is_optional)| {
match (is_optional, parsing_optionals) {
(false, false) => {
required_args.push((name.ident, pat.ty));
Ok(())
}
(false, true) => Err(Error::new_spanned(
name,
"Non-optional values must be placed before optionals",
)),
(true, _) => {
optional_args.push((name.ident, extract_type_from_option(pat.ty)?));
parsing_optionals = true;
Ok(())
}
}
})?;
}
let generics =
GenericGenerator::from_generics(orig.sig.generics.clone(), required_args.len());
let name = orig.sig.ident.clone();
let builder_name = syn::Ident::new(
format!("{}_Builder", name.to_string()).as_str(),
proc_macro2::Span::call_site(),
);
Ok(Self {
vis: orig.vis.clone(),
return_type: orig.sig.output.clone(),
generics,
original: orig,
required_args,
optional_args,
builder_name,
name,
})
}
}
impl ToTokens for OptFn {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let OptFn {
original,
required_args,
optional_args,
vis,
return_type,
name,
generics,
builder_name,
..
} = self;
let mut fields = quote! {};
for (name, ty) in optional_args.iter().chain(required_args) {
fields.append_all(quote! {
#name: Option<#ty>,
})
}
let impl_generics = generics.gen_all_generic(usize::MAX);
let mut builder_struct = quote! {
#[derive(Default)]
#vis struct #builder_name #impl_generics {
#fields
__omarker: ::core::marker::PhantomData<&'_o ()>
}
};
let ty_gen = generics.gen_all(false);
let mut builder_builder = quote! {
impl<'_o> #builder_name #ty_gen {
#vis fn builder() -> #builder_name #ty_gen {
#builder_name::default()
}
}
};
let mut builders = quote! {};
for (id, (name, ty)) in required_args.iter().enumerate() {
let impl_generics = generics.gen_all_generic(id);
let ty_gen_in = generics.gen_positional(id, false);
let ty_gen_out = generics.gen_positional(id, true);
builders.append_all(quote! {
impl #impl_generics #builder_name #ty_gen_in {
#vis fn #name(mut self, v: #ty) -> #builder_name #ty_gen_out {
self.#name = Some(v);
// need to bend const generics, and this is the easiest the works with the macro
// todo: destructure and restrcuture, or find another way
// go from Example<false> to Example<true>
unsafe {::core::mem::transmute(self)}
}
}
})
}
let impl_generics = generics.gen_all_generic(usize::MAX);
let ty_gen = generics.gen_positional(usize::MAX, false);
for (name, ty) in optional_args {
builders.append_all(quote! {
impl #impl_generics #builder_name #ty_gen {
#vis fn #name(mut self, v: #ty) -> #builder_name #ty_gen {
self.#name = Some(v);
self
}
}
})
}
// Generate the fields to unpack the builder to the original function
let mut callerfields = quote! {};
for (name, _ty) in required_args {
// it's okay to unwrap because this method will only exist when all generics are true
callerfields.append_all(quote! {
self.#name.unwrap(),
})
}
for (name, _ty) in optional_args {
callerfields.append_all(quote! {
self.#name,
})
}
let ty_gen = generics.gen_all(true);
let caller = quote! {
impl <'_o> #builder_name #ty_gen {
#vis fn build(self) #return_type {
#name(#callerfields)
}
}
};
// todo - force the number of required arguments
// the optional fragment is forwarded to a helper macro_rules
// this spits out the appropriate key/value pair depending on the fragment input
let macro_impl = quote! {
#[macro_export]
macro_rules! #name {
($($key:ident$(: $value:expr)?), * $(,)?) => {
#builder_name::builder()
$(.$key(::optargs::builder_field!($key $(, $value)?)))*
.build()
};
}
};
let toks = quote! {
#original
#builder_struct
#builder_builder
#builders
#caller
#macro_impl
};
toks.to_tokens(tokens);
}
}
// https://stackoverflow.com/questions/55271857/how-can-i-get-the-t-from-an-optiont-when-using-syn
fn extract_type_from_option(ty: Box<Type>) -> Result<Box<Type>> {
// todo: allow other option types (probably generated by macro)
fn path_is_option(path: &Path) -> bool {
path.leading_colon.is_none()
&& path.segments.len() == 1
&& path.segments.iter().next().unwrap().ident == "Option"
}
match ty.as_ref() {
Type::Path(typepath) if typepath.qself.is_none() && path_is_option(&typepath.path) => {
// Get the first segment of the path (there is only one, in fact: "Option"):
let type_params = &typepath.path.segments.iter().next().unwrap().arguments;
// It should have only on angle-bracketed param ("<String>"):
let generic_arg = match type_params {
PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
_ => panic!("TODO: error handling"),
};
// This argument must be a type:
match generic_arg {
GenericArgument::Type(ty) => Ok(Box::new(ty.clone())),
_ => panic!("TODO: error handling"),
}
}
_ => panic!("TODO: error handling"),
}
}
struct TypeWrapper<'a>(&'a Type);
/*
This struct lets us generate the correct const generics form depending on the arguments.
---
So we can turn this function:
fn blah(a: u32, b: Option<u32>){}
into
impl Builder<false> {
^^^^^^^ -- this gets generated from a method
fn a(self) -> Builder<true> {
^^^^^^ -- this gets generated from a method
...
}
}
--
It's important to keep the original generics, and add any lifetimes for fields that start with &'_os.
To do this, we always generate a borrowed lifetime and let the builder automatically add in the '_os lifetime
*/
struct GenericGenerator {
inner: Generics,
num_args: usize,
}
impl GenericGenerator {
fn from_generics(inner: Generics, num_args: usize) -> Self {
Self { inner, num_args }
}
// generate the generics for an all-generic const
// used in the struct position
/*
pub struct ExampleBuilder<'_o, const M0: bool> {
^^^^^^^^^^^^^^^^^^^^^^^^^^ this bit gets generated
a: Option<u32>,
b: Option<&'_o str>,
}
*/
fn gen_all_generic(&self, exclude: usize) -> TokenStream2 {
let mut inner = quote! {};
for id in 0..self.num_args {
let idref = TokenStream2::from_str(format!("M{}", id).as_str()).unwrap();
if id != exclude {
inner.append_all(quote! { const #idref: bool, });
}
}
let out = quote! {
<'_o, #inner>
};
// panic!("{:#?}", out);
out
}
// generate all the const generics with the same marker
/*
impl ExampleBuilder<'_o, true, M1> {
^^^^^^^^^^^^^^^^^^^^ generate this part
fn call(self) #ret {
#inner(#callerargs)
}
}
*/
fn gen_all(&self, marker: bool) -> TokenStream2 {
let mut inner = quote! {};
for _ in 0..self.num_args {
inner.append_all(quote! { #marker, });
}
quote! {
<'_o, #inner>
}
}
// generate all as generic, except for a single position with the marker
/*
impl<const A: bool> ExampleBuilder<'_o, false, A> {
^^^^^^^^^^^^^^^ gen this
fn call(self, val: #ty) -> ExampleBuilder<'_o, true, A> {
...
}
}
*/
fn gen_positional(&self, position: usize, marker: bool) -> TokenStream2 {
//
let mut inner = quote! {};
for id in 0..self.num_args {
if id == position {
inner.append_all(quote! { #marker, });
} else {
let mtok = TokenStream2::from_str(format!("M{}", id).as_str()).unwrap();
inner.append_all(quote! { #mtok, });
}
}
quote! {
<'_o, #inner>
}
}
// generate all as generic, except for a single position with the marker
/*
impl<'_o, const A: bool> ExampleBuilder<'_o, true, true> {
^^^^^^^^^^^^^^^^^^^^^^^^^ gen this
fn call(self, val: #ty) -> ExampleBuilder<'_o, true, true> {
self.
}
}
*/
fn gen_positional_impl(&self, position: usize, marker: bool) -> TokenStream2 {
//
todo!()
}
fn gen_phantom(&self) -> TokenStream2 {
todo!()
}
// generate all as generic, except for a single position with the marker
/*
impl<'_o, const A: bool> ExampleBuilder<'_o, true, true> {
fn call(self, val: #ty) #ret {
self.
}
}
*/
fn gen_positional_ret(&self, position: usize, marker: bool) -> TokenStream2 {
//
todo!()
}
}
/*
let toks = quote! {
#[derive(Default)]
pub struct ExampleBuilder<'a, const A: bool> {
a: Option<u32>,
b: Option<&'a str>,
}
impl<'a> ExampleBuilder<'a, false> {
pub fn a(self, a: u32) -> ExampleBuilder<'a, true> {
ExampleBuilder {
a: Some(a),
b: self.b,
}
}
}
impl<const A: bool> ExampleBuilder<'_, A> {
pub fn b(self, b: &str) -> ExampleBuilder<'_, A> {
ExampleBuilder {
a: self.a,
b: Some(b),
}
}
}
impl<'a> ExampleBuilder<'a, true> {
pub fn call(self) -> bool {
example(self.a.unwrap(), self.b)
}
}
};
let macro_impl = quote! {
#[macro_export]
macro_rules! example {
($($y:ident: $z:expr), *) => {
{
ExampleBuilder::default()
$(.$y($z))*
.call()
}
};
}
};
*/