engineer_derive 0.1.6

The engineer is a master builder! And this's it's derive.
Documentation
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
extern crate proc_macro;
extern crate proc_macro2;

use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, ToTokens};
use proc_macro::TokenStream;

use quote::{format_ident, quote};
use syn::{parse_macro_input, DeriveInput, GenericArgument, Path, PathArguments, Type};

#[derive(Debug, Clone, FromMeta)]
struct Retype {
    to: String,
    #[darling(rename = "re")]
    restore: String,
}

impl Retype {
    fn new(to: &str, restore: &str) -> Self {
        Self {
            to: to.to_string(),
            restore: restore.to_string(),
        }
    }
}

#[derive(Debug, Clone, FromMeta)]
struct GlobRetype {
    from: String,
    to: String,
    #[darling(rename = "re")]
    restore: String,
}

#[derive(FromField, Clone, Debug)]
#[darling(attributes(engineer), forward_attrs(allow, doc, cfg))]
struct EngineerField {
    ident: Option<syn::Ident>,
    ty: syn::Type,

    default_value: Option<String>,
    retype: Option<Retype>,

    default: Flag,
    /// Shorthand for `retype(to = "impl Into<String>", re = ".into()")`,
    str_retype: Flag,
}

impl EngineerField {
    fn apply_shorthands(&mut self) {
        if self.str_retype.is_present() {
            self.retype = Some(Retype::new("impl Into<String>", ".into()"))
        }

        if self.default.is_present() {
            // if type_is_option(&self.ty) {
            //     self.default_value = Some("Some(Default::default())".to_string())
            // } else {
            self.default_value = Some("Default::default()".to_string())
            // }
        }

        if self.default_value.is_some() {
            self.default = Flag::present()
        }
    }

    fn is_option(&self) -> bool {
        type_is_option(&self.ty) || self.default.is_present()
    }

    fn is_retyped(&self) -> bool {
        self.retype.is_some()
    }

    fn retyped(&self) -> proc_macro2::TokenStream {
        match &self.retype {
            Some(retype) => retype.to.parse().unwrap(),
            None => {
                let ty = if type_is_option(&self.ty) {
                    extract_type_from_option(&self.ty).unwrap()
                } else {
                    &self.ty
                };

                quote!(#ty)
            }
        }
    }

    fn restorer(&self) -> proc_macro2::TokenStream {
        match &self.retype {
            Some(retype) => retype.restore.parse().unwrap(),
            None => Default::default(),
        }
    }

    fn as_struct_field(&self) -> proc_macro2::TokenStream {
        let name = &self.ident;
        let ty = &self.ty;

        quote!(#name: #ty,)
    }

    fn as_struct_setter(&self) -> proc_macro2::TokenStream {
        let name = &self.ident;
        let mut name_ts = quote!(#name);

        if self.is_retyped() {
            let restore = self.restorer();
            quote!( : #name #restore).to_tokens(&mut name_ts)
        }

        name_ts
    }

    fn as_func_argument(&self) -> proc_macro2::TokenStream {
        let name = &self.ident;
        let ty = self.retyped();

        quote!(#name: #ty,)
    }
}

#[derive(FromDeriveInput, Clone, Debug)]
#[darling(
    attributes(engineer),
    supports(struct_named),
    forward_attrs(allow, doc, cfg)
)]
struct EngineerOptions {
    ident: syn::Ident,
    vis: syn::Visibility,
    data: darling::ast::Data<darling::util::Ignored, EngineerField>,

    #[darling(rename = "engineer_name")]
    engineer_name_arg: Option<String>,
    #[darling(rename = "builder_func")]
    builder_func_arg: Option<String>,

    #[darling(multiple, rename = "retype")]
    retypes: Vec<GlobRetype>,
    str_retype: Flag,
    new: Flag,

    #[darling(skip)]
    fields_ref: Option<Vec<EngineerField>>,
    #[darling(skip)]
    engineer_name: Option<syn::Ident>,
    #[darling(skip)]
    builder_func: Option<syn::Ident>,
}

impl EngineerOptions {
    fn from_derive_input_delegate(input: &DeriveInput) -> Result<EngineerOptions, darling::Error> {
        let mut s = Self::from_derive_input(input)?
            .apply_self_shorthands()
            .apply_global_retypes()
            .apply_fields_shorthands()
            .set_custom_fields();

        s.set_fields_ref();

        Ok(s)
    }

    fn apply_self_shorthands(mut self) -> Self {
        if self.str_retype.is_present() {
            self.retypes.push(GlobRetype {
                from: "String".to_string(),
                to: "impl Into<String>".to_string(),
                restore: ".into()".to_string(),
            })
        }

        if self.new.is_present() {
            self.builder_func_arg = Some("new".to_string());
        }

        self
    }

    fn apply_global_retypes(mut self) -> Self {
        self.data = self.data.map_struct_fields(|mut f| {
            let ty_str = if type_is_option(&f.ty) {
                extract_type_from_option(&f.ty).unwrap()
            } else {
                &f.ty
            }
            .to_token_stream()
            .to_string();

            for r in &self.retypes {
                if ty_str == r.from {
                    f.retype = Some(Retype {
                        to: r.to.clone(),
                        restore: r.restore.clone(),
                    });
                }
            }

            f
        });

        self
    }

    fn apply_fields_shorthands(mut self) -> Self {
        self.data = self.data.map_struct_fields(|mut f| {
            f.apply_shorthands();
            f
        });

        self
    }

    fn set_custom_fields(mut self) -> Self {
        self.engineer_name = format_ident!(
            "{}",
            self.engineer_name_arg
                .clone()
                .unwrap_or(format!("{}Engineer", self.ident))
        )
        .into();

        self.builder_func = format_ident!(
            "{}",
            self.builder_func_arg
                .clone()
                .unwrap_or_else(|| "engineer".to_string())
        )
        .into();

        self
    }

    fn set_fields_ref(&mut self) {
        self.fields_ref = Some(self.data.clone().take_struct().unwrap().fields);
    }

    /// The name of Engineer struct.
    fn engineer_name(&self) -> &Option<proc_macro2::Ident> {
        &self.engineer_name
    }

    /// The name of function that builds Engineer struct from main struct.
    fn builder_name(&self) -> &Option<proc_macro2::Ident> {
        &self.builder_func
    }

    fn fields_ref(&self) -> &Vec<EngineerField> {
        self.fields_ref.as_ref().unwrap()
    }
}

trait FieldsHelpers<'e>: Iterator<Item = &'e EngineerField> + Sized {
    fn filter_normals(self) -> Vec<&'e EngineerField> {
        self.filter(|f| !f.is_option()).collect()
    }

    fn filter_options(self) -> Vec<&'e EngineerField> {
        self.filter(|f| f.is_option()).collect()
    }

    fn map_names(self) -> Vec<&'e Option<syn::Ident>> {
        self.map(|f| &f.ident).collect()
    }

