1 | use core::fmt; |
2 | |
3 | use alloc::{ |
4 | borrow::ToOwned, |
5 | string::{String, ToString}, |
6 | }; |
7 | |
8 | use crate::{capitalize, transform}; |
9 | |
10 | /// This trait defines an upper camel case conversion. |
11 | /// |
12 | /// In UpperCamelCase, word boundaries are indicated by capital letters, |
13 | /// including the first word. |
14 | /// |
15 | /// ## Example: |
16 | /// |
17 | /// ```rust |
18 | /// use heck::ToUpperCamelCase; |
19 | /// |
20 | /// let sentence = "We are not in the least afraid of ruins." ; |
21 | /// assert_eq!(sentence.to_upper_camel_case(), "WeAreNotInTheLeastAfraidOfRuins" ); |
22 | /// ``` |
23 | pub trait ToUpperCamelCase: ToOwned { |
24 | /// Convert this type to upper camel case. |
25 | fn to_upper_camel_case(&self) -> Self::Owned; |
26 | } |
27 | |
28 | impl ToUpperCamelCase for str { |
29 | fn to_upper_camel_case(&self) -> String { |
30 | AsUpperCamelCase(self).to_string() |
31 | } |
32 | } |
33 | |
34 | /// `ToPascalCase` is an alias for [`ToUpperCamelCase`]. See ToUpperCamelCase for more |
35 | /// documentation. |
36 | pub trait ToPascalCase: ToOwned { |
37 | /// Convert this type to upper camel case. |
38 | fn to_pascal_case(&self) -> Self::Owned; |
39 | } |
40 | |
41 | impl<T: ?Sized + ToUpperCamelCase> ToPascalCase for T { |
42 | fn to_pascal_case(&self) -> Self::Owned { |
43 | self.to_upper_camel_case() |
44 | } |
45 | } |
46 | |
47 | /// This wrapper performs a upper camel case conversion in [`fmt::Display`]. |
48 | /// |
49 | /// ## Example: |
50 | /// |
51 | /// ``` |
52 | /// use heck::AsUpperCamelCase; |
53 | /// |
54 | /// let sentence = "We are not in the least afraid of ruins." ; |
55 | /// assert_eq!(format!("{}" , AsUpperCamelCase(sentence)), "WeAreNotInTheLeastAfraidOfRuins" ); |
56 | /// ``` |
57 | pub struct AsUpperCamelCase<T: AsRef<str>>(pub T); |
58 | |
59 | impl<T: AsRef<str>> fmt::Display for AsUpperCamelCase<T> { |
60 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
61 | transform(self.0.as_ref(), with_word:capitalize, |_| Ok(()), f) |
62 | } |
63 | } |
64 | |
65 | #[cfg (test)] |
66 | mod tests { |
67 | use super::ToUpperCamelCase; |
68 | |
69 | macro_rules! t { |
70 | ($t:ident : $s1:expr => $s2:expr) => { |
71 | #[test] |
72 | fn $t() { |
73 | assert_eq!($s1.to_upper_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 | |