1use core::fmt;
2
3use alloc::{
4 borrow::ToOwned,
5 string::{String, ToString},
6};
7
8use crate::{capitalize, lowercase, transform};
9
10/// This trait defines a lower camel case conversion.
11///
12/// In lowerCamelCase, word boundaries are indicated by capital letters,
13/// excepting the first word.
14///
15/// ## Example:
16///
17/// ```rust
18/// use heck::ToLowerCamelCase;
19///
20/// let sentence = "It is we who built these palaces and cities.";
21/// assert_eq!(sentence.to_lower_camel_case(), "itIsWeWhoBuiltThesePalacesAndCities");
22/// ```
23pub trait ToLowerCamelCase: ToOwned {
24 /// Convert this type to lower camel case.
25 fn to_lower_camel_case(&self) -> Self::Owned;
26}
27
28impl ToLowerCamelCase for str {
29 fn to_lower_camel_case(&self) -> String {
30 AsLowerCamelCase(self).to_string()
31 }
32}
33
34/// This wrapper performs a lower camel case conversion in [`fmt::Display`].
35///
36/// ## Example:
37///
38/// ```
39/// use heck::AsLowerCamelCase;
40///
41/// let sentence = "It is we who built these palaces and cities.";
42/// assert_eq!(format!("{}", AsLowerCamelCase(sentence)), "itIsWeWhoBuiltThesePalacesAndCities");
43/// ```
44pub struct AsLowerCamelCase<T: AsRef<str>>(pub T);
45
46impl<T: AsRef<str>> fmt::Display for AsLowerCamelCase<T> {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 let mut first: bool = true;
49 transform(
50 self.0.as_ref(),
51 |s, f| {
52 if first {
53 first = false;
54 lowercase(s, f)
55 } else {
56 capitalize(s, f)
57 }
58 },
59 |_| Ok(()),
60 f,
61 )
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::ToLowerCamelCase;
68
69 macro_rules! t {
70 ($t:ident : $s1:expr => $s2:expr) => {
71 #[test]
72 fn $t() {
73 assert_eq!($s1.to_lower_camel_case(), $s2)
74 }
75 };
76 }
77
78 t!(test1: "CamelCase" => "camelCase");
79 t!(test2: "This is Human case." => "thisIsHumanCase");
80 t!(test3: "MixedUP CamelCase, with some Spaces" => "mixedUpCamelCaseWithSomeSpaces");
81 t!(test4: "mixed_up_ snake_case, with some _spaces" => "mixedUpSnakeCaseWithSomeSpaces");
82 t!(test5: "kebab-case" => "kebabCase");
83 t!(test6: "SHOUTY_SNAKE_CASE" => "shoutySnakeCase");
84 t!(test7: "snake_case" => "snakeCase");
85 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "thisContainsAllKindsOfWordBoundaries");
86 t!(test9: "XΣXΣ baffle" => "xσxςBaffle");
87 t!(test10: "XMLHttpRequest" => "xmlHttpRequest");
88}
89