1 use crate::{capitalize, transform};
2 
3 /// This trait defines a camel case conversion.
4 ///
5 /// In CamelCase, word boundaries are indicated by capital letters, including
6 /// the first word.
7 ///
8 /// ## Example:
9 ///
10 /// ```rust
11 /// use heck::CamelCase;
12 ///
13 /// let sentence = "We are not in the least afraid of ruins.";
14 /// assert_eq!(sentence.to_camel_case(), "WeAreNotInTheLeastAfraidOfRuins");
15 /// ```
16 pub trait CamelCase: ToOwned {
17     /// Convert this type to camel case.
to_camel_case(&self) -> Self::Owned18     fn to_camel_case(&self) -> Self::Owned;
19 }
20 
21 impl CamelCase for str {
to_camel_case(&self) -> String22     fn to_camel_case(&self) -> String {
23         transform(self, capitalize, |_| {})
24     }
25 }
26 
27 #[cfg(test)]
28 mod tests {
29     use super::CamelCase;
30 
31     macro_rules! t {
32         ($t:ident : $s1:expr => $s2:expr) => {
33             #[test]
34             fn $t() {
35                 assert_eq!($s1.to_camel_case(), $s2)
36             }
37         };
38     }
39 
40     t!(test1: "CamelCase" => "CamelCase");
41     t!(test2: "This is Human case." => "ThisIsHumanCase");
42     t!(test3: "MixedUP_CamelCase, with some Spaces" => "MixedUpCamelCaseWithSomeSpaces");
43     t!(test4: "mixed_up_ snake_case, with some _spaces" => "MixedUpSnakeCaseWithSomeSpaces");
44     t!(test5: "kebab-case" => "KebabCase");
45     t!(test6: "SHOUTY_SNAKE_CASE" => "ShoutySnakeCase");
46     t!(test7: "snake_case" => "SnakeCase");
47     t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "ThisContainsAllKindsOfWordBoundaries");
48     t!(test9: "XΣXΣ baffle" => "XσxςBaffle");
49     t!(test10: "XMLHttpRequest" => "XmlHttpRequest");
50 }
51