Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.
Open
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ readme = "README.md"
[lib]
proc-macro = true

[dependencies]
Inflector = {version = "0.11.4"}

[dev-dependencies]
paste-test-suite = { version = "0", path = "tests/macros" }
rustversion = "1.0"
Expand Down
12 changes: 2 additions & 10 deletions src/segment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::{Error, Result};
use inflector::cases::snakecase::to_snake_case;
use proc_macro::{token_stream, Delimiter, Ident, Span, TokenTree};
use std::iter::Peekable;

Expand Down Expand Up @@ -181,16 +182,7 @@ pub(crate) fn paste(segments: &[Segment]) -> Result<String> {
evaluated.push(last.to_uppercase());
}
"snake" => {
let mut acc = String::new();
let mut prev = '_';
for ch in last.chars() {
if ch.is_uppercase() && prev != '_' {
acc.push('_');
}
acc.push(ch);
prev = ch;
}
evaluated.push(acc.to_lowercase());
evaluated.push(to_snake_case(&last));
}
"camel" => {
let mut acc = String::new();
Expand Down
26 changes: 26 additions & 0 deletions tests/test_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,32 @@ mod test_to_snake {
}
}

mod test_to_snake_acme {
use inflector::cases::screamingsnakecase::to_screaming_snake_case;
use inflector::cases::snakecase::to_snake_case;
use paste::paste;

macro_rules! m {
($id:ident) => {
paste! {
const RAW: &str = stringify!([<$id>]);
const LOWER_SNAKE: &str = stringify!([<$id:snake:lower>]);
const UPPER_SNAKE: &str = stringify!([<$id:snake:upper>]);
}
};
}
m!(ACMECorp);

#[test]
fn test_to_snake_acme() {
let lower_snake_inflector: &str = &to_snake_case(RAW);
let screaming_snake_inflector: &str = &to_screaming_snake_case(RAW);

assert_eq!(LOWER_SNAKE, lower_snake_inflector);
assert_eq!(UPPER_SNAKE, screaming_snake_inflector);
}
}

mod test_to_camel {
use paste::paste;

Expand Down