sea-orm-macros 2.0.0-rc.38

Derive macros for SeaORM
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
use super::case_style::{CaseStyle, CaseStyleHelpers};
use super::util::camel_case_with_escaped_non_uax31;
use heck::ToUpperCamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned};
use syn::{Expr, Lit, LitInt, LitStr, UnOp, parse};

struct ActiveEnum {
    ident: syn::Ident,
    enum_name: String,
    rs_type: RsType,
    db_type: DbType,
    is_string: bool,
    variants: Vec<ActiveEnumVariant>,
    variant_idents: Vec<syn::Ident>,
    variant_values: Vec<TokenStream>,
    rename_all: Option<CaseStyle>,
}

enum RsType {
    String,
    Enum,
    Other(TokenStream),
}

impl RsType {
    fn from_attr(
        ident_span: proc_macro2::Span,
        rs_type: Option<String>,
        db_type: &DbType,
    ) -> Result<Self, Error> {
        if db_type.is_enum() {
            match rs_type.as_deref() {
                None => Ok(RsType::Enum),
                Some(value) => RsType::from_database_enum_attr_value(value).ok_or_else(|| {
                    Error::TT(quote_spanned! {
                        ident_span => compile_error!("`db_type = \"Enum\"` only supports `rs_type = \"String\"` or `rs_type = \"Enum\"` (or omit `rs_type`)");
                    })
                }),
            }
        } else {
            let rs_type = match rs_type {
                Some(rs_type) => rs_type,
                None => {
                    return Err(Error::TT(quote_spanned! {
                        ident_span => compile_error!("Missing macro attribute `rs_type`");
                    }));
                }
            };

            if rs_type == "Enum" {
                return Err(Error::TT(quote_spanned! {
                    ident_span => compile_error!("`rs_type = \"Enum\"` requires `db_type = \"Enum\"`");
                }));
            }

            RsType::from_str(&rs_type).map_err(Error::Syn)
        }
    }

    fn from_str(value: &str) -> syn::Result<Self> {
        Ok(Self::Other(syn::parse_str::<TokenStream>(value)?))
    }

    fn from_database_enum_attr_value(value: &str) -> Option<Self> {
        match value {
            "Enum" => Some(Self::Enum),
            "String" => Some(Self::String),
            _ => None,
        }
    }
}

impl quote::ToTokens for RsType {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            RsType::String => tokens.extend(quote! { String }),
            RsType::Enum => tokens.extend(quote! { sea_orm::sea_query::Enum }),
            RsType::Other(rs_type) => tokens.extend(rs_type.clone()),
        }
    }
}

enum DbType {
    Enum,
    Other(TokenStream),
}

impl DbType {
    fn from_attr(ident_span: proc_macro2::Span, db_type: Option<String>) -> Result<Self, Error> {
        let db_type = match db_type {
            Some(db_type) => db_type,
            None => {
                return Err(Error::TT(quote_spanned! {
                    ident_span => compile_error!("Missing macro attribute `db_type`");
                }));
            }
        };

        DbType::from_str(&db_type).map_err(Error::Syn)
    }

    fn from_str(value: &str) -> syn::Result<Self> {
        match value {
            "Enum" => Ok(Self::Enum),
            _ => Ok(Self::Other(syn::parse_str::<TokenStream>(value)?)),
        }
    }

    fn is_enum(&self) -> bool {
        matches!(self, DbType::Enum)
    }
}

struct ActiveEnumVariant {
    ident: syn::Ident,
    string_value: Option<LitStr>,
    num_value: Option<LitInt>,
    rename: Option<CaseStyle>,
}

enum Error {
    InputNotEnum,
    Syn(syn::Error),
    TT(TokenStream),
}

