1 use crate::{transform, uppercase}; 2 3 /// This trait defines a shouty kebab case conversion. 4 /// 5 /// In SHOUTY-KEBAB-CASE, word boundaries are indicated by hyphens and all 6 /// words are in uppercase. 7 /// 8 /// ## Example: 9 /// 10 /// ```rust 11 /// use heck::ShoutyKebabCase; 12 /// 13 /// let sentence = "We are going to inherit the earth."; 14 /// assert_eq!(sentence.to_shouty_kebab_case(), "WE-ARE-GOING-TO-INHERIT-THE-EARTH"); 15 /// ``` 16 pub trait ShoutyKebabCase: ToOwned { 17 /// Convert this type to shouty kebab case. to_shouty_kebab_case(&self) -> Self::Owned18 fn to_shouty_kebab_case(&self) -> Self::Owned; 19 } 20 21 impl ShoutyKebabCase for str { to_shouty_kebab_case(&self) -> Self::Owned22 fn to_shouty_kebab_case(&self) -> Self::Owned { 23 transform(self, uppercase, |s| s.push('-')) 24 } 25 } 26 27 #[cfg(test)] 28 mod tests { 29 use super::ShoutyKebabCase; 30 31 macro_rules! t { 32 ($t:ident : $s1:expr => $s2:expr) => { 33 #[test] 34 fn $t() { 35 assert_eq!($s1.to_shouty_kebab_case(), $s2) 36 } 37 } 38 } 39 40 t!(test1: "CamelCase" => "CAMEL-CASE"); 41 t!(test2: "This is Human case." => "THIS-IS-HUMAN-CASE"); 42 t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED-UP-CAMEL-CASE-WITH-SOME-SPACES"); 43 t!(test4: "mixed_up_ snake_case with some _spaces" => "MIXED-UP-SNAKE-CASE-WITH-SOME-SPACES"); 44 t!(test5: "kebab-case" => "KEBAB-CASE"); 45 t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY-SNAKE-CASE"); 46 t!(test7: "snake_case" => "SNAKE-CASE"); 47 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS-CONTAINS-ALL-KINDS-OF-WORD-BOUNDARIES"); 48 t!(test9: "XΣXΣ baffle" => "XΣXΣ-BAFFLE"); 49 t!(test10: "XMLHttpRequest" => "XML-HTTP-REQUEST"); 50 t!(test11: "SHOUTY-KEBAB-CASE" => "SHOUTY-KEBAB-CASE"); 51 } 52