Skip to content

Commit

Permalink
Merge pull request #936 from SeaQL/pulls/634
Browse files Browse the repository at this point in the history
[cli] added with-copy-enums flag to cli and conditional derive
  • Loading branch information
tyt2y3 authored Aug 6, 2022
2 parents 8a269bf + 5fbc336 commit 2b0ac51
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
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/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -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,
);
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

0 comments on commit 2b0ac51

Please sign in to comment.