1 use std::fmt; 2 3 use crate::{transform, uppercase}; 4 5 /// This trait defines a shouty snake case conversion. 6 /// 7 /// In SHOUTY_SNAKE_CASE, word boundaries are indicated by underscores and all 8 /// words are in uppercase. 9 /// 10 /// ## Example: 11 /// 12 /// ```rust 13 /// use heck::ToShoutySnakeCase; 14 /// 15 /// let sentence = "That world is growing in this minute."; 16 /// assert_eq!(sentence.to_shouty_snake_case(), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE"); 17 /// ``` 18 pub trait ToShoutySnakeCase: ToOwned { 19 /// Convert this type to shouty snake case. to_shouty_snake_case(&self) -> Self::Owned20 fn to_shouty_snake_case(&self) -> Self::Owned; 21 } 22 23 /// Oh heck, ToShoutySnekCase is an alias for ToShoutySnakeCase. See 24 /// ToShoutySnakeCase for more documentation. 25 pub trait ToShoutySnekCase: ToOwned { 26 /// CONVERT THIS TYPE TO SNEK CASE. 27 #[allow(non_snake_case)] TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned28 fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned; 29 } 30 31 impl<T: ?Sized + ToShoutySnakeCase> ToShoutySnekCase for T { TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned32 fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned { 33 self.to_shouty_snake_case() 34 } 35 } 36 37 impl ToShoutySnakeCase for str { to_shouty_snake_case(&self) -> Self::Owned38 fn to_shouty_snake_case(&self) -> Self::Owned { 39 AsShoutySnakeCase(self).to_string() 40 } 41 } 42 43 /// This wrapper performs a shouty snake case conversion in [`fmt::Display`]. 44 /// 45 /// ## Example: 46 /// 47 /// ``` 48 /// use heck::AsShoutySnakeCase; 49 /// 50 /// let sentence = "That world is growing in this minute."; 51 /// assert_eq!(format!("{}", AsShoutySnakeCase(sentence)), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE"); 52 /// ``` 53 pub struct AsShoutySnakeCase<T: AsRef<str>>(pub T); 54 55 impl<T: AsRef<str>> fmt::Display for AsShoutySnakeCase<T> { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 57 transform(self.0.as_ref(), uppercase, |f| write!(f, "_"), f) 58 } 59 } 60 61 #[cfg(test)] 62 mod tests { 63 use super::ToShoutySnakeCase; 64 65 macro_rules! t { 66 ($t:ident : $s1:expr => $s2:expr) => { 67 #[test] 68 fn $t() { 69 assert_eq!($s1.to_shouty_snake_case(), $s2) 70 } 71 }; 72 } 73 74 t!(test1: "CamelCase" => "CAMEL_CASE"); 75 t!(test2: "This is Human case." => "THIS_IS_HUMAN_CASE"); 76 t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED_UP_CAMEL_CASE_WITH_SOME_SPACES"); 77 t!(test4: "mixed_up_snake_case with some _spaces" => "MIXED_UP_SNAKE_CASE_WITH_SOME_SPACES"); 78 t!(test5: "kebab-case" => "KEBAB_CASE"); 79 t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY_SNAKE_CASE"); 80 t!(test7: "snake_case" => "SNAKE_CASE"); 81 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS_CONTAINS_ALL_KINDS_OF_WORD_BOUNDARIES"); 82 #[cfg(feature = "unicode")] 83 t!(test9: "XΣXΣ baffle" => "XΣXΣ_BAFFLE"); 84 t!(test10: "XMLHttpRequest" => "XML_HTTP_REQUEST"); 85 } 86