From 2ad04c3cb9f1e9937f642299ddb1ce97ba33cb4b Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Tue, 24 Oct 2023 23:37:13 +0200 Subject: [PATCH 1/4] feat: Support `--enum-extra-derives` --- sea-orm-cli/src/cli.rs | 7 ++++++ sea-orm-cli/src/commands/generate.rs | 2 ++ sea-orm-codegen/src/entity/active_enum.rs | 13 +++++++--- sea-orm-codegen/src/entity/writer.rs | 30 ++++++++++++++--------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index ff88d30a4..cc92208c6 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -280,6 +280,13 @@ pub enum GenerateSubcommands { )] model_extra_attributes: Vec, + #[arg( + long, + value_delimiter = ',', + help = "Add extra derive macros to generated enums (comma separated), e.g. `--enum-extra-derives 'ts_rs::Ts','CustomDerive'`" + )] + enum_extra_derives: Vec, + #[arg( long, default_value = "false", diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index 7b7a89562..8e8994fc0 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -31,6 +31,7 @@ pub async fn run_generate_command( lib, model_extra_derives, model_extra_attributes, + enum_extra_derives, seaography, } => { if verbose { @@ -180,6 +181,7 @@ pub async fn run_generate_command( serde_skip_hidden_column, model_extra_derives, model_extra_attributes, + enum_extra_derives, seaography, ); let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context); diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index 30c9c5df6..a8cccc6fc 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -12,7 +12,12 @@ pub struct ActiveEnum { } impl ActiveEnum { - pub fn impl_active_enum(&self, with_serde: &WithSerde, with_copy_enums: bool) -> TokenStream { + pub fn impl_active_enum( + &self, + with_serde: &WithSerde, + with_copy_enums: bool, + extra_derives: &TokenStream, + ) -> TokenStream { let enum_name = &self.enum_name.to_string(); let enum_iden = format_ident!("{}", enum_name.to_upper_camel_case()); let values: Vec = self.values.iter().map(|v| v.to_string()).collect(); @@ -24,7 +29,7 @@ impl ActiveEnum { } }); - let extra_derive = with_serde.extra_derive(); + let serde_derive = with_serde.extra_derive(); let copy_derive = if with_copy_enums { quote! { , Copy } } else { @@ -32,7 +37,7 @@ impl ActiveEnum { }; quote! { - #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum #copy_derive #extra_derive)] + #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum #copy_derive #serde_derive #extra_derives)] #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = #enum_name)] pub enum #enum_iden { #( @@ -72,7 +77,7 @@ mod tests { .map(|variant| Alias::new(variant).into_iden()) .collect(), } - .impl_active_enum(&WithSerde::None, true) + .impl_active_enum(&WithSerde::None, true, "e! {}) .to_string(), quote!( #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy)] diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index dc60654fa..e7b46838c 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -47,6 +47,7 @@ pub struct EntityWriterContext { pub(crate) serde_skip_deserializing_primary_key: bool, pub(crate) model_extra_derives: TokenStream, pub(crate) model_extra_attributes: TokenStream, + pub(crate) enum_extra_derives: TokenStream, pub(crate) seaography: bool, } @@ -79,19 +80,19 @@ impl WithSerde { } } -/// Converts model_extra_derives argument to token stream -fn bonus_derive(model_extra_derives: I) -> TokenStream +/// Converts *_extra_derives argument to token stream +fn bonus_derive(extra_derives: I) -> TokenStream where T: Into, I: IntoIterator, { - model_extra_derives - .into_iter() - .map(Into::::into) - .fold(TokenStream::default(), |acc, derive| { + extra_derives.into_iter().map(Into::::into).fold( + TokenStream::default(), + |acc, derive| { let tokens: TokenStream = derive.parse().unwrap(); quote! { #acc, #tokens } - }) + }, + ) } /// convert attributes argument to token stream @@ -143,6 +144,7 @@ impl EntityWriterContext { serde_skip_hidden_column: bool, model_extra_derives: Vec, model_extra_attributes: Vec, + enum_extra_derives: Vec, seaography: bool, ) -> Self { Self { @@ -156,6 +158,7 @@ impl EntityWriterContext { serde_skip_hidden_column, model_extra_derives: bonus_derive(model_extra_derives), model_extra_attributes: bonus_attributes(model_extra_attributes), + enum_extra_derives: bonus_derive(enum_extra_derives), seaography, } } @@ -168,9 +171,11 @@ impl EntityWriter { files.push(self.write_index_file(context.lib)); files.push(self.write_prelude()); if !self.enums.is_empty() { - files.push( - self.write_sea_orm_active_enums(&context.with_serde, context.with_copy_enums), - ); + files.push(self.write_sea_orm_active_enums( + &context.with_serde, + context.with_copy_enums, + &context.enum_extra_derives, + )); } WriterOutput { files } } @@ -283,6 +288,7 @@ impl EntityWriter { &self, with_serde: &WithSerde, with_copy_enums: bool, + extra_derives: &TokenStream, ) -> OutputFile { let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); @@ -291,7 +297,9 @@ impl EntityWriter { let code_blocks = self .enums .values() - .map(|active_enum| active_enum.impl_active_enum(with_serde, with_copy_enums)) + .map(|active_enum| { + active_enum.impl_active_enum(with_serde, with_copy_enums, extra_derives) + }) .collect(); Self::write(&mut lines, code_blocks); OutputFile { From d396d82aaf93747711573d6c1e1bb180afb45a98 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Tue, 24 Oct 2023 23:45:24 +0200 Subject: [PATCH 2/4] test: Enum extra derives --- sea-orm-codegen/src/entity/active_enum.rs | 32 +++++++++++++++++++++++ sea-orm-codegen/src/entity/writer.rs | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index a8cccc6fc..a7e7b42af 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -51,6 +51,8 @@ impl ActiveEnum { #[cfg(test)] mod tests { + use crate::entity::writer::bonus_derive; + use super::*; use pretty_assertions::assert_eq; use sea_query::{Alias, IntoIden}; @@ -110,4 +112,34 @@ mod tests { .to_string() ) } + + #[test] + fn test_enum_extra_derives() { + assert_eq!( + ActiveEnum { + enum_name: Alias::new("media_type").into_iden(), + values: vec!["UNKNOWN", "BITMAP",] + .into_iter() + .map(|variant| Alias::new(variant).into_iden()) + .collect(), + } + .impl_active_enum( + &WithSerde::None, + true, + &bonus_derive(["specta::Type", "ts_rs::TS"]) + ) + .to_string(), + quote!( + #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy, specta :: Type, ts_rs :: TS)] + #[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, + } + ) + .to_string() + ) + } } diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index e7b46838c..71ffdf94f 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -81,7 +81,7 @@ impl WithSerde { } /// Converts *_extra_derives argument to token stream -fn bonus_derive(extra_derives: I) -> TokenStream +pub(crate) fn bonus_derive(extra_derives: I) -> TokenStream where T: Into, I: IntoIterator, From 761611f92ef27b4babca4dafd984a1d40254ed3b Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Tue, 24 Oct 2023 23:46:42 +0200 Subject: [PATCH 3/4] style: Don't format derives --- sea-orm-codegen/src/entity/active_enum.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index a7e7b42af..f476d79be 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -130,6 +130,7 @@ mod tests { ) .to_string(), quote!( + #[rustfmt::skip] #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy, specta :: Type, ts_rs :: TS)] #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "media_type")] pub enum MediaType { From c66b2f19e584f8e8df815be97df3df9482798e77 Mon Sep 17 00:00:00 2001 From: Lev Khoroshansky Date: Wed, 8 Nov 2023 19:00:28 +0100 Subject: [PATCH 4/4] fix: Put `quote!` inside a function with `#[rustfmt::skip]` --- sea-orm-codegen/src/entity/active_enum.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index f476d79be..95fd0a03b 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -129,8 +129,12 @@ mod tests { &bonus_derive(["specta::Type", "ts_rs::TS"]) ) .to_string(), + build_generated_enum(), + ); + + #[rustfmt::skip] + fn build_generated_enum() -> String { quote!( - #[rustfmt::skip] #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy, specta :: Type, ts_rs :: TS)] #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "media_type")] pub enum MediaType { @@ -141,6 +145,6 @@ mod tests { } ) .to_string() - ) + } } }