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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
extern crate proc_macro;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use syn::export::TokenStream2;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{
    parenthesized, parse2, Data, DeriveInput, Field, Fields, Ident, LitStr, Result, Token, Variant,
};
use syn::{punctuated, Attribute};

struct FieldOrdered(Field, usize);

#[derive(PartialEq, Eq)]
enum FieldType {
    Named,
    Unnamed,
    Unit,
}

/// Convert fields of both named and unnamed to a unified Vec.
fn convert_fields(fields: &Fields) -> (FieldType, Vec<FieldOrdered>) {
    let field_type;
    let fields = match fields {
        Fields::Named(named) => {
            field_type = FieldType::Named;
            named
                .named
                .iter()
                .enumerate()
                .map(|(i, f)| FieldOrdered(f.clone(), i))
                .collect()
        }
        Fields::Unnamed(unnamed) => {
            field_type = FieldType::Unnamed;
            unnamed
                .unnamed
                .iter()
                .enumerate()
                .map(|(i, f)| FieldOrdered(f.clone(), i))
                .collect()
        }
        Fields::Unit => {
            field_type = FieldType::Unit;
            Vec::new()
        }
    };
    (field_type, fields)
}

/// Get a token stream describing the type name of the destructed enum type
fn get_destruct_enum_type(
    name: &Ident,
    variants: &mut punctuated::Iter<Variant>,
) -> proc_macro2::TokenStream {
    match variants.next() {
        Some(variant) => {
            let vname = format_ident!("_destruct_enum_{}_variant_{}", name, variant.ident);
            let metadata_name =
                format_ident!("_destruct_enum_{}_variant_{}_meta", name, variant.ident);
            let field_struct_metadata_name = format_ident!(
                "_destruct__destruct_enum_{}_variant_{}_meta",
                name,
                variant.ident
            );
            let tail = get_destruct_enum_type(name, variants);
            let (_, fields) = convert_fields(&variant.fields);
            let destruct_type = get_destruct_type(&vname, &mut fields.iter());
            quote! {
                destruct::DestructEnumVariant<destruct::DestructBegin<#destruct_type, #field_struct_metadata_name>, #tail, #metadata_name>
            }
        }
        None => {
            let metadata_name = format_ident!("_destruct_enum_{}_meta", name);
            quote! {
                destruct::DestructEnumEnd<#metadata_name>
            }
        }
    }
}

