diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index 5b302396d..d5bb4ef28 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -163,10 +163,22 @@ pub enum GenerateSubcommands { value_parser, long, default_value = "none", - help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)" + help = "Automatically derive serde Serialize / Deserialize traits for the entity (none,\ + serialize, deserialize, both)" )] with_serde: String, + #[clap( + action, + long, + default_value = "false", + long_help = "Automatically derive the Copy trait on generated enums.\n\ + Enums generated from a database don't have associated data by default, and as such can\ + derive Copy. + " + )] + with_copy_enums: bool, + #[clap( arg_enum, value_parser, diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index 0ff95f410..0cea31e44 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -24,6 +24,7 @@ pub async fn run_generate_command( database_schema, database_url, with_serde, + with_copy_enums, date_time_crate, } => { if verbose { @@ -167,6 +168,7 @@ pub async fn run_generate_command( let writer_context = EntityWriterContext::new( expanded_format, WithSerde::from_str(&with_serde).unwrap(), + with_copy_enums, date_time_crate.into(), schema_name, ); diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index f5567b754..d4a7e6003 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -10,7 +10,7 @@ pub struct ActiveEnum { } impl ActiveEnum { - pub fn impl_active_enum(&self, with_serde: &WithSerde) -> TokenStream { + pub fn impl_active_enum(&self, with_serde: &WithSerde, with_copy_enums: bool) -> TokenStream { let enum_name = &self.enum_name; let enum_iden = format_ident!("{}", enum_name.to_camel_case()); let values = &self.values; @@ -23,9 +23,14 @@ impl ActiveEnum { }); let extra_derive = with_serde.extra_derive(); + let copy_derive = if with_copy_enums { + quote! { , Copy } + } else { + quote! {} + }; quote! { - #[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum #extra_derive)] + #[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum #copy_derive #extra_derive)] #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = #enum_name)] pub enum #enum_iden { #( diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 446869fb7..8227fa482 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -39,6 +39,7 @@ pub enum DateTimeCrate { pub struct EntityWriterContext { pub(crate) expanded_format: bool, pub(crate) with_serde: WithSerde, + pub(crate) with_copy_enums: bool, pub(crate) date_time_crate: DateTimeCrate, pub(crate) schema_name: Option, } @@ -97,12 +98,14 @@ impl EntityWriterContext { pub fn new( expanded_format: bool, with_serde: WithSerde, + with_copy_enums: bool, date_time_crate: DateTimeCrate, schema_name: Option, ) -> Self { Self { expanded_format, with_serde, + with_copy_enums, date_time_crate, schema_name, } @@ -116,7 +119,9 @@ impl EntityWriter { files.push(self.write_mod()); files.push(self.write_prelude()); if !self.enums.is_empty() { - files.push(self.write_sea_orm_active_enums(&context.with_serde)); + files.push( + self.write_sea_orm_active_enums(&context.with_serde, context.with_copy_enums), + ); } WriterOutput { files } } @@ -200,7 +205,11 @@ impl EntityWriter { } } - pub fn write_sea_orm_active_enums(&self, with_serde: &WithSerde) -> OutputFile { + pub fn write_sea_orm_active_enums( + &self, + with_serde: &WithSerde, + with_copy_enums: bool, + ) -> OutputFile { let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); Self::write(&mut lines, vec![Self::gen_import(with_serde)]); @@ -208,7 +217,7 @@ impl EntityWriter { let code_blocks = self .enums .iter() - .map(|(_, active_enum)| active_enum.impl_active_enum(with_serde)) + .map(|(_, active_enum)| active_enum.impl_active_enum(with_serde, with_copy_enums)) .collect(); Self::write(&mut lines, code_blocks); OutputFile {