heck/
upper_camel.rs
1use core::fmt;
2
3use alloc::{
4 borrow::ToOwned,
5 string::{String, ToString},
6};
7
8use crate::{capitalize, transform};
9
10pub trait ToUpperCamelCase: ToOwned {
24 fn to_upper_camel_case(&self) -> Self::Owned;
26}
27
28impl ToUpperCamelCase for str {
29 fn to_upper_camel_case(&self) -> String {
30 AsUpperCamelCase(self).to_string()
31 }
32}
33
34pub trait ToPascalCase: ToOwned {
37 fn to_pascal_case(&self) -> Self::Owned;
39}
40
41impl<T: ?Sized + ToUpperCamelCase> ToPascalCase for T {
42 fn to_pascal_case(&self) -> Self::Owned {
43 self.to_upper_camel_case()
44 }
45}
46
47pub struct AsUpperCamelCase<T: AsRef<str>>(pub T);
58
59impl<T: AsRef<str>> fmt::Display for AsUpperCamelCase<T> {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 transform(self.0.as_ref(), capitalize, |_| Ok(()), f)
62 }
63}
64
65#[cfg(test)]
66mod 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}