    fn map_types(self) -> Vec<&'e Type> {
        self.map(|f| &f.ty).collect()
    }
}

impl<'e, T> FieldsHelpers<'e> for T where T: Iterator<Item = &'e EngineerField> {}

struct EngineerStructDefinition<'e>(&'e EngineerOptions);

impl<'e> EngineerStructDefinition<'e> {
    fn name(&self) -> &Option<proc_macro2::Ident> {
        self.0.engineer_name()
    }

    fn struct_definition(&self) -> proc_macro2::TokenStream {
        let struct_name = &self.0.ident;
        let vis = &self.0.vis;
        let engineer_name = &self.0.engineer_name();

        let fields = self.0.fields_ref();
        let struct_fields = fields.iter().map(|f| f.as_struct_field());
        let names = fields.iter().map(|f| &f.ident);

        quote! {
            #vis struct #engineer_name {
                #(
                    #struct_fields
                )*
            }

            impl Builder<#struct_name> for #engineer_name {
                fn done(self) -> #struct_name {
                    #struct_name {
                        #(
                            #names: self.#names,
                        )*
                    }
                }
            }

            impl From<#engineer_name> for #struct_name
            {
                fn from(value: #engineer_name) -> Self {
                    value.done()
                }
            }
        }
    }

    fn new_func(&self) -> proc_macro2::TokenStream {
        let engineer_name = self.name();
        let vis = &self.0.vis;

        let fields = self.0.fields_ref();
        let nrm_fields = fields.iter().filter_normals();

        let opt_names = fields.iter().filter(|f| f.is_option()).map(|f| &f.ident);
        let opt_values = fields
            .iter()
            .filter(|f| f.is_option())
            .map(|f| match &f.default_value {
                Some(sec) => {
                    let t = sec.parse::<proc_macro2::TokenStream>().unwrap();

                    if type_is_option(&f.ty) {
                        quote!(Some(#t))
                    } else {
                        quote!(#t)
                    }
                }
                _ => {
                    if type_is_option(&f.ty) {
                        quote!(None)
                    } else {
                        quote!(Default::default())
                    }
                }
            });

        let func_args = nrm_fields.iter().map(|f| f.as_func_argument());
        let struct_setters = nrm_fields.iter().map(|f| f.as_struct_setter());

        quote! {
            #vis fn new(#(#func_args)*) -> Self {
                #engineer_name {
                    #(
                        #struct_setters,
                    )*

                    #(
                        #opt_names: #opt_values,
                    )*
                }
            }
        }
    }

    fn opt_setters(&self) -> proc_macro2::TokenStream {
        let vis = &self.0.vis;

        let fields = self.0.fields_ref();
        let opt_fields = fields.iter().filter(|f| f.is_option());
        let opt_names = opt_fields.clone().map(|f| &f.ident);

        let opt_types = opt_fields.clone().map(|f| f.retyped());
        let opt_restores = opt_fields.map(|f| f.restorer());

        quote! {
            #(
                #vis fn #opt_names(mut self, #opt_names: #opt_types) -> Self {
                    self.#opt_names = (#opt_names #opt_restores).into();
                    self
                }
            )*
        }
    }

    fn struct_impl(&self) -> proc_macro2::TokenStream {
        let engineer_name = &self.name();
        let new_func = self.new_func();
        let opt_setters = self.opt_setters();

        quote! {
            impl #engineer_name {
                #new_func
                #opt_setters
            }
        }
    }
}