/// Get the pattern matching of From for enums.
/// It will be placed in:
/// ```rust, norun
/// impl From<OritinalEnum> for DestructType {
///     fn from(t: OritinalEnum) -> Self {
///         destruct::DestructEnumBegin::new(match t {
///             #result
///         })
///     }
/// }
/// ```
fn get_destruct_enum_from(
    name: &Ident,
    variants: &mut punctuated::Iter<Variant>,
) -> proc_macro2::TokenStream {
    match variants.next() {
        Some(variant) => {
            let ident = variant.ident.clone();
            let vname = format_ident!("_destruct_enum_{}_variant_{}", name, variant.ident);
            let (field_type, fields) = convert_fields(&variant.fields);
            let idents: Vec<Ident> = fields
                .iter()
                .map(|f| f.0.ident.clone().unwrap_or(format_ident!("field{}", f.1)))
                .collect();
            let variant_case = match field_type {
                FieldType::Named => {
                    quote! {
                        #name::#ident { #(#idents,)* } => destruct::DestructEnumVariant::new_head((#vname { #(#idents,)* }).destruct())
                    }
                }
                FieldType::Unnamed => {
                    quote! {
                        #name::#ident ( #(#idents,)* ) => destruct::DestructEnumVariant::new_head(#vname ( #(#idents,)* ).destruct())
                    }
                }
                FieldType::Unit => {
                    quote! {
                        #name::#ident => destruct::DestructEnumVariant::new_head(#vname.destruct())
                    }
                }
            };
            let tail = get_destruct_enum_from(name, variants);
            quote! {
                #variant_case,
                other_case => destruct::DestructEnumVariant::new_tail(match other_case { #tail })
            }
        }
        None => {
            quote! {
                _ => destruct::DestructEnumEnd::new()
            }
        }
    }
}

/// Get the pattern matching of Into for enums. It will be placed in:
///
/// ```rust, norun
/// impl Into<OritinalEnum> for DestructType {
///     fn into(self) -> OriginalEnum {
///         match self.variants {
///             #result
///         }
///     }
/// }
/// ```
fn get_destruct_enum_into(
    name: &Ident,
    variants: &mut punctuated::Iter<Variant>,
) -> proc_macro2::TokenStream {
    match variants.next() {
        Some(variant) => {
            let ident = variant.ident.clone();
            let (field_type, fields) = convert_fields(&variant.fields);
            let value_name = format_ident!("variant");
            let is_unit = field_type == FieldType::Unit;
            let tail = get_destruct_enum_into(name, variants);
            if is_unit {
                quote! {
                    destruct::DestructEnumVariant::Head(variant, _) => #name::#ident,
                    destruct::DestructEnumVariant::Tail(tail, _) => match tail { #tail }
                }
            } else {
                let case = get_destruct_into_fields(
                    &value_name,
                    field_type == FieldType::Named,
                    &mut fields.iter(),
                );
                quote! {
                    destruct::DestructEnumVariant::Head(variant, _) => #name::#ident #case,
                    destruct::DestructEnumVariant::Tail(tail, _) => match tail { #tail }
                }
            }
        }
        None => {
            quote! {
                _ => panic!("impossible")
            }
        }
    }
}

/// Get the type name of destructed type.
fn get_destruct_type(
    name: &Ident,
    fields: &mut std::slice::Iter<FieldOrdered>,
) -> proc_macro2::TokenStream {
    match fields.next() {
        Some(head_field) => {
            let head_name = head_field
                .0
                .ident
                .clone()
                .unwrap_or(format_ident!("unnamed_{}", head_field.1));
            let metadata_name = format_ident!("_destruct_{}_field_{}_meta", name, head_name);
            let head = head_field.0.ty.clone();
            let tail = get_destruct_type(name, fields);
            quote! {
                destruct::DestructField<#head, #tail, #metadata_name>
            }
        }
        None => {
            let metadata_name = format_ident!("_destruct_{}_meta", name);
            quote! {
                destruct::DestructEnd<#metadata_name>
            }
        }
    }
}

/// Get the field assignments of the from function.
/// It will be placed in:
/// ```rust, no_run
/// impl From<OriginalStruct> for DestructType {
///     fn from(value: OriginalStruct) -> Self {
///         #result
///     }
/// }
/// ```
fn get_destruct_from(fields: &mut std::slice::Iter<FieldOrdered>) -> proc_macro2::TokenStream {
    match fields.next() {
        Some(head_field) => {
            let tail = get_destruct_from(fields);
            match head_field.0.ident.clone() {
                Some(head) => {
                    quote! {
                        destruct::DestructField::new(t.#head, #tail)
                    }
                }
                None => {
                    let i = proc_macro2::Literal::usize_unsuffixed(head_field.1);
                    quote! {
                        destruct::DestructField::new(t.#i, #tail)
                    }
                }
            }
        }
        None => {
            quote! {
                destruct::DestructEnd::new()
            }
        }
    }
}

/// Get the field assignments of the into function.
/// It will be placed in:
/// ```rust, no_run
/// impl Into<OriginalStruct> for DestructType {
///     fn into(self) -> OriginalStruct {
///         OriginalStruct #result;
///     }
/// }
/// ```
fn get_destruct_into_fields(
    self_name: &Ident,
    struct_is_named: bool,
    fields: &mut std::slice::Iter<FieldOrdered>,
) -> proc_macro2::TokenStream {
    let mut acc = quote! { . };
    let mut tokens = TokenStream2::new();
    for field in fields {
        match field.0.ident.clone() {
            Some(name) => {
                tokens.extend(quote! {
                    #name: #self_name.fields #acc head,
                });
                acc = quote! { #acc tail . }
            }
            None => {
                tokens.extend(quote! {
                    #self_name.fields #acc head,
                });
                acc = quote! { #acc tail . }
            }
        }
    }
    if struct_is_named {
        quote! {
            { #tokens }
        }
    } else {
        quote! {
            ( #tokens )
        }
    }
}

/// Generate metadata type for struct fields. The generated type is a unit struct which implements
/// `destruct::DestructMetadata` and `destruct::DestructFieldMetadata`
fn get_destruct_field_meta(
    name: &Ident,
    struct_is_named: bool,
    fields: &mut std::slice::Iter<FieldOrdered>,
) -> proc_macro2::TokenStream {
    let mut tokens = TokenStream2::new();
    for field in fields {
        let field_name = field
            .0
            .ident
            .clone()
            .unwrap_or(format_ident!("unnamed_{}", field.1));
        let field_index = field.1;
        let field_meta_name = format_ident!("_destruct_{}_field_{}_meta", name, field_name);
        let s = format!("{}", name);
        let lit_name = LitStr::new(s.as_str(), name.span());
        let s = format!("{}", field_name);
        let field_lit_name = LitStr::new(s.as_str(), field_name.span());
        tokens.extend(quote! {
            #[allow(non_camel_case_types)]
            #[derive(Debug, PartialEq, Eq)]
            pub struct #field_meta_name;

            impl destruct::DestructMetadata for #field_meta_name {
                fn struct_name() -> &'static str {
                    #lit_name
                }
                fn named_fields() -> bool {
                    #struct_is_named
                }
            }
            impl destruct::DestructFieldMetadata for #field_meta_name {
                fn field_name() -> &'static str {
                    #field_lit_name
                }
                fn field_index() -> usize {
                    #field_index
                }
            }
        });
    }
    tokens
}

struct DestructArgs {
    brace_token: syn::token::Paren,
    fields: Punctuated<Ident, Token![,]>,
}

impl Parse for DestructArgs {
    fn parse(input: ParseStream) -> Result<Self> {
        let content;
        Ok(DestructArgs {
            brace_token: parenthesized!(content in input),
            fields: content.parse_terminated(Ident::parse)?,
        })
    }
}

#[proc_macro_derive(Destruct, attributes(destruct))]
pub fn derive_destruct(input: TokenStream) -> TokenStream {
    let input = proc_macro2::TokenStream::from(input);
    let input: DeriveInput = parse2(input).unwrap();
    let name = input.ident;
    let attrs: Vec<&Attribute> = input
        .attrs
        .iter()
        .filter(|a| a.path.get_ident().unwrap() == "destruct")
        .collect();

    if attrs.len() > 1 {
        panic!("only one destruct attribute is allowed");
    }

    let mut result = match input.data {
        Data::Struct(s) => {
            let (field_type, fields) = convert_fields(&s.fields);
            let s = format!("{}", name);
            let lit_name = LitStr::new(s.as_str(), name.span());
            derive_struct(name.clone(), lit_name, field_type, fields)
        }
        Data::Enum(e) => {
            let mut tt = TokenStream2::new();
            let s = format!("{}", name);
            let lit_name = LitStr::new(s.as_str(), name.span());
            for (variant_index, variant) in e.variants.iter().enumerate() {
                let vname = format_ident!("_destruct_enum_{}_variant_{}", name, variant.ident);
                let meta_name =
                    format_ident!("_destruct_enum_{}_variant_{}_meta", name, variant.ident);
                let vfields = variant.fields.clone();
                let (field_type, fields) = convert_fields(&variant.fields);
                let struct_is_named = field_type == FieldType::Named;
                if struct_is_named {
                    tt.extend(quote! {
                        #[allow(non_camel_case_types)]
                        #[derive(Debug, PartialEq, Eq)]
                        pub struct #vname #vfields
                    });
                } else {
                    tt.extend(quote! {
                        #[allow(non_camel_case_types)]
                        #[derive(Debug, PartialEq, Eq)]
                        pub struct #vname #vfields;
                    });
                }
                let s = format!("{}", name);
                let lit_name = LitStr::new(s.as_str(), name.span());
                let s = format!("{}::{}", name, variant.ident);
                let lit_vname = LitStr::new(s.as_str(), name.span());
                tt.extend(quote! {
                    #[allow(non_camel_case_types)]
                    pub struct #meta_name;
                    impl destruct::DestructMetadata for #meta_name {
                        fn struct_name() -> &'static str {
                            #lit_vname
                        }
                        fn named_fields() -> bool {
                            #struct_is_named
                        }
                    }
                    impl destruct::DestructEnumMetadata for #meta_name {
                        fn enum_name() -> &'static str {
                            #lit_name
                        }
                    }
                    impl destruct::DestructEnumVariantMetadata for #meta_name {
                        fn variant_name() -> &'static str {
                            #lit_vname
                        }
                        fn variant_index() -> usize {
                            #variant_index
                        }
                    }
                });
                let s = format!("{}::{}", name, vname);
                let lit_name = LitStr::new(s.as_str(), name.span());
                tt.extend(derive_struct(vname, lit_name, field_type, fields));
            }
            let destruct_enum_meta_name = format_ident!("_destruct_enum_{}_meta", name);
            let destruct_enum_type = get_destruct_enum_type(&name, &mut e.variants.iter());
            let destruct_enum_from = get_destruct_enum_from(&name, &mut e.variants.iter());
            let destruct_enum_into = get_destruct_enum_into(&name, &mut e.variants.iter());
            quote! {
                #tt

                impl From<#name> for destruct::DestructEnumBegin<#destruct_enum_type, #destruct_enum_meta_name> {
                    fn from(t: #name) -> Self {
                        destruct::DestructEnumBegin::new(match t {#destruct_enum_from})
                    }
                }

                #[allow(non_camel_case_types)]
                #[derive(Debug, PartialEq, Eq)]
                pub struct #destruct_enum_meta_name;

                impl destruct::DestructEnumMetadata for #destruct_enum_meta_name {
                    fn enum_name() -> &'static str {
                        #lit_name
                    }
                }

                impl Into<#name> for destruct::DestructEnumBegin<#destruct_enum_type, #destruct_enum_meta_name> {
                    fn into(self) -> #name {
                        match self.variants {
                            #destruct_enum_into
                        }
                    }
                }

                impl destruct::Destruct for #name {
                    type DestructType = destruct::DestructEnumBegin<#destruct_enum_type, #destruct_enum_meta_name>;

                    fn destruct(self) -> Self::DestructType {
                        self.into()
                    }

                    fn construct(d: Self::DestructType) -> Self {
                        d.into()
                    }
                }
            }
        }
        _ => panic!("derive Destruct supports only structs and enums"),
    };
    if !attrs.is_empty() {
        let attr = attrs[0];
        let args: DestructArgs = syn::parse2(attr.tokens.clone()).unwrap();

        for ident in args.fields.iter() {
            result.extend(quote! {
                #ident!(#name);
            });
        }
    }

    proc_macro::TokenStream::from(result)
}

