enum_variant_type 0.4.0

Generates types for each enum variant and conversion trait impls.
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#![deny(missing_debug_implementations, missing_docs)]
#![no_std]
#![recursion_limit = "128"]

//! Proc macro derive to generate structs from enum variants.
//!
//! This is a poor-man's implementation of <https://github.com/rust-lang/rfcs/pull/2593>.
//!
//! ```toml
//! [dependencies]
//! enum_variant_type = "0.4.0"
//! ```
//!
//! # Examples
//!
//! ```rust,edition2018
//! use enum_variant_type::EnumVariantType;
//!
//! #[derive(Debug, EnumVariantType, PartialEq)]
//! pub enum MyEnum {
//!     /// Unit variant.
//!     #[evt(derive(Clone, Copy, Debug, PartialEq))]
//!     Unit,
//!     /// Tuple variant.
//!     #[evt(derive(Debug, PartialEq))]
//!     Tuple(u32, u64),
//!     /// Struct variant.
//!     #[evt(derive(Debug))]
//!     Struct { field_0: u32, field_1: u64 },
//!     /// Skipped variant.
//!     #[evt(skip)]
//!     Skipped,
//! }
//!
//! // Now you can do the following:
//! use core::convert::TryFrom;
//! let unit: Unit = Unit::try_from(MyEnum::Unit).unwrap();
//! let tuple: Tuple = Tuple::try_from(MyEnum::Tuple(12, 34)).unwrap();
//! let named: Struct = Struct::try_from(MyEnum::Struct {
//!     field_0: 12,
//!     field_1: 34,
//! })
//! .unwrap();
//!
//! let enum_unit = MyEnum::from(unit);
//! let enum_tuple = MyEnum::from(tuple);
//! let enum_struct = MyEnum::from(named);
//!
//! // If the enum variant doesn't match the variant type, then the original variant is returned in
//! // the `Result`'s `Err` variant.
//! assert_eq!(Err(MyEnum::Unit), Tuple::try_from(MyEnum::Unit));
//! ```
//!
//! <details>
//!
//! <summary>Generated code</summary>
//!
//! ```rust,edition2018
//! use core::convert::TryFrom;
//!
//! /// Unit variant.
//! #[derive(Clone, Copy, Debug, PartialEq)]
//! pub struct Unit;
//!
//! /// Tuple variant.
//! #[derive(Debug, PartialEq)]
//! pub struct Tuple(pub u32, pub u64);
//!
//! /// Struct variant.
//! #[derive(Debug)]
//! pub struct Struct {
//!     pub field_0: u32,
//!     pub field_1: u64,
//! }
//!
//! impl From<Unit> for MyEnum {
//!     fn from(variant_struct: Unit) -> Self {
//!         MyEnum::Unit
//!     }
//! }
//!
//! impl TryFrom<MyEnum> for Unit {
//!     type Error = MyEnum;
//!
//!     fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
//!         if let MyEnum::Unit = enum_variant {
//!             Ok(Unit)
//!         } else {
//!             Err(enum_variant)
//!         }
//!     }
//! }
//!
//! impl From<Tuple> for MyEnum {
//!     fn from(variant_struct: Tuple) -> Self {
//!         let Tuple(_0, _1) = variant_struct;
//!         MyEnum::Tuple(_0, _1)
//!     }
//! }
//!
//! impl TryFrom<MyEnum> for Tuple {
//!     type Error = MyEnum;
//!
//!     fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
//!         if let MyEnum::Tuple(_0, _1) = enum_variant {
//!             Ok(Tuple(_0, _1))
//!         } else {
//!             Err(enum_variant)
//!         }
//!     }
//! }
//!
//! impl From<Struct> for MyEnum {
//!     fn from(variant_struct: Struct) -> Self {
//!         let Struct { field_0, field_1 } = variant_struct;
//!         MyEnum::Struct { field_0, field_1 }
//!     }
//! }
//!
//! impl TryFrom<MyEnum> for Struct {
//!     type Error = MyEnum;
//!
//!     fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
//!         if let MyEnum::Struct { field_0, field_1 } = enum_variant {
//!             Ok(Struct { field_0, field_1 })
//!         } else {
//!             Err(enum_variant)
//!         }
//!     }
//! }
//!
//! # pub enum MyEnum {
//! #     /// Unit variant.
//! #     Unit,
//! #     /// Tuple variant.
//! #     Tuple(u32, u64),
//! #     /// Struct variant.
//! #     Struct {
//! #         field_0: u32,
//! #         field_1: u64,
//! #     },
//! # }
//! #
//! ```
//!
//! </details>
//!
//! ### Additional options specified by an `evt` attribute on enum:
//!
//! * `#[evt(derive(Clone, Copy))]`: Derives `Clone`, `Copy` on **every**
//!   variant.
//! * `#[evt(module = "module1")]`: Generated structs are placed into `mod
//!   module1 { ... }`.
//! * `#[evt(implement_marker_traits(MarkerTrait1))]`: Generated structs all
//!   `impl MarkerTrait1`.

