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
// Take a look at the license at the top of the repository in the LICENSE file.

use heck::ToKebabCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Data, DeriveInput, Fields, FieldsNamed, FieldsUnnamed, Generics, Ident, Type};

use crate::utils::crate_ident_new;

pub fn impl_variant(input: DeriveInput) -> syn::Result<TokenStream> {
    match input.data {
        Data::Struct(data_struct) => Ok(derive_variant_for_struct(
            input.ident,
            input.generics,
            data_struct,
        )),
        Data::Enum(data_enum) => {
            let mode = get_enum_mode(&input.attrs)?;
            let has_data = data_enum
                .variants
                .iter()
                .any(|v| !matches!(v.fields, syn::Fields::Unit));
            if has_data {
                derive_variant_for_enum(input.ident, input.generics, data_enum, mode)
            } else {
                Ok(derive_variant_for_c_enum(
                    input.ident,
                    input.generics,
                    data_enum,
                    mode,
                ))
            }
        }
        Data::Union(..) => Err(syn::Error::new_spanned(
            input,
            "#[derive(glib::Variant)] is not available for unions.",
        )),
    }
}

fn derive_variant_for_struct(
    ident: Ident,
    generics: Generics,
    data_struct: syn::DataStruct,
) -> TokenStream {
    let glib = crate_ident_new();
    let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
    let (static_variant_type, to_variant, from_variant) = match data_struct.fields {
        Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
            let types = unnamed
                .into_pairs()
                .map(|pair| pair.into_value())
                .map(|field| field.ty)
                .collect::<Vec<_>>();

            let idents = (0..types.len()).map(syn::Index::from).collect::<Vec<_>>();
            let idents_len = idents.len();

            let static_variant_type = quote! {
                impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause {
                    #[inline]
                    fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> {
                        static TYP: ::std::sync::OnceLock<#glib::VariantType> = ::std::sync::OnceLock::new();
                        ::std::borrow::Cow::Borrowed(TYP.get_or_init(|| {

                            let mut builder = #glib::GStringBuilder::new("(");

                            #(
                                {
                                    let typ = <#types as #glib::variant::StaticVariantType>::static_variant_type();
                                    builder.append(typ.as_str());
                                }
                            )*
                            builder.append_c(')');

                            #glib::VariantType::from_string(builder.into_string()).unwrap()
                        }))
                    }
                }
            };

            let to_variant = quote! {
                impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause {
                    fn to_variant(&self) -> #glib::Variant {
                        #glib::Variant::tuple_from_iter(::std::array::IntoIter::<#glib::Variant, #idents_len>::new([
                            #(
                                #glib::variant::ToVariant::to_variant(&self.#idents)
                            ),*
                        ]))
                    }
                }

                impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause {
                    fn from(v: #ident #type_generics) -> #glib::Variant {
                        #glib::Variant::tuple_from_iter(::std::array::IntoIter::<#glib::Variant, #idents_len>::new([
                            #(
                                <#glib::Variant as ::std::convert::From<_>>::from(v.#idents)
                            ),*
                        ]))
                    }
                }
            };

            let from_variant = quote! {
                impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause {
                    fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> {
                        if !variant.is_container() {
                            return ::core::option::Option::None;
                        }
                        ::core::option::Option::Some(Self(
                            #(
                                match variant.try_child_get::<#types>(#idents) {
                                    ::core::result::Result::Ok(::core::option::Option::Some(field)) => field,
                                    _ => return ::core::option::Option::None,
                                }
                            ),*
                        ))
                    }
                }
            };

            (static_variant_type, to_variant, from_variant)
        }
        Fields::Named(FieldsNamed { named, .. }) => {
            let fields: Vec<(Ident, Type)> = named
                .into_pairs()
                .map(|pair| pair.into_value())
                .map(|field| (field.ident.expect("Field ident is specified"), field.ty))
                .collect();

            let idents: Vec<_> = fields.iter().map(|(ident, _ty)| ident).collect();
            let types: Vec<_> = fields.iter().map(|(_ident, ty)| ty).collect();
            let counts = (0..types.len()).map(syn::Index::from).collect::<Vec<_>>();

            let static_variant_type = quote! {
                impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause {
                    #[inline]
                    fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> {
                        static TYP: ::std::sync::OnceLock<#glib::VariantType> = ::std::sync::OnceLock::new();
                        ::std::borrow::Cow::Borrowed(TYP.get_or_init(|| unsafe {
                            let ptr = #glib::ffi::g_string_sized_new(16);
                            #glib::ffi::g_string_append_c(ptr, b'(' as _);

                            #(
                                {
                                    let typ = <#types as #glib::variant::StaticVariantType>::static_variant_type();
                                    #glib::ffi::g_string_append_len(
                                        ptr,
                                        typ.as_str().as_ptr() as *const _,
                                        typ.as_str().len() as isize,
                                    );
                                }
                            )*
                            #glib::ffi::g_string_append_c(ptr, b')' as _);

                            #glib::translate::from_glib_full(
                                #glib::ffi::g_string_free(ptr, #glib::ffi::GFALSE) as *mut #glib::ffi::GVariantType
                            )
                        }))
                    }
                }
            };

            let to_variant = quote! {
                impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause {
                    fn to_variant(&self) -> #glib::Variant {
                        #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([
                            #(
                                #glib::variant::ToVariant::to_variant(&self.#idents)
                            ),*
                        ]))
                    }
                }

                impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause {
                    fn from(v: #ident #type_generics) -> #glib::Variant {
                        #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([
                            #(
                                <#glib::Variant as ::std::convert::From<_>>::from(v.#idents)
                            ),*
                        ]))
                    }
                }
            };

            let from_variant = quote! {
                impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause {
                    fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> {
                        if !variant.is_container() {
                            return ::core::option::Option::None;
                        }
                        ::core::option::Option::Some(Self {
                            #(
                                #idents: match variant.try_child_get::<#types>(#counts) {
                                    ::core::result::Result::Ok(::core::option::Option::Some(field)) => field,
                                    _ => return ::core::option::Option::None,
                                }
                            ),*
                        })
                    }
                }
            };

            (static_variant_type, to_variant, from_variant)
        }
        Fields::Unit => {
            let static_variant_type = quote! {
                impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause {
                    #[inline]
                    fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> {
                        ::std::borrow::Cow::Borrowed(#glib::VariantTy::UNIT)
                    }
                }
            };

            let to_variant = quote! {
                impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause {
                    #[inline]
                    fn to_variant(&self) -> #glib::Variant {
                        #glib::variant::ToVariant::to_variant(&())
                    }
                }

                impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause {
                    #[inline]
                    fn from(v: #ident #type_generics) -> #glib::Variant {
                        #glib::variant::ToVariant::to_variant(&())
                    }
                }
            };

            let from_variant = quote! {
                impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause {
                    fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> {
                        ::core::option::Option::Some(Self)
                    }
                }
            };

            (static_variant_type, to_variant, from_variant)
        }
    };

    quote! {
        #static_variant_type

        #to_variant

        #from_variant
    }
}

