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