extern crate alloc;
extern crate proc_macro;

use alloc::vec::Vec;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use proc_macro_roids::{namespace_parameters, FieldsExt};
use quote::quote;
use syn::{
    parse_macro_input, parse_quote, Attribute, Data, DataEnum, DeriveInput, Field, Fields, LitStr,
    Meta, Path,
};

/// Attributes that should be copied across.
const ATTRIBUTES_TO_COPY: &[&str] = &["doc", "cfg", "allow", "deny"];

/// Derives a struct for each enum variant.
///
/// Struct fields including their attributes are copied over.
#[cfg(not(tarpaulin_include))]
#[proc_macro_derive(EnumVariantType, attributes(evt))]
pub fn enum_variant_type(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);

    // Need to do this, otherwise we can't unit test the input.
    enum_variant_type_impl(ast).into()
}

#[inline]
fn enum_variant_type_impl(ast: DeriveInput) -> proc_macro2::TokenStream {
    let enum_name = &ast.ident;
    let vis = &ast.vis;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    let data_enum = data_enum(&ast);
    let variants = &data_enum.variants;

    let mut wrap_in_module = None::<Ident>;
    let mut derive_for_all_variants = None::<Attribute>;
    let mut marker_trait_paths = Vec::<Path>::new();
    let mut repr_c = false;

    for attr in ast.attrs.iter() {
        if attr.path().is_ident("repr") {
            // wrap each enum struct in "repr(C)" ?
            if let Meta::List(list) = &attr.meta {
                list.parse_nested_meta(|parse_nested_meta| {
                    if parse_nested_meta.path.is_ident("C") {
                        repr_c = true;
                    }
                    Ok(())
                })
                .unwrap_or_else(|e| panic!("Failed to parse repr attribute. Error: {}", e));
            }
        } else if attr.path().is_ident("evt") {
            attr.parse_nested_meta(|nested_meta| {
                if nested_meta.path.is_ident("module") {
                    // `#[evt(module = \"some_module_name\")]`
                    let module_name: LitStr = nested_meta
                        .value()
                        .and_then(|value| value.parse())
                        .unwrap_or_else(|e| {
                            panic!(
                                "Expected `evt` attribute argument in the form: \
                                    `#[evt(module = \"some_module_name\")]`. Error: {}",
                                e
                            )
                        });

                    wrap_in_module = Some(Ident::new(&module_name.value(), Span::call_site()));
                    return Ok(());
                }
                // `#[evt(derive(Clone, Debug))]`
                if nested_meta.path.is_ident("derive") {
                    let mut items = Vec::new();
                    nested_meta.parse_nested_meta(|parse_nested_meta| {
                        items.push(parse_nested_meta.path);
                        Ok(())
                    })?;

                    derive_for_all_variants = Some(parse_quote! {
                        #[derive( #(#items),* )]
                    });
                    return Ok(());
                }

                // `#[evt(implement_marker_traits(MarkerTrait1, MarkerTrait2))]`
                if nested_meta.path.is_ident("implement_marker_traits") {
                    nested_meta.parse_nested_meta(|parse_nested_meta| {
                        marker_trait_paths.push(parse_nested_meta.path);
                        Ok(())
                    })?;

                    return Ok(());
                }

                panic!(
                    "Unexpected usage of `evt` attribute, please see  examples at:\n\
                        <https://docs.rs/enum_variant_type/>"
                )
            })
            .unwrap_or_else(|e| {
                panic!("Failed to process evt attribute. Error: {}", e);
            });
        }
    }

    let mut struct_declarations = proc_macro2::TokenStream::new();

    let ns: Path = parse_quote!(evt);
    let skip: Path = parse_quote!(skip);
    let struct_declarations_iter = variants.iter()
        .filter(|variant| !proc_macro_roids::contains_tag(&variant.attrs,  &ns, &skip))
        .map(|variant| {

        let variant_name = &variant.ident;
        let attrs_to_copy = variant
            .attrs
            .iter()
            .filter(|attribute| {
                ATTRIBUTES_TO_COPY
                    .iter()
                    .any(|attr_to_copy| attribute.path().is_ident(attr_to_copy))
            })
            .collect::<Vec<&Attribute>>();

        let evt_meta_lists = namespace_parameters(&variant.attrs, &ns);
        let mut variant_struct_attrs = evt_meta_lists
            .into_iter()
            .fold(
                proc_macro2::TokenStream::new(),
                |mut attrs_tokens, variant_struct_attr| {
                    attrs_tokens.extend(quote!(#[#variant_struct_attr]));
                    attrs_tokens
                },
            );

        if repr_c {
            variant_struct_attrs.extend(quote! {
                 #[repr(C)]
            })
        }

        let variant_fields = &variant.fields;

        // Need to attach visibility modifier to fields.
        let fields_with_vis = variant_fields
            .iter()
            .cloned()
            .map(|mut field| {
                field.vis = vis.clone();
                field
            })
            .collect::<Vec<Field>>();

        let data_struct = match variant_fields {
            Fields::Unit => quote! {
                struct #variant_name;
            },
            Fields::Unnamed(..) => {
                quote! {
                    struct #variant_name #ty_generics (#(#fields_with_vis,)*) #where_clause;
                }
            }
            Fields::Named(..) => quote! {
                struct #variant_name #ty_generics #where_clause {
                    #(#fields_with_vis,)*
                }
            },
        };

        // TODO: This generates invalid code if the type parameter is not used by this variant.
        let construction_form = variant_fields.construction_form();
        let deconstruct_variant_struct = if variant_fields.is_unit() {
            proc_macro2::TokenStream::new()
        } else {
            quote! {
                let #variant_name #construction_form = variant_struct;
            }
        };
        let impl_from_variant_for_enum = quote! {
            impl #impl_generics core::convert::From<#variant_name #ty_generics>
                for #enum_name #ty_generics
            #where_clause {
                fn from(variant_struct: #variant_name #ty_generics) -> Self {
                    // Deconstruct the parameter.
                    #deconstruct_variant_struct

                    #enum_name::#variant_name #construction_form
                }
            }
        };

        let impl_try_from_enum_for_variant = quote! {
            impl #impl_generics core::convert::TryFrom<#enum_name #ty_generics>
                for #variant_name #ty_generics
            #where_clause {
                type Error = #enum_name #ty_generics;

                fn try_from(enum_variant: #enum_name #ty_generics) -> Result<Self, Self::Error> {
                    // Deconstruct the variant.
                    if let #enum_name::#variant_name #construction_form = enum_variant {
                        core::result::Result::Ok(#variant_name #construction_form)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }
        };

        quote! {
            #(#attrs_to_copy)*
            #derive_for_all_variants
            #variant_struct_attrs
            #vis #data_struct

            #impl_from_variant_for_enum

            #impl_try_from_enum_for_variant

            #(impl #ty_generics #marker_trait_paths for #variant_name #ty_generics {})*
        }
    });
    struct_declarations.extend(struct_declarations_iter);

    if let Some(module_to_wrap_in) = wrap_in_module {
        quote! {
            #vis mod #module_to_wrap_in {
                use super::*;

                #struct_declarations
            }
        }
    } else {
        struct_declarations
    }
}

fn data_enum(ast: &DeriveInput) -> &DataEnum {
    if let Data::Enum(data_enum) = &ast.data {
        data_enum
    } else {
        panic!("`EnumVariantType` derive can only be used on an enum.");
    }
}

#[cfg(test)]
mod tests {
    extern crate alloc;

    use alloc::string::ToString;
    use pretty_assertions::assert_eq;
    use quote::quote;
    use syn::{parse_quote, DeriveInput};

    use super::enum_variant_type_impl;

    #[test]
    fn generates_correct_tokens_for_basic_enum() {
        let ast: DeriveInput = parse_quote! {
            pub enum MyEnum {
                /// Unit variant.
                #[evt(derive(Clone, Copy, Debug, PartialEq))]
                Unit,
                /// Tuple variant.
                #[evt(derive(Debug))]
                Tuple(u32, u64),
                /// Struct variant.
                Struct {
                    field_0: u32,
                    field_1: u64,
                },
            }
        };

        let actual_tokens = enum_variant_type_impl(ast);
        let expected_tokens = quote! {
            /// Unit variant.
            #[derive(Clone, Copy, Debug, PartialEq)]
            pub struct Unit;

            impl core::convert::From<Unit> for MyEnum {
                fn from(variant_struct: Unit) -> Self {
                    MyEnum::Unit
                }
            }

            impl core::convert::TryFrom<MyEnum> for Unit {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::Unit = enum_variant {
                        core::result::Result::Ok(Unit)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }

            /// Tuple variant.
            #[derive(Debug)]
            pub struct Tuple(pub u32, pub u64,);

            impl core::convert::From<Tuple> for MyEnum {
                fn from(variant_struct: Tuple) -> Self {
                    let Tuple(_0, _1,) = variant_struct;
                    MyEnum::Tuple(_0, _1,)
                }
            }

            impl core::convert::TryFrom<MyEnum> for Tuple {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::Tuple(_0, _1,) = enum_variant {
                        core::result::Result::Ok(Tuple(_0, _1,))
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }

            /// Struct variant.
            pub struct Struct {
                pub field_0: u32,
                pub field_1: u64,
            }

            impl core::convert::From<Struct> for MyEnum {
                fn from(variant_struct: Struct) -> Self {
                    let Struct { field_0, field_1, } = variant_struct;
                    MyEnum::Struct { field_0, field_1, }
                }
            }

            impl core::convert::TryFrom<MyEnum> for Struct {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::Struct { field_0, field_1, } = enum_variant {
                        core::result::Result::Ok(Struct { field_0, field_1, })
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }
        };

        assert_eq!(expected_tokens.to_string(), actual_tokens.to_string());
    }

    #[test]
    fn skips_variants_marked_with_evt_skip() {
        let ast: DeriveInput = parse_quote! {
            pub enum MyEnum {
                /// Unit variant.
                #[evt(derive(Clone, Copy, Debug, PartialEq))]
                Unit,
                /// Skipped variant.
                #[evt(skip)]
                UnitSkipped,
            }
        };

        let actual_tokens = enum_variant_type_impl(ast);
        let expected_tokens = quote! {
            /// Unit variant.
            #[derive(Clone, Copy, Debug, PartialEq)]
            pub struct Unit;

            impl core::convert::From<Unit> for MyEnum {
                fn from(variant_struct: Unit) -> Self {
                    MyEnum::Unit
                }
            }

            impl core::convert::TryFrom<MyEnum> for Unit {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::Unit = enum_variant {
                        core::result::Result::Ok(Unit)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }
        };

        assert_eq!(expected_tokens.to_string(), actual_tokens.to_string());
    }

    #[test]
    fn put_variants_in_module() {
        let ast: DeriveInput = parse_quote! {
            #[evt(module = "example")]
            pub enum MyEnum {
                A,
                B
            }
        };

        let actual_tokens = enum_variant_type_impl(ast);
        let expected_tokens = quote! {
            pub mod example {
                use super::*;

                pub struct A;

                impl core::convert::From<A> for MyEnum {
                    fn from(variant_struct: A) -> Self {
                        MyEnum::A
                    }
                }

                impl core::convert::TryFrom<MyEnum> for A {
                    type Error = MyEnum;
                    fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                        if let MyEnum::A = enum_variant {
                            core::result::Result::Ok(A)
                        } else {
                            core::result::Result::Err(enum_variant)
                        }
                    }
                }

                pub struct B;

                impl core::convert::From<B> for MyEnum {
                    fn from(variant_struct: B) -> Self {
                        MyEnum::B
                    }
                }

                impl core::convert::TryFrom<MyEnum> for B {
                    type Error = MyEnum;
                    fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                        if let MyEnum::B = enum_variant {
                            core::result::Result::Ok(B)
                        } else {
                            core::result::Result::Err(enum_variant)
                        }
                    }
                }
            }
        };

        assert_eq!(expected_tokens.to_string(), actual_tokens.to_string());
    }

    #[test]
    fn derive_traits_for_all_variants() {
        let ast: DeriveInput = parse_quote! {
            #[evt(derive(Debug))]
            pub enum MyEnum {
                A,
                #[evt(derive(Clone))]
                B
            }
        };

        let actual_tokens = enum_variant_type_impl(ast);
        let expected_tokens = quote! {
            #[derive(Debug)]
            pub struct A;

            impl core::convert::From<A> for MyEnum {
                fn from(variant_struct: A) -> Self {
                    MyEnum::A
                }
            }

            impl core::convert::TryFrom<MyEnum> for A {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::A = enum_variant {
                        core::result::Result::Ok(A)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }

            #[derive(Debug)]
            #[derive(Clone)]
            pub struct B;

            impl core::convert::From<B> for MyEnum {
                fn from(variant_struct: B) -> Self {
                    MyEnum::B
                }
            }

            impl core::convert::TryFrom<MyEnum> for B {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::B = enum_variant {
                        core::result::Result::Ok(B)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }
        };

        assert_eq!(expected_tokens.to_string(), actual_tokens.to_string());
    }

    #[test]
    fn derive_marker_trait() {
        let ast: DeriveInput = parse_quote! {
            #[evt(implement_marker_traits(MarkerTrait1))]
            pub enum MyEnum {
                A,
                B
            }
        };

        let actual_tokens = enum_variant_type_impl(ast);
        let expected_tokens = quote! {
            pub struct A;

            impl core::convert::From<A> for MyEnum {
                fn from(variant_struct: A) -> Self {
                    MyEnum::A
                }
            }

            impl core::convert::TryFrom<MyEnum> for A {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::A = enum_variant {
                        core::result::Result::Ok(A)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }

            impl MarkerTrait1 for A {}

            pub struct B;

            impl core::convert::From<B> for MyEnum {
                fn from(variant_struct: B) -> Self {
                    MyEnum::B
                }
            }

            impl core::convert::TryFrom<MyEnum> for B {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::B = enum_variant {
                        core::result::Result::Ok(B)
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }

            impl MarkerTrait1 for B {}
        };

        assert_eq!(expected_tokens.to_string(), actual_tokens.to_string());
    }

    #[test]
    fn derive_marker_repr() {
        let ast: DeriveInput = parse_quote! {
            #[derive(Debug)]
            #[repr(C)]
            pub enum MyEnum {
                A { i: i64 },
                B { i: i64 },
            }
        };

        let actual_tokens = enum_variant_type_impl(ast);
        let expected_tokens = quote! {

            #[repr(C)]
            pub struct A { pub i: i64, }

            impl core::convert::From<A> for MyEnum {
                fn from(variant_struct: A) -> Self {
                    let A { i, } = variant_struct;
                    MyEnum::A { i, }
                }
            }

            impl core::convert::TryFrom<MyEnum> for A {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::A { i, } = enum_variant {
                        core::result::Result::Ok(A { i, })
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }

            #[repr(C)]
            pub struct B { pub i: i64, }

            impl core::convert::From<B> for MyEnum {
                fn from(variant_struct: B) -> Self {
                    let B { i, } = variant_struct;
                    MyEnum::B { i, }
                }
            }

            impl core::convert::TryFrom<MyEnum> for B {
                type Error = MyEnum;
                fn try_from(enum_variant: MyEnum) -> Result<Self, Self::Error> {
                    if let MyEnum::B { i, } = enum_variant {
                        core::result::Result::Ok(B { i, })
                    } else {
                        core::result::Result::Err(enum_variant)
                    }
                }
            }
        };

        assert_eq!(expected_tokens.to_string(), actual_tokens.to_string());
    }
}