16fccd0a4Sopenharmony_ciuse std::fmt;
26fccd0a4Sopenharmony_ci
36fccd0a4Sopenharmony_ciuse crate::{capitalize, transform};
46fccd0a4Sopenharmony_ci
56fccd0a4Sopenharmony_ci/// This trait defines a title case conversion.
66fccd0a4Sopenharmony_ci///
76fccd0a4Sopenharmony_ci/// In Title Case, word boundaries are indicated by spaces, and every word is
86fccd0a4Sopenharmony_ci/// capitalized.
96fccd0a4Sopenharmony_ci///
106fccd0a4Sopenharmony_ci/// ## Example:
116fccd0a4Sopenharmony_ci///
126fccd0a4Sopenharmony_ci/// ```rust
136fccd0a4Sopenharmony_ci/// use heck::ToTitleCase;
146fccd0a4Sopenharmony_ci///
156fccd0a4Sopenharmony_ci/// let sentence = "We have always lived in slums and holes in the wall.";
166fccd0a4Sopenharmony_ci/// assert_eq!(sentence.to_title_case(), "We Have Always Lived In Slums And Holes In The Wall");
176fccd0a4Sopenharmony_ci/// ```
186fccd0a4Sopenharmony_cipub trait ToTitleCase: ToOwned {
196fccd0a4Sopenharmony_ci    /// Convert this type to title case.
206fccd0a4Sopenharmony_ci    fn to_title_case(&self) -> Self::Owned;
216fccd0a4Sopenharmony_ci}
226fccd0a4Sopenharmony_ci
236fccd0a4Sopenharmony_ciimpl ToTitleCase for str {
246fccd0a4Sopenharmony_ci    fn to_title_case(&self) -> String {
256fccd0a4Sopenharmony_ci        AsTitleCase(self).to_string()
266fccd0a4Sopenharmony_ci    }
276fccd0a4Sopenharmony_ci}
286fccd0a4Sopenharmony_ci
296fccd0a4Sopenharmony_ci/// This wrapper performs a title case conversion in [`fmt::Display`].
306fccd0a4Sopenharmony_ci///
316fccd0a4Sopenharmony_ci/// ## Example:
326fccd0a4Sopenharmony_ci///
336fccd0a4Sopenharmony_ci/// ```
346fccd0a4Sopenharmony_ci/// use heck::AsTitleCase;
356fccd0a4Sopenharmony_ci///
366fccd0a4Sopenharmony_ci/// let sentence = "We have always lived in slums and holes in the wall.";
376fccd0a4Sopenharmony_ci/// assert_eq!(format!("{}", AsTitleCase(sentence)), "We Have Always Lived In Slums And Holes In The Wall");
386fccd0a4Sopenharmony_ci/// ```
396fccd0a4Sopenharmony_cipub struct AsTitleCase<T: AsRef<str>>(pub T);
406fccd0a4Sopenharmony_ci
416fccd0a4Sopenharmony_ciimpl<T: AsRef<str>> fmt::Display for AsTitleCase<T> {
426fccd0a4Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
436fccd0a4Sopenharmony_ci        transform(self.0.as_ref(), capitalize, |f| write!(f, " "), f)
446fccd0a4Sopenharmony_ci    }
456fccd0a4Sopenharmony_ci}
466fccd0a4Sopenharmony_ci
476fccd0a4Sopenharmony_ci#[cfg(test)]
486fccd0a4Sopenharmony_cimod tests {
496fccd0a4Sopenharmony_ci    use super::ToTitleCase;
506fccd0a4Sopenharmony_ci
516fccd0a4Sopenharmony_ci    macro_rules! t {
526fccd0a4Sopenharmony_ci        ($t:ident : $s1:expr => $s2:expr) => {
536fccd0a4Sopenharmony_ci            #[test]
546fccd0a4Sopenharmony_ci            fn $t() {
556fccd0a4Sopenharmony_ci                assert_eq!($s1.to_title_case(), $s2)
566fccd0a4Sopenharmony_ci            }
576fccd0a4Sopenharmony_ci        };
586fccd0a4Sopenharmony_ci    }
596fccd0a4Sopenharmony_ci
606fccd0a4Sopenharmony_ci    t!(test1: "CamelCase" => "Camel Case");
616fccd0a4Sopenharmony_ci    t!(test2: "This is Human case." => "This Is Human Case");
626fccd0a4Sopenharmony_ci    t!(test3: "MixedUP CamelCase, with some Spaces" => "Mixed Up Camel Case With Some Spaces");
636fccd0a4Sopenharmony_ci    t!(test4: "mixed_up_ snake_case, with some _spaces" => "Mixed Up Snake Case With Some Spaces");
646fccd0a4Sopenharmony_ci    t!(test5: "kebab-case" => "Kebab Case");
656fccd0a4Sopenharmony_ci    t!(test6: "SHOUTY_SNAKE_CASE" => "Shouty Snake Case");
666fccd0a4Sopenharmony_ci    t!(test7: "snake_case" => "Snake Case");
676fccd0a4Sopenharmony_ci    t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "This Contains All Kinds Of Word Boundaries");
686fccd0a4Sopenharmony_ci    #[cfg(feature = "unicode")]
696fccd0a4Sopenharmony_ci    t!(test9: "XΣXΣ baffle" => "Xσxς Baffle");
706fccd0a4Sopenharmony_ci    t!(test10: "XMLHttpRequest" => "Xml Http Request");
716fccd0a4Sopenharmony_ci}
72