/// Generate metadata class and implement Destruct for a struct.
///
/// Parameters:
/// - name: The identifier of the struct. It may be a generated name for enum variants.
/// - lit_name: The name of the struct. In the case of enum variants, it's "Enum::Variant".
/// - struct_is_named: Whether the struct definition is named.
/// - fields: The field of the struct or enum variant.
fn derive_struct(
    name: Ident,
    lit_name: LitStr,
    field_type: FieldType,
    fields: Vec<FieldOrdered>,
) -> TokenStream2 {
    let struct_is_named = field_type == FieldType::Named;
    let destruct_type = get_destruct_type(&name, &mut fields.iter());
    let destruct_from = get_destruct_from(&mut fields.iter());
    let self_name = format_ident!("self");
    let destruct_into = if field_type == FieldType::Unit {
        TokenStream2::new()
    } else {
        get_destruct_into_fields(&self_name, struct_is_named, &mut fields.iter())
    };
    let destruct_field_meta = get_destruct_field_meta(&name, struct_is_named, &mut fields.iter());

    let destruct_meta_name = format_ident!("_destruct_{}_meta", name);

    // Return the generated impl
    let output = quote! {
        impl From<#name> for destruct::DestructBegin<#destruct_type, #destruct_meta_name> {
            fn from(t: #name) -> Self {
                destruct::DestructBegin::new(#destruct_from)
            }
        }

        #[allow(non_camel_case_types)]
        #[derive(Debug, PartialEq, Eq)]
        pub struct #destruct_meta_name;

        impl destruct::DestructMetadata for #destruct_meta_name {
            fn struct_name() -> &'static str {
                #lit_name
            }
            fn named_fields() -> bool {
                #struct_is_named
            }
        }

        #destruct_field_meta

        impl Into<#name> for destruct::DestructBegin<#destruct_type, #destruct_meta_name> {
            fn into(self) -> #name {
                #name #destruct_into
            }
        }

        impl destruct::Destruct for #name {
            type DestructType = destruct::DestructBegin<#destruct_type, #destruct_meta_name>;

            fn destruct(self) -> Self::DestructType {
                self.into()
            }

            fn construct(d: Self::DestructType) -> Self {
                d.into()
            }
        }
    };
    output
}