Skip to content

Commit

Permalink
WIP: Use Existing Case Style Handlers For Enum Rename Rules, Add Unit…
Browse files Browse the repository at this point in the history
… Tests For DeriveActiveEnum rename rules SeaQL#2160
  • Loading branch information
anshap1719 committed Apr 25, 2024
1 parent cc43446 commit 9e085b9
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 190 deletions.
15 changes: 8 additions & 7 deletions sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::util::camel_case_with_escaped_non_uax31;
use crate::derives::enum_variant_rename::RenameRule;
use heck::ToUpperCamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned};
use syn::{parse, Expr, Lit, LitInt, LitStr, UnOp};
use syn::{Expr, Lit, LitInt, LitStr, parse, UnOp};

use crate::strum::helpers::case_style::{CaseStyle, CaseStyleHelpers};

use super::util::camel_case_with_escaped_non_uax31;

enum Error {
InputNotEnum,
Expand All @@ -18,14 +20,14 @@ struct ActiveEnum {
db_type: TokenStream,
is_string: bool,
variants: Vec<ActiveEnumVariant>,
rename_all: Option<RenameRule>,
rename_all: Option<CaseStyle>,
}

struct ActiveEnumVariant {
ident: syn::Ident,
string_value: Option<LitStr>,
num_value: Option<LitInt>,
rename: Option<RenameRule>,
rename: Option<CaseStyle>,
}

impl ActiveEnum {
Expand Down Expand Up @@ -225,8 +227,7 @@ impl ActiveEnum {
} else if let Some(num_value) = &variant.num_value {
quote! { #num_value }
} else if let Some(rename_rule) = variant.rename.or(*rename_all) {
let variant_ident = variant.ident.to_string();
let variant_ident = rename_rule.apply_to_variant(&variant_ident);
let variant_ident = variant.ident.convert_case(Some(rename_rule));

quote! { #variant_ident }
} else {
Expand Down
141 changes: 0 additions & 141 deletions sea-orm-macros/src/derives/enum_variant_rename.rs

This file was deleted.

39 changes: 19 additions & 20 deletions sea-orm-macros/src/derives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
pub use active_enum::*;
pub use active_enum_display::*;
pub use active_model::*;
pub use active_model_behavior::*;
pub use column::*;
pub use derive_iden::*;
pub use entity::*;
pub use entity_model::*;
pub use from_query_result::*;
pub use into_active_model::*;
pub use migration::*;
pub use model::*;
pub use partial_model::*;
pub use primary_key::*;
pub use related_entity::*;
pub use relation::*;
pub use try_getable_from_json::*;
pub use value_type::*;

mod active_enum;
mod active_enum_display;
mod active_model;
Expand All @@ -7,7 +26,6 @@ mod column;
mod derive_iden;
mod entity;
mod entity_model;
mod enum_variant_rename;
mod from_query_result;
mod into_active_model;
mod migration;
Expand All @@ -20,22 +38,3 @@ mod sql_type_match;
mod try_getable_from_json;
mod util;
mod value_type;

pub use active_enum::*;
pub use active_enum_display::*;
pub use active_model::*;
pub use active_model_behavior::*;
pub use column::*;
pub use derive_iden::*;
pub use entity::*;
pub use entity_model::*;
pub use from_query_result::*;
pub use into_active_model::*;
pub use migration::*;
pub use model::*;
pub use partial_model::*;
pub use primary_key::*;
pub use related_entity::*;
pub use relation::*;
pub use try_getable_from_json::*;
pub use value_type::*;
31 changes: 24 additions & 7 deletions sea-orm-macros/src/strum/helpers/case_style.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::str::FromStr;

use heck::{
ToKebabCase, ToLowerCamelCase, ToShoutySnakeCase, ToSnakeCase, ToTitleCase, ToUpperCamelCase,
};
use std::str::FromStr;
use syn::meta::ParseNestedMeta;
use syn::{
parse::{Parse, ParseStream},
Ident, LitStr,
Expand All @@ -24,15 +26,15 @@ pub enum CaseStyle {

const VALID_CASE_STYLES: &[&str] = &[
"camelCase",
"PascalCase",
"kebab-case",
"snake_case",
"mixed_case",
"SCREAMING_SNAKE_CASE",
"SCREAMING-KEBAB-CASE",
"lowercase",
"UPPERCASE",
"snake_case",
"title_case",
"mixed_case",
"UPPERCASE",
"lowercase",
"SCREAMING-KEBAB-CASE",
"PascalCase",
];

impl Parse for CaseStyle {
Expand Down Expand Up @@ -109,6 +111,21 @@ impl CaseStyleHelpers for Ident {
}
}

impl<'meta> TryFrom<&ParseNestedMeta<'meta>> for CaseStyle {
type Error = syn::Error;

fn try_from(value: &ParseNestedMeta) -> Result<Self, Self::Error> {
let meta_string_literal: LitStr = value.value()?.parse()?;
let value_string = meta_string_literal.value();
match CaseStyle::from_str(value_string.as_str()) {
Ok(rule) => Ok(rule),
Err(()) => Err(value.error(format!(
"Unknown value for attribute parameter: `{value_string}`. Valid values are: `{VALID_CASE_STYLES:?}`"
))),
}
}
}

#[test]
fn test_convert_case() {
let id = Ident::new("test_me", proc_macro2::Span::call_site());
Expand Down

0 comments on commit 9e085b9

Please sign in to comment.