[cli] added with-copy-enums flag to cli and conditional derive
This commit is contained in:
parent
8a269bf171
commit
5fbc336de0
@ -163,10 +163,22 @@ pub enum GenerateSubcommands {
|
|||||||
value_parser,
|
value_parser,
|
||||||
long,
|
long,
|
||||||
default_value = "none",
|
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,
|
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(
|
#[clap(
|
||||||
arg_enum,
|
arg_enum,
|
||||||
value_parser,
|
value_parser,
|
||||||
|
@ -24,6 +24,7 @@ pub async fn run_generate_command(
|
|||||||
database_schema,
|
database_schema,
|
||||||
database_url,
|
database_url,
|
||||||
with_serde,
|
with_serde,
|
||||||
|
with_copy_enums,
|
||||||
date_time_crate,
|
date_time_crate,
|
||||||
} => {
|
} => {
|
||||||
if verbose {
|
if verbose {
|
||||||
@ -167,6 +168,7 @@ pub async fn run_generate_command(
|
|||||||
let writer_context = EntityWriterContext::new(
|
let writer_context = EntityWriterContext::new(
|
||||||
expanded_format,
|
expanded_format,
|
||||||
WithSerde::from_str(&with_serde).unwrap(),
|
WithSerde::from_str(&with_serde).unwrap(),
|
||||||
|
with_copy_enums,
|
||||||
date_time_crate.into(),
|
date_time_crate.into(),
|
||||||
schema_name,
|
schema_name,
|
||||||
);
|
);
|
||||||
|
@ -10,7 +10,7 @@ pub struct ActiveEnum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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_name = &self.enum_name;
|
||||||
let enum_iden = format_ident!("{}", enum_name.to_camel_case());
|
let enum_iden = format_ident!("{}", enum_name.to_camel_case());
|
||||||
let values = &self.values;
|
let values = &self.values;
|
||||||
@ -23,9 +23,14 @@ impl ActiveEnum {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let extra_derive = with_serde.extra_derive();
|
let extra_derive = with_serde.extra_derive();
|
||||||
|
let copy_derive = if with_copy_enums {
|
||||||
|
quote! { , Copy }
|
||||||
|
} else {
|
||||||
|
quote! {}
|
||||||
|
};
|
||||||
|
|
||||||
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)]
|
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = #enum_name)]
|
||||||
pub enum #enum_iden {
|
pub enum #enum_iden {
|
||||||
#(
|
#(
|
||||||
|
@ -39,6 +39,7 @@ pub enum DateTimeCrate {
|
|||||||
pub struct EntityWriterContext {
|
pub struct EntityWriterContext {
|
||||||
pub(crate) expanded_format: bool,
|
pub(crate) expanded_format: bool,
|
||||||
pub(crate) with_serde: WithSerde,
|
pub(crate) with_serde: WithSerde,
|
||||||
|
pub(crate) with_copy_enums: bool,
|
||||||
pub(crate) date_time_crate: DateTimeCrate,
|
pub(crate) date_time_crate: DateTimeCrate,
|
||||||
pub(crate) schema_name: Option<String>,
|
pub(crate) schema_name: Option<String>,
|
||||||
}
|
}
|
||||||
@ -97,12 +98,14 @@ impl EntityWriterContext {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
expanded_format: bool,
|
expanded_format: bool,
|
||||||
with_serde: WithSerde,
|
with_serde: WithSerde,
|
||||||
|
with_copy_enums: bool,
|
||||||
date_time_crate: DateTimeCrate,
|
date_time_crate: DateTimeCrate,
|
||||||
schema_name: Option<String>,
|
schema_name: Option<String>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
expanded_format,
|
expanded_format,
|
||||||
with_serde,
|
with_serde,
|
||||||
|
with_copy_enums,
|
||||||
date_time_crate,
|
date_time_crate,
|
||||||
schema_name,
|
schema_name,
|
||||||
}
|
}
|
||||||
@ -116,7 +119,9 @@ impl EntityWriter {
|
|||||||
files.push(self.write_mod());
|
files.push(self.write_mod());
|
||||||
files.push(self.write_prelude());
|
files.push(self.write_prelude());
|
||||||
if !self.enums.is_empty() {
|
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 }
|
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();
|
let mut lines = Vec::new();
|
||||||
Self::write_doc_comment(&mut lines);
|
Self::write_doc_comment(&mut lines);
|
||||||
Self::write(&mut lines, vec![Self::gen_import(with_serde)]);
|
Self::write(&mut lines, vec![Self::gen_import(with_serde)]);
|
||||||
@ -208,7 +217,7 @@ impl EntityWriter {
|
|||||||
let code_blocks = self
|
let code_blocks = self
|
||||||
.enums
|
.enums
|
||||||
.iter()
|
.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();
|
.collect();
|
||||||
Self::write(&mut lines, code_blocks);
|
Self::write(&mut lines, code_blocks);
|
||||||
OutputFile {
|
OutputFile {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user