enum EnumMode {
    String,
    Repr(Ident),
    Enum { repr: bool },
    Flags { repr: bool },
}

impl EnumMode {
    fn tag_type(&self) -> char {
        match self {
            EnumMode::String => 's',
            EnumMode::Repr(repr) => match repr.to_string().as_str() {
                "i8" | "i16" => 'n',
                "i32" => 'i',
                "i64" => 'x',
                "u8" => 'y',
                "u16" => 'q',
                "u32" => 'u',
                "u64" => 't',
                _ => unimplemented!(),
            },
            EnumMode::Enum { repr } => {
                if *repr {
                    'i'
                } else {
                    's'
                }
            }
            EnumMode::Flags { repr } => {
                if *repr {
                    'u'
                } else {
                    's'
                }
            }
        }
    }
}

fn derive_variant_for_enum(
    ident: Ident,
    generics: Generics,
    data_enum: syn::DataEnum,
    mode: EnumMode,
) -> syn::Result<TokenStream> {
    let glib = crate_ident_new();
    let static_variant_type = format!("({}v)", mode.tag_type());
    let (impl_generics, type_generics, where_clause) = generics.split_for_impl();

    let to = data_enum.variants.iter().enumerate().map(|(index, v)| {
        let ident = &v.ident;
        let tag = match &mode {
            EnumMode::String => {
                let nick = ToKebabCase::to_kebab_case(ident.to_string().as_str());
                quote! { #nick }
            },
            EnumMode::Repr(repr) => quote! { #index as #repr },
            _ => unimplemented!(),
        };
        if !matches!(v.fields, syn::Fields::Unit) {
            match &mode {
                EnumMode::Enum { .. } =>
                    return Err(syn::Error::new_spanned(v, "#[variant_enum(enum) only allowed with C-style enums using #[derive(glib::Enum)]")),
                EnumMode::Flags { .. } =>
                    return Err(syn::Error::new_spanned(v, "#[variant_enum(flags) only allowed with bitflags using #[glib::flags]")),
                _ => (),
            }
        }
        Ok(match &v.fields {
            syn::Fields::Named(FieldsNamed { named, .. }) => {
                let field_names = named.iter().map(|f| f.ident.as_ref().unwrap());
                let field_names2 = field_names.clone();
                quote! {
                    Self::#ident { #(#field_names),* } => #glib::variant::ToVariant::to_variant(&(
                        #tag,
                        #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([
                            #(#glib::variant::ToVariant::to_variant(&#field_names2)),*
                        ]))
                    ))
                }
            },
            syn::Fields::Unnamed(FieldsUnnamed  { unnamed, .. }) => {
                let field_names = unnamed.iter().enumerate().map(|(i, _)| {
                    format_ident!("field{}", i)
                });
                let field_names2 = field_names.clone();
                quote! {
                    Self::#ident(#(#field_names),*) => #glib::variant::ToVariant::to_variant(&(
                        #tag,
                        #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([
                            #(#glib::variant::ToVariant::to_variant(&#field_names2)),*
                        ]))
                    ))
                }
            },
            syn::Fields::Unit => {
                quote! {
                    Self::#ident => #glib::variant::ToVariant::to_variant(&(
                        #tag,
                        #glib::variant::ToVariant::to_variant(&())
                    ))
                }
            },
        })
    }).collect::<Result<Vec<_>, _>>()?;
    let into = data_enum.variants.iter().enumerate().map(|(index, v)| {
        let field_ident = &v.ident;
        let tag = match &mode {
            EnumMode::String => {
                let nick = ToKebabCase::to_kebab_case(field_ident.to_string().as_str());
                quote! { #nick }
            }
            EnumMode::Repr(repr) => quote! { #index as #repr },
            _ => unimplemented!(),
        };
        match &v.fields {
            syn::Fields::Named(FieldsNamed { named, .. }) => {
                let field_names = named.iter().map(|f| f.ident.as_ref().unwrap());
                let field_names2 = field_names.clone();
                quote! {
                    #ident::#field_ident { #(#field_names),* } => #glib::variant::ToVariant::to_variant(&(
                        #tag,
                        #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([
                            #(<#glib::Variant as ::std::convert::From<_>>::from(#field_names2)),*
                        ]))
                    ))
                }
            }
            syn::Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
                let field_names = unnamed
                    .iter()
                    .enumerate()
                    .map(|(i, _)| format_ident!("field{}", i));
                let field_names2 = field_names.clone();
                quote! {
                    #ident::#field_ident(#(#field_names),*) => #glib::variant::ToVariant::to_variant(&(
                        #tag,
                        #glib::Variant::tuple_from_iter(::std::iter::IntoIterator::into_iter([
                            #(<#glib::Variant as ::std::convert::From<_>>::from(#field_names2)),*
                        ]))
                    ))
                }
            }
            syn::Fields::Unit => {
                quote! {
                    #ident::#field_ident => #glib::variant::ToVariant::to_variant(&(
                        #tag,
                        #glib::variant::ToVariant::to_variant(&())
                    ))
                }
            }
        }
    });
    let from = data_enum.variants.iter().enumerate().map(|(index, v)| {
        let ident = &v.ident;
        let tag = match &mode {
            EnumMode::String => {
                let nick = ToKebabCase::to_kebab_case(ident.to_string().as_str());
                quote! { #nick }
            }
            EnumMode::Repr(_) => quote! { #index },
            _ => unimplemented!(),
        };
        match &v.fields {
            syn::Fields::Named(FieldsNamed { named, .. }) => {
                let fields = named.iter().enumerate().map(|(index, f)| {
                    let name = f.ident.as_ref().unwrap();
                    let repr = &f.ty;
                    quote! {
                        #name: <#repr as #glib::variant::FromVariant>::from_variant(
                            &#glib::Variant::try_child_value(&value, #index)?
                        )?
                    }
                });
                quote! { #tag => ::core::option::Option::Some(Self::#ident { #(#fields),* }), }
            }
            syn::Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
                let indices = 0..unnamed.iter().count();
                let repr = unnamed.iter().map(|f| &f.ty);
                quote! {
                    #tag => ::core::option::Option::Some(Self::#ident(
                        #(
                            <#repr as #glib::variant::FromVariant>::from_variant(
                                &#glib::Variant::try_child_value(&value, #indices)?
                            )?
                        ),*
                    )),
                }
            }
            syn::Fields::Unit => {
                quote! { #tag => ::core::option::Option::Some(Self::#ident), }
            }
        }
    });

    let (repr, tag_match) = match &mode {
        EnumMode::String => (quote! { String }, quote! { tag.as_str() }),
        EnumMode::Repr(repr) => (quote! { #repr }, quote! { tag as usize }),
        _ => unimplemented!(),
    };

    Ok(quote! {
        impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause {
            #[inline]
            fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> {
                ::std::borrow::Cow::Borrowed(
                    unsafe {
                        #glib::VariantTy::from_str_unchecked(#static_variant_type)
                    }
                )
            }
        }

        impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause {
            fn to_variant(&self) -> #glib::Variant {
                match self {
                    #(#to),*
                }
            }
        }

        impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause {
            fn from(v: #ident #type_generics) -> #glib::Variant {
                match v {
                    #(#into),*
                }
            }
        }

        impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause {
            fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> {
                let (tag, value) = <(#repr, #glib::Variant) as #glib::variant::FromVariant>::from_variant(&variant)?;
                if !#glib::VariantTy::is_tuple(#glib::Variant::type_(&value)) {
                    return ::core::option::Option::None;
                }
                match #tag_match {
                    #(#from)*
                    _ => ::core::option::Option::None
                }
            }
        }
    })
}

