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

Codegen add serde derives to enums, if specified #463

Merged
merged 1 commit into from
Mar 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
8 changes: 6 additions & 2 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::WithSerde;
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
Expand All @@ -9,16 +10,19 @@ pub struct 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_iden = format_ident!("{}", enum_name.to_camel_case());
let values = &self.values;
let variants = self
.values
.iter()
.map(|v| format_ident!("{}", v.to_camel_case()));

let extra_derive = with_serde.extra_derive();

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)]
pub enum #enum_iden {
#(
Expand Down
18 changes: 8 additions & 10 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ impl FromStr for WithSerde {
impl EntityWriter {
pub fn generate(self, expanded_format: bool, with_serde: WithSerde) -> WriterOutput {
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_prelude());
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 }
}

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
.iter()
.map(|entity| {
let mut lines = Vec::new();
Self::write_doc_comment(&mut lines);
let code_blocks = if expanded_format {
Self::gen_expanded_code_blocks(entity, &with_serde)
Self::gen_expanded_code_blocks(entity, with_serde)
} else {
Self::gen_compact_code_blocks(entity, &with_serde)
Self::gen_compact_code_blocks(entity, with_serde)
};
Self::write(&mut lines, code_blocks);
OutputFile {
Expand Down Expand Up @@ -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();
Self::write_doc_comment(&mut lines);
Self::write(
&mut lines,
vec![quote! {
use sea_orm::entity::prelude::*;
}],
vec![Self::gen_import(with_serde)],
);
lines.push("".to_owned());
let code_blocks = self
.enums
.iter()
.map(|(_, active_enum)| active_enum.impl_active_enum())
.map(|(_, active_enum)| active_enum.impl_active_enum(with_serde))
.collect();
Self::write(&mut lines, code_blocks);
OutputFile {
Expand Down