Skip to content

Commit

Permalink
WIP: Improve Implementation Of Name Case Parameters In ActiveEnum Mac…
Browse files Browse the repository at this point in the history
…ros SeaQL#2160
  • Loading branch information
anshap1719 committed Mar 22, 2024
1 parent 539e4dc commit 8acc709
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl ActiveEnum {
.map_err(Error::Syn)?;
}

if is_string && is_int {
if (is_string || rename_rule.is_some() || rename_all_rule.is_some()) && is_int {
return Err(Error::TT(quote_spanned! {
ident_span => compile_error!("All enum variants should specify the same `*_value` macro attribute, either `string_value` or `num_value` but not both");
}));
Expand Down Expand Up @@ -232,7 +232,7 @@ impl ActiveEnum {
quote! { #variant_ident }
} else {
quote_spanned! {
variant_span => compile_error!("Missing macro attribute, either `string_value` or `num_value` should be specified");
variant_span => compile_error!("Missing macro attribute, either `string_value`, `rename` or `num_value` should be specified");
}
}
})
Expand All @@ -253,6 +253,7 @@ impl ActiveEnum {
.string_value
.as_ref()
.map(|string_value| string_value.value())
.or(variant.rename.map(|rename| variant.ident.convert_case(Some(rename))))
})
.collect();

Expand Down
5 changes: 5 additions & 0 deletions sea-orm-macros/src/derives/active_enum_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use syn::{LitInt, LitStr};

use crate::strum::helpers::case_style::CaseStyle;

enum Error {
InputNotEnum,
Syn(syn::Error),
Expand Down Expand Up @@ -29,6 +31,7 @@ impl Display {
let mut variants = Vec::new();
for variant in variant_vec {
let mut display_value = variant.ident.to_string().to_token_stream();

for attr in variant.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
continue;
Expand All @@ -40,6 +43,8 @@ impl Display {
meta.value()?.parse::<LitInt>()?;
} else if meta.path.is_ident("display_value") {
display_value = meta.value()?.parse::<LitStr>()?.to_token_stream();
} else if meta.path.is_ident("rename") {
CaseStyle::try_from(&meta)?;
} else {
return Err(meta.error(format!(
"Unknown attribute parameter found: {:?}",
Expand Down

0 comments on commit 8acc709

Please sign in to comment.