diff --git a/impl/src/lib.rs b/impl/src/lib.rs index b2c1e41..b62929d 100644 --- a/impl/src/lib.rs +++ b/impl/src/lib.rs @@ -194,6 +194,26 @@ fn paste_segments(span: Span, segments: &[Segment]) -> Result { prev = ch; } evaluated.push(acc.to_lowercase()); + } else if ident == "camel" { + let mut acc = String::new(); + let mut prev = '_'; + for ch in last.chars() { + if ch != '_' { + if prev == '_' { + for chu in ch.to_uppercase() { + acc.push(chu); + } + } else if prev.is_uppercase() { + for chl in ch.to_lowercase() { + acc.push(chl); + } + } else { + acc.push(ch); + } + } + prev = ch; + } + evaluated.push(acc); } else { return Err(Error::new_spanned(span, "unsupported modifier")); } diff --git a/src/lib.rs b/src/lib.rs index 8125222..6d73ee3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,8 +117,9 @@ //! example, `[]` would paste to `ld_bc_expr` if invoked //! with $reg=`Bc`. //! -//! Use `$var:snake` to convert CamelCase input to snake\_case. These compose, -//! so for example `$var:snake:upper` would give you SCREAMING\_CASE. +//! Use `$var:snake` to convert CamelCase input to snake\_case. +//! Use `$var:camel` to convert snake\_case to CamelCase. +//! These compose, so for example `$var:snake:upper` would give you SCREAMING\_CASE. //! //! The precise Unicode conversions are as defined by [`str::to_lowercase`] and //! [`str::to_uppercase`]. diff --git a/tests/test.rs b/tests/test.rs index 435eb71..f81b6d4 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -255,3 +255,33 @@ fn test_env_to_snake() { let _ = LIBPASTE; } } + +mod test_to_camel { + macro_rules! m { + ($id:ident) => { + paste::item! { + const DEFAULT_CAMEL: &str = stringify!([<$id:camel>]); + const LOWER_CAMEL: &str = stringify!([<$id:camel:lower>]); + const UPPER_CAMEL: &str = stringify!([<$id:camel:upper>]); + } + }; + } + + m!(this_is_but_a_test); + + #[test] + fn test_to_snake() { + assert_eq!(DEFAULT_CAMEL, "ThisIsButATest"); + assert_eq!(LOWER_CAMEL, "thisisbutatest"); + assert_eq!(UPPER_CAMEL, "THISISBUTATEST"); + } +} + +#[test] +fn test_env_to_camel() { + paste::expr! { + const []: &str = "libpaste"; + + let _ = LIBPaste; + } +}