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

feat: support more case options #41

Merged
merged 2 commits into from
Oct 29, 2024
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ proc-macro = true
proc-macro2 = "1.0.82"
quote = "1.0.36"
syn = "2.0.63"
convert_case = "0.6.0"

[dev-dependencies]
serde = { version = "1.0.202", features = ["derive"] }
Expand Down
34 changes: 22 additions & 12 deletions src/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use convert_case::Casing;
use proc_macro2::{Ident, TokenStream};
use syn::{DeriveInput, Meta};

Expand All @@ -11,10 +12,7 @@ fn parse_string(s: &str) -> Result<String, ()> {
}
}

pub(crate) enum Case {
Lower,
Upper,
}
pub(crate) struct Case(convert_case::Case);

impl TryFrom<(String, String)> for Case {
type Error = ();
Expand All @@ -32,11 +30,26 @@ impl TryFrom<String> for Case {
type Error = ();

fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(match value.as_str() {
"\"lower\"" => Self::Lower,
"\"upper\"" => Self::Upper,
Ok(Self(match value.as_str() {
"\"upper\"" => convert_case::Case::Upper,
"\"lower\"" => convert_case::Case::Lower,
"\"title\"" => convert_case::Case::Title,
"\"toggle\"" => convert_case::Case::Toggle,
"\"camel\"" => convert_case::Case::Camel,
"\"pascal\"" => convert_case::Case::Pascal,
"\"upper_camel\"" => convert_case::Case::UpperCamel,
"\"snake\"" => convert_case::Case::Snake,
"\"upper_snake\"" => convert_case::Case::UpperSnake,
"\"screaming_snake\"" => convert_case::Case::ScreamingSnake,
"\"kebab\"" => convert_case::Case::Kebab,
"\"cobol\"" => convert_case::Case::Cobol,
"\"upper_kebab\"" => convert_case::Case::UpperKebab,
"\"train\"" => convert_case::Case::Train,
"\"flat\"" => convert_case::Case::Flat,
"\"upper_flat\"" => convert_case::Case::UpperFlat,
"\"alternating\"" => convert_case::Case::Alternating,
_ => Err(())?,
})
}))
}
}

Expand Down Expand Up @@ -235,10 +248,7 @@ impl Attributes {
}

if let Some(case) = &self.case {
new_name = match case {
Case::Lower => new_name.to_lowercase(),
Case::Upper => new_name.to_uppercase(),
};
new_name = new_name.to_case(case.0);
}

new_names.push(new_name);
Expand Down
12 changes: 6 additions & 6 deletions tests/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,44 @@ fn test_prefix_suffix_from_str() {
// Testing commutativity of prefix, suffix and case

#[derive(Debug, PartialEq, enum_stringify::EnumStringify)]
#[enum_stringify(suffix = "Suff", prefix = "Pref", case = "lower")]
#[enum_stringify(suffix = "Suff", prefix = "Pref", case = "flat")]
enum Number4 {
Zero,
One,
Two,
}

#[test]
fn test_suffix_prefix_lower_to_string() {
fn test_suffix_prefix_flat_to_string() {
assert_eq!(Number4::Zero.to_string(), "prefzerosuff");
assert_eq!(Number4::One.to_string(), "prefonesuff");
assert_eq!(Number4::Two.to_string(), "preftwosuff");
}

#[test]
fn test_suffix_prefix_lower_from_str() {
fn test_suffix_prefix_flat_from_str() {
assert_eq!(Number4::from_str("prefzerosuff"), Ok(Number4::Zero));
assert_eq!(Number4::from_str("prefonesuff"), Ok(Number4::One));
assert_eq!(Number4::from_str("preftwosuff"), Ok(Number4::Two));
}

#[derive(Debug, PartialEq, enum_stringify::EnumStringify)]
#[enum_stringify(suffix = "Suff", prefix = "Pref", case = "upper")]
#[enum_stringify(suffix = "Suff", prefix = "Pref", case = "upper_flat")]
enum Number5 {
Zero,
One,
Two,
}

#[test]
fn test_suffix_prefix_upper_to_string() {
fn test_suffix_prefix_upper_flat_to_string() {
assert_eq!(Number5::Zero.to_string(), "PREFZEROSUFF");
assert_eq!(Number5::One.to_string(), "PREFONESUFF");
assert_eq!(Number5::Two.to_string(), "PREFTWOSUFF");
}

#[test]
fn test_suffix_prefix_upper_from_str() {
fn test_suffix_prefix_upper_flat_from_str() {
assert_eq!(Number5::from_str("PREFZEROSUFF"), Ok(Number5::Zero));
assert_eq!(Number5::from_str("PREFONESUFF"), Ok(Number5::One));
assert_eq!(Number5::from_str("PREFTWOSUFF"), Ok(Number5::Two));
Expand Down
Loading