impl<'e> quote::ToTokens for EngineerStructDefinition<'e> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let struct_definition = self.struct_definition();
        let struct_impl = self.struct_impl();

        quote! {
            #struct_definition
            #struct_impl
        }
        .to_tokens(tokens)
    }
}

struct StructImpl<'e>(&'e EngineerOptions);

impl<'e> StructImpl<'e> {
    fn builder_func(&self) -> proc_macro2::TokenStream {
        let engineer_name = self.0.engineer_name();
        let builder_name = self.0.builder_name();
        let vis = &self.0.vis;

        let fields = self.0.fields_ref();
        let nrm_fields = fields.iter().filter(|f| !f.is_option());
        let nrm_names = nrm_fields.clone().map(|f| &f.ident);

        let func_args = nrm_fields.clone().map(|f| f.as_func_argument());

        quote! {
            #vis fn #builder_name(#(#func_args)*) -> #engineer_name {
                <#engineer_name>::new(#(#nrm_names,)*)
            }
        }
    }
}

impl<'e> quote::ToTokens for StructImpl<'e> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let name = &self.0.ident;
        let builder_func = self.builder_func();

        quote! { impl #name { #builder_func } }.to_tokens(tokens)
    }
}

struct TraitImpl<'e>(&'e EngineerOptions);

impl<'e> quote::ToTokens for TraitImpl<'e> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let name = &self.0.ident;
        let engineer_name = &self.0.engineer_name();

        let fields = self.0.fields_ref();
        let nrm_fields = fields.iter().filter(|f| !f.is_option());

        let nrm_count = nrm_fields.clone().count();
        let opt_count = fields.len() - nrm_count;
        let nrm_fields_types = nrm_fields.map(|f| &f.ty);

        let members = (0..nrm_count).map(|f| {
            format!("required.{}", f)
                .parse::<proc_macro2::TokenStream>()
                .unwrap()
        });

        quote! {
            impl Engineer for #name {
                const NORMAL_FIELDS: usize = #nrm_count;
                const OPTIONAL_FIELDS: usize = #opt_count;

                type Builder = #engineer_name;
                type Params = (#(#nrm_fields_types,)*);

                fn builder(required: Self::Params) -> Self::Builder {
                    #engineer_name::new(#(#members,)*)
                }
            }
        }
        .to_tokens(tokens);
    }
}

#[proc_macro_derive(Engineer, attributes(engineer))]
pub fn engineer(input: TokenStream) -> TokenStream {
    // Parse the input tokens into a syntax tree
    let input = parse_macro_input!(input as DeriveInput);
    let engineer_opts = EngineerOptions::from_derive_input_delegate(&input).unwrap();

    let engineer_struct_definition = EngineerStructDefinition(&engineer_opts);
    let struct_impl = StructImpl(&engineer_opts);
    let trait_impl = TraitImpl(&engineer_opts);

    // Build the output, possibly using quasi-quotation
    let expanded = quote! {
        #engineer_struct_definition
        #struct_impl
        #trait_impl
    };

    // Hand the output tokens back to the compiler
    TokenStream::from(expanded)
}

fn type_is_option(ty: &Type) -> bool {
    match ty {
        Type::Path(path_type) => {
            path_type.path.leading_colon.is_none()
                && path_type.path.segments.len() == 1
                && path_type.path.segments.iter().next().unwrap().ident == "Option"
        }
        _ => false,
    }
}

fn path_is_option(path: &Path) -> bool {
    path.leading_colon.is_none()
        && path.segments.len() == 1
        && path.segments.iter().next().unwrap().ident == "Option"
}

fn extract_type_from_option(ty: &Type) -> Option<&Type> {
    match ty {
        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) => Some(params.args.iter().next().unwrap()),
                _ => None,
            }?;
            // This argument must be a type:
            match generic_arg {
                GenericArgument::Type(ty) => Some(ty),
                _ => None,
            }
        }
        _ => None,
    }
}