fn derive_variant_for_c_enum(
    ident: Ident,
    generics: Generics,
    data_enum: syn::DataEnum,
    mode: EnumMode,
) -> TokenStream {
    let glib = crate_ident_new();
    let static_variant_type = mode.tag_type().to_string();
    let (impl_generics, type_generics, where_clause) = generics.split_for_impl();

    let (to_variant, from_variant) = match mode {
        EnumMode::String => {
            let idents = data_enum.variants.iter().map(|v| &v.ident);
            let nicks = data_enum.variants.iter().map(|v| {
                let nick = ToKebabCase::to_kebab_case(v.ident.to_string().as_str());
                quote! { #nick }
            });
            let idents2 = idents.clone();
            let nicks2 = nicks.clone();
            (
                quote! {
                    #glib::variant::ToVariant::to_variant(match self {
                        #(Self::#idents => #nicks),*
                    })
                },
                quote! {
                    let tag = #glib::Variant::str(&variant)?;
                    match tag {
                        #(#nicks2 => ::core::option::Option::Some(Self::#idents2),)*
                        _ => ::core::option::Option::None
                    }
                },
            )
        }
        EnumMode::Repr(repr) => {
            let idents = data_enum.variants.iter().map(|v| &v.ident);
            (
                quote! {
                    #glib::variant::ToVariant::to_variant(&(*self as #repr))
                },
                quote! {
                    let value = <#repr as #glib::variant::FromVariant>::from_variant(&variant)?;
                    #(if value == Self::#idents as #repr {
                        return ::core::option::Option::Some(Self::#idents);
                    })*
                    ::core::option::Option::None
                },
            )
        }
        EnumMode::Enum { repr: true } => (
            quote! {
                #glib::variant::ToVariant::to_variant(&(*self as i32))
            },
            quote! {
                let value = <i32 as #glib::variant::FromVariant>::from_variant(&variant)?;
                unsafe { #glib::translate::try_from_glib(value) }.ok()
            },
        ),
        EnumMode::Enum { repr: false } => (
            quote! {
                let enum_class = #glib::EnumClass::new::<Self>();
                let value = <Self as #glib::translate::IntoGlib>::into_glib(*self);
                let value = #glib::EnumClass::value(&enum_class, value);
                let value = ::core::option::Option::unwrap(value);
                let nick = #glib::EnumValue::nick(&value);
                #glib::variant::ToVariant::to_variant(nick)
            },
            quote! {
                let enum_class = #glib::EnumClass::new::<Self>();
                let tag = #glib::Variant::str(&variant)?;
                let value = #glib::EnumClass::value_by_nick(&enum_class, tag)?;
                let value = #glib::EnumValue::value(&value);
                unsafe { #glib::translate::try_from_glib(value) }.ok()
            },
        ),
        EnumMode::Flags { repr: true } => (
            quote! {
                #glib::variant::ToVariant::to_variant(&self.bits())
            },
            quote! {
                let value = <u32 as #glib::variant::FromVariant>::from_variant(&variant)?;
                Self::from_bits(value)
            },
        ),
        EnumMode::Flags { repr: false } => (
            quote! {
                let flags_class = #glib::FlagsClass::new::<Self>();
                let value = <Self as #glib::translate::IntoGlib>::into_glib(*self);
                let s = #glib::FlagsClass::to_nick_string(&flags_class, value);
                #glib::variant::ToVariant::to_variant(&s)
            },
            quote! {
                let flags_class = #glib::FlagsClass::new::<Self>();
                let s = #glib::Variant::str(&variant)?;
                let value = #glib::FlagsClass::from_nick_string(&flags_class, s).ok()?;
                ::core::option::Option::Some(unsafe { #glib::translate::from_glib(value) })
            },
        ),
    };

    quote! {
        impl #impl_generics #glib::variant::StaticVariantType for #ident #type_generics #where_clause {
            #[inline]
            fn static_variant_type() -> ::std::borrow::Cow<'static, #glib::VariantTy> {
                ::std::borrow::Cow::Borrowed(
                    unsafe {
                        #glib::VariantTy::from_str_unchecked(#static_variant_type)
                    }
                )
            }
        }

        impl #impl_generics #glib::variant::ToVariant for #ident #type_generics #where_clause {
            fn to_variant(&self) -> #glib::Variant {
                #to_variant
            }
        }

        impl #impl_generics ::std::convert::From<#ident #type_generics> for #glib::Variant #where_clause {
            #[inline]
            fn from(v: #ident #type_generics) -> #glib::Variant {
                <#ident #type_generics as #glib::variant::ToVariant>::to_variant(&v)
            }
        }

        impl #impl_generics #glib::variant::FromVariant for #ident #type_generics #where_clause {
            fn from_variant(variant: &#glib::Variant) -> ::core::option::Option<Self> {
                #from_variant
            }
        }
    }
}

