Skip to content

Commit

Permalink
feat: support more case options (#41)
Browse files Browse the repository at this point in the history
Hey, nice library! I was just about to write my own stringifier, but
then I discovered this existed. Unfortunately, it doesn't support
_everything_ I needed. Specifically, the case conversion. So I added
some more. I was originally going to write them by hand, but then I
discovered that [this](https://github.com/rutrum/convert-case) exists,
so I just based it on that.

One downside of doing it like this is what was formerly "lower" and
"upper" are now "flat" and "upper_flat". Let me know if you don't like
this or whatever.
  • Loading branch information
Yag000 authored Oct 29, 2024
2 parents b33353e + 48a0eeb commit e02f802
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
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

0 comments on commit e02f802

Please sign in to comment.