impl ActiveEnum {
    fn new(input: syn::DeriveInput) -> Result<Self, Error> {
        let ident_span = input.ident.span();
        let ident = input.ident;

        let mut enum_name = ident.to_string().to_upper_camel_case();
        let mut rs_type = None;
        let mut db_type = None;
        let mut rename_all = None;

        input
            .attrs
            .iter()
            .filter(|attr| attr.path().is_ident("sea_orm"))
            .try_for_each(|attr| {
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("rs_type") {
                        let litstr: LitStr = meta.value()?.parse()?;
                        rs_type = Some(litstr.value());
                    } else if meta.path.is_ident("db_type") {
                        let litstr: LitStr = meta.value()?.parse()?;
                        db_type = Some(litstr.value());
                    } else if meta.path.is_ident("enum_name") {
                        let litstr: LitStr = meta.value()?.parse()?;
                        enum_name = litstr.value();
                    } else if meta.path.is_ident("rename_all") {
                        rename_all = Some((&meta).try_into()?);
                    } else {
                        return Err(meta.error(format!(
                            "Unknown attribute parameter found: {:?}",
                            meta.path.get_ident()
                        )));
                    }
                    Ok(())
                })
                .map_err(Error::Syn)
            })?;

        let db_type = DbType::from_attr(ident_span, db_type)?;
        let rs_type = RsType::from_attr(ident_span, rs_type, &db_type)?;

        let variant_vec = match input.data {
            syn::Data::Enum(syn::DataEnum { variants, .. }) => variants,
            _ => return Err(Error::InputNotEnum),
        };

        let mut is_string = rename_all.is_some();
        let mut is_int = false;
        let mut variants = Vec::new();

        for variant in variant_vec {
            let variant_span = variant.ident.span();
            let mut string_value = None;
            let mut num_value = None;
            let mut rename_rule = None;

            for attr in variant.attrs.iter() {
                if !attr.path().is_ident("sea_orm") {
                    continue;
                }
                attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("string_value") {
                        is_string = true;
                        string_value = Some(meta.value()?.parse::<LitStr>()?);
                    } else if meta.path.is_ident("num_value") {
                        is_int = true;
                        num_value = Some(meta.value()?.parse::<LitInt>()?);
                    } else if meta.path.is_ident("display_value") {
                        // This is a placeholder to prevent the `display_value` proc_macro attribute of `DeriveDisplay`
                        // to be considered unknown attribute parameter
                        meta.value()?.parse::<LitStr>()?;
                    } else if meta.path.is_ident("rename") {
                        is_string = true;
                        rename_rule = Some((&meta).try_into()?);
                    } else {
                        return Err(meta.error(format!(
                            "Unknown attribute parameter found: {:?}",
                            meta.path.get_ident()
                        )));
                    }

                    Ok(())
                })
                .map_err(Error::Syn)?;
            }

            if is_string && is_int {
                return Err(Error::TT(quote_spanned! {
                    ident_span => compile_error!("All enum variants should specify the same `*_value` macro attribute, either `string_value` or `num_value` but not both");
                }));
            }

            if string_value.is_none() && num_value.is_none() && rename_rule.or(rename_all).is_none()
            {
                match variant.discriminant {
                    Some((_, Expr::Lit(exprlit))) => {
                        if let Lit::Int(litint) = exprlit.lit {
                            is_int = true;
                            num_value = Some(litint);
                        } else {
                            return Err(Error::TT(quote_spanned! {
                                variant_span => compile_error!("Enum variant discriminant is not an integer");
                            }));
                        }
                    }
                    //rust doesn't provide negative variants in enums as a single LitInt, this workarounds that
                    Some((_, Expr::Unary(exprnlit))) => {
                        if let UnOp::Neg(_) = exprnlit.op {
                            if let Expr::Lit(exprlit) = *exprnlit.expr {
                                if let Lit::Int(litint) = exprlit.lit {
                                    let negative_token = quote! { -#litint };
                                    let litint = parse(negative_token.into()).unwrap();

                                    is_int = true;
                                    num_value = Some(litint);
                                }
                            }
                        } else {
                            return Err(Error::TT(quote_spanned! {
                                variant_span => compile_error!("Only - token is supported in enum variants, not ! and *");
                            }));
                        }
                    }
                    _ => {
                        return Err(Error::TT(quote_spanned! {
                            variant_span => compile_error!("Missing macro attribute, either `string_value`, `num_value` or `rename` should be specified or specify repr[X] and have a value for every entry");
                        }));
                    }
                }
            }

            variants.push(ActiveEnumVariant {
                ident: variant.ident,
                string_value,
                num_value,
                rename: rename_rule,
            });
        }

        if db_type.is_enum() && is_int {
            return Err(Error::TT(quote_spanned! {
                ident_span => compile_error!("`db_type = \"Enum\"` does not support `num_value` or numeric discriminants");
            }));
        }

        let variant_idents: Vec<syn::Ident> = variants
            .iter()
            .map(|variant| variant.ident.clone())
            .collect();

        let variant_values: Vec<TokenStream> = variants
            .iter()
            .map(|variant| {
                let variant_span = variant.ident.span();

                if let Some(string_value) = &variant.string_value {
                    let string = string_value.value();
                    Ok(quote! { #string })
                } else if let Some(num_value) = &variant.num_value {
                    Ok(quote! { #num_value })
                } else if let Some(rename_rule) = variant.rename.or(rename_all) {
                    let variant_ident = variant.ident.convert_case(Some(rename_rule));
                    Ok(quote! { #variant_ident })
                } else {
                    Err(Error::TT(quote_spanned! {
                        variant_span => compile_error!("Missing macro attribute, either `string_value`, `num_value` or `rename_all` should be specified");
                    }))
                }
            })
            .collect::<Result<_, _>>()?;

        Ok(Self {
            ident,
            enum_name,
            rs_type,
            db_type,
            is_string,
            variants,
            variant_idents,
            variant_values,
            rename_all,
        })
    }

    fn generate_enum_impls(&self) -> bool {
        self.db_type.is_enum() && matches!(self.rs_type, RsType::Enum)
    }

    fn to_value_impl(&self) -> TokenStream {
        let enum_name = &self.enum_name;
        let variant_idents = &self.variant_idents;
        let variant_values = &self.variant_values;

        if self.generate_enum_impls() {
            quote! {
                let value = match self {
                    #( Self::#variant_idents => #variant_values, )*
                };
                sea_orm::sea_query::Enum {
                    type_name: #enum_name.into(),
                    value: value.into(),
                }
            }
        } else {
            quote! {
                match self {
                    #( Self::#variant_idents => #variant_values, )*
                }
                .to_owned()
            }
        }
    }

    fn value_type_try_from_impl(&self) -> TokenStream {
        if self.generate_enum_impls() {
            quote! {
                use sea_orm::sea_query::{OptionEnum, Value, ValueTypeErr};

                match v {
                    Value::Enum(value) => match value {
                        OptionEnum::Some(value) => <Self as sea_orm::ActiveEnum>::try_from_value(value.as_ref())
                            .map_err(|_| ValueTypeErr),
                        OptionEnum::None(_) => Err(ValueTypeErr),
                    },
                    _ => Err(ValueTypeErr),
                }
            }
        } else {
            quote! {
                use sea_orm::sea_query::{ValueType, ValueTypeErr};

                let value = <<Self as sea_orm::ActiveEnum>::Value as ValueType>::try_from(v)?;
                <Self as sea_orm::ActiveEnum>::try_from_value(&value).map_err(|_| ValueTypeErr)
            }
        }
    }

    fn nullable_impl(&self) -> TokenStream {
        let ident = &self.ident;
        let enum_name = &self.enum_name;
        let nullable_value_impl = if self.generate_enum_impls() {
            quote! {
                use sea_orm::sea_query::{OptionEnum, Value};
                Value::Enum(OptionEnum::None(#enum_name.into()))
            }
        } else {
            quote! {
                use sea_orm::sea_query;
                <<Self as sea_orm::ActiveEnum>::Value as sea_query::Nullable>::null()
            }
        };

        quote! {
            #[automatically_derived]
            #[allow(unexpected_cfgs)]
            impl sea_orm::sea_query::Nullable for #ident {
                fn null() -> sea_orm::sea_query::Value {
                    #nullable_value_impl
                }
            }
        }
    }

    fn value_type_impl(&self) -> TokenStream {
        let ident = &self.ident;
        let value_type_try_from_impl = self.value_type_try_from_impl();
        let enum_name = &self.enum_name;

        let type_name_impl = quote! { stringify!(#ident).to_owned() };

        let value_type_array_type = if self.generate_enum_impls() {
            quote! {
                sea_orm::sea_query::ArrayType::Enum(Box::new(#enum_name.into()))
            }
        } else {
            quote! {
                <<Self as sea_orm::ActiveEnum>::Value as sea_orm::sea_query::ValueType>::array_type()
            }
        };

        let enum_type_name = if self.db_type.is_enum() {
            quote! { Some(#enum_name) }
        } else {
            quote! { None }
        };

        quote! {
            #[automatically_derived]
            #[allow(unexpected_cfgs)]
            impl sea_orm::sea_query::ValueType for #ident {
                fn try_from(v: sea_orm::sea_query::Value) -> std::result::Result<Self, sea_orm::sea_query::ValueTypeErr> {
                    #value_type_try_from_impl
                }

                fn type_name() -> String {
                    #type_name_impl
                }

                fn array_type() -> sea_orm::sea_query::ArrayType {
                    #value_type_array_type
                }

                fn column_type() -> sea_orm::sea_query::ColumnType {
                    <Self as sea_orm::ActiveEnum>::db_type()
                        .get_column_type()
                        .to_owned()
                        .into()
                }

                fn enum_type_name() -> Option<&'static str> {
                    #enum_type_name
                }
            }
        }
    }

    fn try_getable_impl(&self) -> TokenStream {
        let ident = &self.ident;
        let sqlx_postgres_try_get = if cfg!(feature = "sqlx-postgres") && self.db_type.is_enum() {
            quote! {
                if let Some(result) = res.try_get_from_sqlx_postgres::<Self, I>(idx) {
                    return result;
                }
            }
        } else {
            quote!()
        };
        let try_get_by_impl = {
            let enum_name = &self.enum_name;
            if self.generate_enum_impls() {
                quote! {
                    #sqlx_postgres_try_get
                    let value: String = <String as sea_orm::TryGetable>::try_get_by(res, idx)?;
                    let value = sea_orm::sea_query::Enum {
                        type_name: #enum_name.into(),
                        value: value.into(),
                    };
                    <Self as sea_orm::ActiveEnum>::try_from_value(&value)
                        .map_err(sea_orm::TryGetError::DbErr)
                }
            } else {
                quote! {
                    #sqlx_postgres_try_get
                    let value = <<Self as sea_orm::ActiveEnum>::Value as sea_orm::TryGetable>::try_get_by(res, idx)?;
                    <Self as sea_orm::ActiveEnum>::try_from_value(&value)
                        .map_err(sea_orm::TryGetError::DbErr)
                }
            }
        };

        quote! {
            #[automatically_derived]
            impl sea_orm::TryGetable for #ident {
                fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, idx: I) -> std::result::Result<Self, sea_orm::TryGetError> {
                    #try_get_by_impl
                }
            }
        }
    }

    fn active_enum_impl(&self) -> TokenStream {
        let ident = &self.ident;
        let enum_name_iden = format_ident!("{}Enum", ident);
        let rs_type = &self.rs_type;
        let variant_idents = &self.variant_idents;
        let variant_values = &self.variant_values;
        let to_value_body = self.to_value_impl();
        let column_type = {
            match &self.db_type {
                DbType::Enum => quote! {
                    Enum {
                        name: <Self as sea_orm::ActiveEnum>::name(),
                        variants: Self::iden_values(),
                    }
                },
                DbType::Other(db_type) => db_type.clone(),
            }
        };

        let val = if self.generate_enum_impls() {
            quote! { v.value.as_ref() }
        } else if self.is_string {
            quote! { <<Self as sea_orm::ActiveEnum>::Value as std::convert::AsRef<str>>::as_ref(v) }
        } else {
            quote! { v }
        };

        quote! {
            #[automatically_derived]
            impl sea_orm::ActiveEnum for #ident {
                type Value = #rs_type;

                type ValueVec = Vec<#rs_type>;

                fn name() -> sea_orm::sea_query::DynIden {
                    #enum_name_iden.into()
                }

                fn to_value(&self) -> <Self as sea_orm::ActiveEnum>::Value {
                    #to_value_body
                }

                fn try_from_value(v: &<Self as sea_orm::ActiveEnum>::Value) -> std::result::Result<Self, sea_orm::DbErr> {
                    match #val {
                        #( #variant_values => Ok(Self::#variant_idents), )*
                        _ => Err(sea_orm::DbErr::Type(format!(
                            "unexpected value for {} enum: {}",
                            stringify!(#ident),
                            #val
                        ))),
                    }
                }

                fn db_type() -> sea_orm::ColumnDef {
                    sea_orm::prelude::ColumnTypeTrait::def(sea_orm::ColumnType::#column_type)
                }
            }
        }
    }

    fn convert_impls(&self) -> TokenStream {
        let ident = &self.ident;

        if self.generate_enum_impls() {
            let enum_name = &self.enum_name;
            let variant_idents = &self.variant_idents;
            let variant_values = &self.variant_values;

            quote! {
                #[automatically_derived]
                impl std::convert::From<#ident> for sea_orm::sea_query::Enum {
                    fn from(source: #ident) -> Self {
                        let value = match source {
                            #( #ident::#variant_idents => #variant_values, )*
                        };
                        Self {
                            type_name: #enum_name.into(),
                            value: value.into(),
                        }
                    }
                }

                #[automatically_derived]
                impl std::convert::From<#ident> for sea_orm::sea_query::Value {
                    fn from(source: #ident) -> Self {
                        let enum_value = sea_orm::sea_query::Enum::from(source);
                        sea_orm::sea_query::Value::from(enum_value)
                    }
                }
            }
        } else {
            quote! {
                #[automatically_derived]
                impl std::convert::From<#ident> for sea_orm::sea_query::Value {
                    fn from(source: #ident) -> Self {
                        <#ident as sea_orm::ActiveEnum>::to_value(&source).into()
                    }
                }
            }
        }
    }

    fn sqlx_postgres_impl(&self) -> TokenStream {
        if !cfg!(feature = "sqlx-postgres") || !self.db_type.is_enum() {
            return quote!();
        }

        let ident = &self.ident;
        let enum_name = &self.enum_name;
        let ident_s = ident.to_string();
        let variant_idents = &self.variant_idents;
        let variant_values = &self.variant_values;

        quote! {
            #[automatically_derived]
            impl sea_orm::sqlx::Type<sea_orm::sqlx::Postgres> for #ident {
                fn type_info() -> sea_orm::sqlx::postgres::PgTypeInfo {
                    sea_orm::sqlx::postgres::PgTypeInfo::with_name(#enum_name)
                }
            }

            #[automatically_derived]
            impl<'r> sea_orm::sqlx::decode::Decode<'r, sea_orm::sqlx::Postgres> for #ident {
                fn decode(
                    value: sea_orm::sqlx::postgres::PgValueRef<'r>,
                ) -> std::result::Result<
                    Self,
                    std::boxed::Box<
                        dyn std::error::Error + 'static + std::marker::Send + std::marker::Sync,
                    >,
                > {
                    let value = <&'r str as sea_orm::sqlx::decode::Decode<
                        'r,
                        sea_orm::sqlx::Postgres,
                    >>::decode(value)?;

                    match value {
                        #( #variant_values => Ok(Self::#variant_idents), )*
                        _ => Err(format!("invalid value {:?} for enum {}", value, #ident_s).into()),
                    }
                }
            }

            #[automatically_derived]
            impl sea_orm::sqlx::postgres::PgHasArrayType for #ident {
                fn array_type_info() -> sea_orm::sqlx::postgres::PgTypeInfo {
                    sea_orm::sqlx::postgres::PgTypeInfo::array_of(#enum_name)
                }
            }
        }
    }

    fn try_getable_array_impl(&self) -> TokenStream {
        let ident = &self.ident;

        if cfg!(feature = "postgres-array") {
            let sqlx_postgres_try_get = if cfg!(feature = "sqlx-postgres") && self.db_type.is_enum()
            {
                quote! {
                    if let Some(result) = res.try_get_from_sqlx_postgres::<Vec<Self>, I>(index) {
                        return result;
                    }
                }
            } else {
                quote!()
            };
            quote!(
                #[automatically_derived]
                impl sea_orm::TryGetableArray for #ident {
                    fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, index: I) -> std::result::Result<Vec<Self>, sea_orm::TryGetError> {
                        #sqlx_postgres_try_get
                        <<Self as sea_orm::ActiveEnum>::Value as sea_orm::ActiveEnumValue>::try_get_vec_by(res, index)?
                            .into_iter()
                            .map(|value| <Self as sea_orm::ActiveEnum>::try_from_value(&value).map_err(Into::into))
                            .collect()
                    }
                }
            )
        } else {
            quote!()
        }
    }

    fn expand(&self) -> TokenStream {
        let Self {
            ident,
            enum_name,
            variants,
            rename_all,
            ..
        } = self;

        let enum_name_iden = format_ident!("{}Enum", ident);

        let str_variants: Vec<String> = variants
            .iter()
            .filter_map(|variant| {
                variant
                    .string_value
                    .as_ref()
                    .map(|string_value| string_value.value())
                    .or(variant
                        .rename
                        .map(|rename| variant.ident.convert_case(Some(rename))))
                    .or_else(|| rename_all.map(|rule| variant.ident.convert_case(Some(rule))))
            })
            .collect();

        let impl_enum_variant_iden = if !str_variants.is_empty() {
            let enum_variant_iden = format_ident!("{}Variant", ident);
            let enum_variants: Vec<syn::Ident> = str_variants
                .iter()
                .map(|v| {
                    let v_cleaned = camel_case_with_escaped_non_uax31(v);

                    format_ident!("{}", v_cleaned)
                })
                .collect();

            quote!(
                #[doc = " Generated by sea-orm-macros"]
                #[derive(Debug, Clone, PartialEq, Eq, sea_orm::EnumIter)]
                pub enum #enum_variant_iden {
                    #(
                        #[doc = " Generated by sea-orm-macros"]
                        #enum_variants,
                    )*
                }

                #[automatically_derived]
                impl sea_orm::Iden for #enum_variant_iden {
                    fn unquoted(&self) -> &str {
                        match self {
                            #(
                                Self::#enum_variants => #str_variants,
                            )*
                        }
                    }
                }

                #[automatically_derived]
                impl #ident {
                    #[doc = " Generated by sea-orm-macros"]
                    pub fn iden_values() -> Vec<sea_orm::sea_query::DynIden> {
                        <#enum_variant_iden as sea_orm::strum::IntoEnumIterator>::iter()
                            // TODO: Use DynIden constructor
                            .map(|v| sea_orm::sea_query::SeaRc::new(v) as sea_orm::sea_query::DynIden)
                            .collect()
                    }
                }
            )
        } else {
            quote!()
        };

        let not_u8_impl = if cfg!(feature = "postgres-array") {
            quote!(
                #[automatically_derived]
                impl sea_orm::sea_query::postgres_array::NotU8 for #ident {}
            )
        } else {
            quote!()
        };

        let value_type_impl = self.value_type_impl();
        let convert_impls = self.convert_impls();
        let nullable_impl = self.nullable_impl();
        let impl_try_getable_array = self.try_getable_array_impl();
        let active_enum_impl = self.active_enum_impl();
        let try_getable_impl = self.try_getable_impl();
        let sqlx_postgres_impl = self.sqlx_postgres_impl();

        quote!(
            #[doc = " Generated by sea-orm-macros"]
            #[derive(Debug, Clone, PartialEq, Eq)]
            pub struct #enum_name_iden;

            #[automatically_derived]
            impl sea_orm::Iden for #enum_name_iden {
                fn unquoted(&self) -> &str {
                    #enum_name
                }
            }

            #impl_enum_variant_iden

            #active_enum_impl

            #impl_try_getable_array

            #convert_impls

            #try_getable_impl

            #sqlx_postgres_impl

            #value_type_impl

            #nullable_impl

            #[automatically_derived]
            impl sea_orm::IntoActiveValue<#ident> for #ident {
                fn into_active_value(self) -> sea_orm::ActiveValue<#ident> {
                    sea_orm::ActiveValue::set(self)
                }
            }

            #not_u8_impl
        )
    }
}

pub fn expand_derive_active_enum(input: syn::DeriveInput) -> syn::Result<TokenStream> {
    let ident_span = input.ident.span();

    match ActiveEnum::new(input) {
        Ok(model) => Ok(model.expand()),
        Err(Error::InputNotEnum) => Ok(quote_spanned! {
            ident_span => compile_error!("you can only derive ActiveEnum on enums");
        }),
        Err(Error::TT(token_stream)) => Ok(token_stream),
        Err(Error::Syn(e)) => Err(e),
    }
}