fn get_enum_mode(attrs: &[syn::Attribute]) -> syn::Result<EnumMode> {
    let attr = attrs.iter().find(|a| a.path().is_ident("variant_enum"));

    let Some(attr) = attr else {
        return Ok(EnumMode::String);
    };

    let mut repr_attr = None;
    let mut mode = EnumMode::String;
    attr.parse_nested_meta(|meta| {
        match meta.path.get_ident().map(|id| id.to_string()).as_deref() {
            Some("repr") => {
                repr_attr = Some(meta.path);
                Ok(())
            }
            Some("enum") => {
                mode = EnumMode::Enum { repr: false };
                Ok(())
            }
            Some("flags") => {
                mode = EnumMode::Flags { repr: false };
                Ok(())
            }
            _ => Err(syn::Error::new_spanned(
                meta.path,
                "unknown type in #[variant_enum] attribute",
            )),
        }
    })?;
    Ok(match mode {
        EnumMode::String if repr_attr.is_some() => {
            let repr_attr = repr_attr.unwrap();
            let repr = get_repr(attrs).ok_or_else(|| {
                syn::Error::new_spanned(
                    repr_attr,
                    "Must have #[repr] attribute with one of i8, i16, i32, i64, u8, u16, u32, u64",
                )
            })?;
            EnumMode::Repr(repr)
        }
        EnumMode::Enum { .. } => EnumMode::Enum {
            repr: repr_attr.is_some(),
        },
        EnumMode::Flags { .. } => EnumMode::Flags {
            repr: repr_attr.is_some(),
        },
        e => e,
    })
}

fn get_repr(attrs: &[syn::Attribute]) -> Option<Ident> {
    let attr = attrs.iter().find(|a| a.path().is_ident("repr"))?;
    let mut repr_ty = None;
    attr.parse_nested_meta(|meta| {
        repr_ty = Some(meta.path.get_ident().unwrap().clone());
        Ok(())
    })
    .unwrap();
    match repr_ty.as_ref()?.to_string().as_str() {
        "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => Some(repr_ty?),
        _ => None,
    }
}