| 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 a title case conversion. | 
|---|
| 11 | /// | 
|---|
| 12 | /// In Title Case, word boundaries are indicated by spaces, and every word is | 
|---|
| 13 | /// capitalized. | 
|---|
| 14 | /// | 
|---|
| 15 | /// ## Example: | 
|---|
| 16 | /// | 
|---|
| 17 | /// ```rust | 
|---|
| 18 | /// use heck::ToTitleCase; | 
|---|
| 19 | /// | 
|---|
| 20 | /// let sentence = "We have always lived in slums and holes in the wall."; | 
|---|
| 21 | /// assert_eq!(sentence.to_title_case(), "We Have Always Lived In Slums And Holes In The Wall"); | 
|---|
| 22 | /// ``` | 
|---|
| 23 | pub trait ToTitleCase: ToOwned { | 
|---|
| 24 | /// Convert this type to title case. | 
|---|
| 25 | fn to_title_case(&self) -> Self::Owned; | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | impl ToTitleCase for str { | 
|---|
| 29 | fn to_title_case(&self) -> String { | 
|---|
| 30 | AsTitleCase(self).to_string() | 
|---|
| 31 | } | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | /// This wrapper performs a title case conversion in [`fmt::Display`]. | 
|---|
| 35 | /// | 
|---|
| 36 | /// ## Example: | 
|---|
| 37 | /// | 
|---|
| 38 | /// ``` | 
|---|
| 39 | /// use heck::AsTitleCase; | 
|---|
| 40 | /// | 
|---|
| 41 | /// let sentence = "We have always lived in slums and holes in the wall."; | 
|---|
| 42 | /// assert_eq!(format!( "{}", AsTitleCase(sentence)), "We Have Always Lived In Slums And Holes In The Wall"); | 
|---|
| 43 | /// ``` | 
|---|
| 44 | pub struct AsTitleCase<T: AsRef<str>>(pub T); | 
|---|
| 45 |  | 
|---|
| 46 | impl<T: AsRef<str>> fmt::Display for AsTitleCase<T> { | 
|---|
| 47 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 
|---|
| 48 | transform(self.0.as_ref(), with_word:capitalize, |f: &mut Formatter<'_>| write!(f, " "), f) | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | #[ cfg(test)] | 
|---|
| 53 | mod 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 | } | 
|---|
| 76 |  | 
|---|