1use core::fmt;
2
3use alloc::{
4 borrow::ToOwned,
5 string::{String, ToString},
6};
7
8use crate::{capitalize, transform};
9
10pub trait ToTitleCase: ToOwned {
24 fn to_title_case(&self) -> Self::Owned;
26}
27
28impl ToTitleCase for str {
29 fn to_title_case(&self) -> String {
30 AsTitleCase(self).to_string()
31 }
32}
33
34pub struct AsTitleCase<T: AsRef<str>>(pub T);
45
46impl<T: AsRef<str>> fmt::Display for AsTitleCase<T> {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 transform(self.0.as_ref(), capitalize, |f| write!(f, " "), f)
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::ToTitleCase;
55
56 macro_rules! t {
57 ($t:ident : $s1:expr => $s2:expr) => {
58 #[test]
59 fn $t() {
60 assert_eq!($s1.to_title_case(), $s2)
61 }
62 };
63 }
64
65 t!(test1: "CamelCase" => "Camel Case");
66 t!(test2: "This is Human case." => "This Is Human Case");
67 t!(test3: "MixedUP CamelCase, with some Spaces" => "Mixed Up Camel Case With Some Spaces");
68 t!(test4: "mixed_up_ snake_case, with some _spaces" => "Mixed Up Snake Case With Some Spaces");
69 t!(test5: "kebab-case" => "Kebab Case");
70 t!(test6: "SHOUTY_SNAKE_CASE" => "Shouty Snake Case");
71 t!(test7: "snake_case" => "Snake Case");
72 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "This Contains All Kinds Of Word Boundaries");
73 t!(test9: "XΣXΣ baffle" => "Xσxς Baffle");
74 t!(test10: "XMLHttpRequest" => "Xml Http Request");
75}