From 1be6673f0e2325f6158415ebb897245e2bfc9636 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 15 Nov 2022 10:56:25 +0800 Subject: [PATCH] Fix DeriveActiveEnum expand enum variant starts with number (#1219) --- sea-orm-macros/src/derives/active_enum.rs | 8 +++++- tests/common/features/sea_orm_active_enums.rs | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/sea-orm-macros/src/derives/active_enum.rs b/sea-orm-macros/src/derives/active_enum.rs index 5e6b27f0..b219b06e 100644 --- a/sea-orm-macros/src/derives/active_enum.rs +++ b/sea-orm-macros/src/derives/active_enum.rs @@ -246,7 +246,13 @@ impl ActiveEnum { let enum_variant_iden = format_ident!("{}Variant", ident); let enum_variants: Vec = str_variants .iter() - .map(|v| format_ident!("{}", v.to_camel_case())) + .map(|v| { + if v.chars().next().map(char::is_numeric).unwrap_or(false) { + format_ident!("_{}", v) + } else { + format_ident!("{}", v.to_camel_case()) + } + }) .collect(); quote!( diff --git a/tests/common/features/sea_orm_active_enums.rs b/tests/common/features/sea_orm_active_enums.rs index 57ba0551..94289dfc 100644 --- a/tests/common/features/sea_orm_active_enums.rs +++ b/tests/common/features/sea_orm_active_enums.rs @@ -26,3 +26,30 @@ pub enum Tea { #[sea_orm(string_value = "BreakfastTea")] BreakfastTea, } + +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy)] +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "media_type")] +pub enum MediaType { + #[sea_orm(string_value = "UNKNOWN")] + Unknown, + #[sea_orm(string_value = "BITMAP")] + Bitmap, + #[sea_orm(string_value = "DRAWING")] + Drawing, + #[sea_orm(string_value = "AUDIO")] + Audio, + #[sea_orm(string_value = "VIDEO")] + Video, + #[sea_orm(string_value = "MULTIMEDIA")] + Multimedia, + #[sea_orm(string_value = "OFFICE")] + Office, + #[sea_orm(string_value = "TEXT")] + Text, + #[sea_orm(string_value = "EXECUTABLE")] + Executable, + #[sea_orm(string_value = "ARCHIVE")] + Archive, + #[sea_orm(string_value = "3D")] + _3D, +}