feat: add serde derives to enums, if specified
This commit is contained in:
parent
2e038a7eae
commit
2a2b5d7b62
@ -1,3 +1,4 @@
|
|||||||
|
use crate::WithSerde;
|
||||||
use heck::CamelCase;
|
use heck::CamelCase;
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
@ -9,7 +10,7 @@ pub struct ActiveEnum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ActiveEnum {
|
impl ActiveEnum {
|
||||||
pub fn impl_active_enum(&self) -> TokenStream {
|
pub fn impl_active_enum(&self, with_serde: &WithSerde) -> 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;
|
||||||
@ -17,8 +18,11 @@ impl ActiveEnum {
|
|||||||
.values
|
.values
|
||||||
.iter()
|
.iter()
|
||||||
.map(|v| format_ident!("{}", v.to_camel_case()));
|
.map(|v| format_ident!("{}", v.to_camel_case()));
|
||||||
|
|
||||||
|
let extra_derive = with_serde.extra_derive();
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
|
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum #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 {
|
||||||
#(
|
#(
|
||||||
|
@ -81,25 +81,25 @@ impl FromStr for WithSerde {
|
|||||||
impl EntityWriter {
|
impl EntityWriter {
|
||||||
pub fn generate(self, expanded_format: bool, with_serde: WithSerde) -> WriterOutput {
|
pub fn generate(self, expanded_format: bool, with_serde: WithSerde) -> WriterOutput {
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
files.extend(self.write_entities(expanded_format, with_serde));
|
files.extend(self.write_entities(expanded_format, &with_serde));
|
||||||
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());
|
files.push(self.write_sea_orm_active_enums(&with_serde));
|
||||||
}
|
}
|
||||||
WriterOutput { files }
|
WriterOutput { files }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_entities(&self, expanded_format: bool, with_serde: WithSerde) -> Vec<OutputFile> {
|
pub fn write_entities(&self, expanded_format: bool, with_serde: &WithSerde) -> Vec<OutputFile> {
|
||||||
self.entities
|
self.entities
|
||||||
.iter()
|
.iter()
|
||||||
.map(|entity| {
|
.map(|entity| {
|
||||||
let mut lines = Vec::new();
|
let mut lines = Vec::new();
|
||||||
Self::write_doc_comment(&mut lines);
|
Self::write_doc_comment(&mut lines);
|
||||||
let code_blocks = if expanded_format {
|
let code_blocks = if expanded_format {
|
||||||
Self::gen_expanded_code_blocks(entity, &with_serde)
|
Self::gen_expanded_code_blocks(entity, with_serde)
|
||||||
} else {
|
} else {
|
||||||
Self::gen_compact_code_blocks(entity, &with_serde)
|
Self::gen_compact_code_blocks(entity, with_serde)
|
||||||
};
|
};
|
||||||
Self::write(&mut lines, code_blocks);
|
Self::write(&mut lines, code_blocks);
|
||||||
OutputFile {
|
OutputFile {
|
||||||
@ -147,20 +147,18 @@ impl EntityWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_sea_orm_active_enums(&self) -> OutputFile {
|
pub fn write_sea_orm_active_enums(&self, with_serde: &WithSerde) -> 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(
|
Self::write(
|
||||||
&mut lines,
|
&mut lines,
|
||||||
vec![quote! {
|
vec![Self::gen_import(with_serde)],
|
||||||
use sea_orm::entity::prelude::*;
|
|
||||||
}],
|
|
||||||
);
|
);
|
||||||
lines.push("".to_owned());
|
lines.push("".to_owned());
|
||||||
let code_blocks = self
|
let code_blocks = self
|
||||||
.enums
|
.enums
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, active_enum)| active_enum.impl_active_enum())
|
.map(|(_, active_enum)| active_enum.impl_active_enum(with_serde))
|
||||||
.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