Skip to content
Draft
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
23 changes: 23 additions & 0 deletions pastey-test-suite/tests/test_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ mod test_to_snake {
}
}

mod test_to_snake_number {
use pastey::paste;

macro_rules! m {
($id:ident) => {
paste! {
const DEFAULT_SNAKE_NUMBER: &str = stringify!([<$id:snake_number>]);
const LOWER_SNAKE_NUMBER: &str = stringify!([<$id:snake_number:lower>]);
const UPPER_SNAKE_NUMBER: &str = stringify!([<$id:snake_number:upper>]);
}
};
}

m!(ThisIsButA1Test);

#[test]
fn test_to_snake_number() {
assert_eq!(DEFAULT_SNAKE_NUMBER, "this_is_but_a_1_test");
assert_eq!(LOWER_SNAKE_NUMBER, "this_is_but_a_1_test");
assert_eq!(UPPER_SNAKE_NUMBER, "THIS_IS_BUT_A_1_TEST");
}
}
Comment on lines +156 to +164

mod test_to_camel {
use pastey::paste;

Expand Down
7 changes: 5 additions & 2 deletions src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ pub(crate) fn paste(segments: &[Segment]) -> Result<String> {
"upper" => {
evaluated.push(last.to_uppercase());
}
"snake" => {
"snake" | "snake_number" => {
let mut acc = String::new();
let mut prev = '_';
let include_numbers = ident.to_string().as_str() == "snake_number";
for ch in last.chars() {
if ch.is_uppercase() && prev != '_' {
let should_trigger_underscore =
ch.is_uppercase() || (include_numbers && ch.is_numeric());
Comment on lines +216 to +217
if should_trigger_underscore && prev != '_' {
acc.push('_');
}
acc.push(ch);
Expand Down
Loading