Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Copy to the derived traits for active enums #634

Merged
merged 8 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub async fn run_generate_command(
database_schema,
database_url,
with_serde,
with_copy_enums,
date_time_crate,
} => {
if verbose {
Expand Down Expand Up @@ -169,6 +170,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,
);
Expand Down
9 changes: 7 additions & 2 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
#(
Expand Down
15 changes: 12 additions & 3 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}
Expand Down Expand Up @@ -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<String>,
) -> Self {
Self {
expanded_format,
with_serde,
with_copy_enums,
date_time_crate,
schema_name,
}
Expand All @@ -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 }
}
Expand Down Expand Up @@ -200,15 +205,19 @@ 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)]);
lines.push("".to_owned());
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 {
Expand Down