16fccd0a4Sopenharmony_ciuse std::fmt;
26fccd0a4Sopenharmony_ci
36fccd0a4Sopenharmony_ciuse crate::{capitalize, lowercase, transform};
46fccd0a4Sopenharmony_ci
56fccd0a4Sopenharmony_ci/// This trait defines a lower camel case conversion.
66fccd0a4Sopenharmony_ci///
76fccd0a4Sopenharmony_ci/// In lowerCamelCase, word boundaries are indicated by capital letters,
86fccd0a4Sopenharmony_ci/// excepting the first word.
96fccd0a4Sopenharmony_ci///
106fccd0a4Sopenharmony_ci/// ## Example:
116fccd0a4Sopenharmony_ci///
126fccd0a4Sopenharmony_ci/// ```rust
136fccd0a4Sopenharmony_ci/// use heck::ToLowerCamelCase;
146fccd0a4Sopenharmony_ci///
156fccd0a4Sopenharmony_ci/// let sentence = "It is we who built these palaces and cities.";
166fccd0a4Sopenharmony_ci/// assert_eq!(sentence.to_lower_camel_case(), "itIsWeWhoBuiltThesePalacesAndCities");
176fccd0a4Sopenharmony_ci/// ```
186fccd0a4Sopenharmony_cipub trait ToLowerCamelCase: ToOwned {
196fccd0a4Sopenharmony_ci    /// Convert this type to lower camel case.
206fccd0a4Sopenharmony_ci    fn to_lower_camel_case(&self) -> Self::Owned;
216fccd0a4Sopenharmony_ci}
226fccd0a4Sopenharmony_ci
236fccd0a4Sopenharmony_ciimpl ToLowerCamelCase for str {
246fccd0a4Sopenharmony_ci    fn to_lower_camel_case(&self) -> String {
256fccd0a4Sopenharmony_ci        AsLowerCamelCase(self).to_string()
266fccd0a4Sopenharmony_ci    }
276fccd0a4Sopenharmony_ci}
286fccd0a4Sopenharmony_ci
296fccd0a4Sopenharmony_ci/// This wrapper performs a lower camel case conversion in [`fmt::Display`].
306fccd0a4Sopenharmony_ci///
316fccd0a4Sopenharmony_ci/// ## Example:
326fccd0a4Sopenharmony_ci///
336fccd0a4Sopenharmony_ci/// ```
346fccd0a4Sopenharmony_ci/// use heck::AsLowerCamelCase;
356fccd0a4Sopenharmony_ci///
366fccd0a4Sopenharmony_ci/// let sentence = "It is we who built these palaces and cities.";
376fccd0a4Sopenharmony_ci/// assert_eq!(format!("{}", AsLowerCamelCase(sentence)), "itIsWeWhoBuiltThesePalacesAndCities");
386fccd0a4Sopenharmony_ci/// ```
396fccd0a4Sopenharmony_cipub struct AsLowerCamelCase<T: AsRef<str>>(pub T);
406fccd0a4Sopenharmony_ci
416fccd0a4Sopenharmony_ciimpl<T: AsRef<str>> fmt::Display for AsLowerCamelCase<T> {
426fccd0a4Sopenharmony_ci    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
436fccd0a4Sopenharmony_ci        let mut first = true;
446fccd0a4Sopenharmony_ci        transform(
456fccd0a4Sopenharmony_ci            self.0.as_ref(),
466fccd0a4Sopenharmony_ci            |s, f| {
476fccd0a4Sopenharmony_ci                if first {
486fccd0a4Sopenharmony_ci                    first = false;
496fccd0a4Sopenharmony_ci                    lowercase(s, f)
506fccd0a4Sopenharmony_ci                } else {
516fccd0a4Sopenharmony_ci                    capitalize(s, f)
526fccd0a4Sopenharmony_ci                }
536fccd0a4Sopenharmony_ci            },
546fccd0a4Sopenharmony_ci            |_| Ok(()),
556fccd0a4Sopenharmony_ci            f,
566fccd0a4Sopenharmony_ci        )
576fccd0a4Sopenharmony_ci    }
586fccd0a4Sopenharmony_ci}
596fccd0a4Sopenharmony_ci
606fccd0a4Sopenharmony_ci#[cfg(test)]
616fccd0a4Sopenharmony_cimod tests {
626fccd0a4Sopenharmony_ci    use super::ToLowerCamelCase;
636fccd0a4Sopenharmony_ci
646fccd0a4Sopenharmony_ci    macro_rules! t {
656fccd0a4Sopenharmony_ci        ($t:ident : $s1:expr => $s2:expr) => {
666fccd0a4Sopenharmony_ci            #[test]
676fccd0a4Sopenharmony_ci            fn $t() {
686fccd0a4Sopenharmony_ci                assert_eq!($s1.to_lower_camel_case(), $s2)
696fccd0a4Sopenharmony_ci            }
706fccd0a4Sopenharmony_ci        };
716fccd0a4Sopenharmony_ci    }
726fccd0a4Sopenharmony_ci
736fccd0a4Sopenharmony_ci    t!(test1: "CamelCase" => "camelCase");
746fccd0a4Sopenharmony_ci    t!(test2: "This is Human case." => "thisIsHumanCase");
756fccd0a4Sopenharmony_ci    t!(test3: "MixedUP CamelCase, with some Spaces" => "mixedUpCamelCaseWithSomeSpaces");
766fccd0a4Sopenharmony_ci    t!(test4: "mixed_up_ snake_case, with some _spaces" => "mixedUpSnakeCaseWithSomeSpaces");
776fccd0a4Sopenharmony_ci    t!(test5: "kebab-case" => "kebabCase");
786fccd0a4Sopenharmony_ci    t!(test6: "SHOUTY_SNAKE_CASE" => "shoutySnakeCase");
796fccd0a4Sopenharmony_ci    t!(test7: "snake_case" => "snakeCase");
806fccd0a4Sopenharmony_ci    t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "thisContainsAllKindsOfWordBoundaries");
816fccd0a4Sopenharmony_ci    #[cfg(feature = "unicode")]
826fccd0a4Sopenharmony_ci    t!(test9: "XΣXΣ baffle" => "xσxςBaffle");
836fccd0a4Sopenharmony_ci    t!(test10: "XMLHttpRequest" => "xmlHttpRequest");
846fccd0a4Sopenharmony_ci    // TODO unicode tests
856fccd0a4Sopenharmony_ci}
86