16fccd0a4Sopenharmony_ciuse std::fmt; 26fccd0a4Sopenharmony_ci 36fccd0a4Sopenharmony_ciuse crate::{transform, uppercase}; 46fccd0a4Sopenharmony_ci 56fccd0a4Sopenharmony_ci/// This trait defines a shouty snake case conversion. 66fccd0a4Sopenharmony_ci/// 76fccd0a4Sopenharmony_ci/// In SHOUTY_SNAKE_CASE, word boundaries are indicated by underscores and all 86fccd0a4Sopenharmony_ci/// words are in uppercase. 96fccd0a4Sopenharmony_ci/// 106fccd0a4Sopenharmony_ci/// ## Example: 116fccd0a4Sopenharmony_ci/// 126fccd0a4Sopenharmony_ci/// ```rust 136fccd0a4Sopenharmony_ci/// use heck::ToShoutySnakeCase; 146fccd0a4Sopenharmony_ci/// 156fccd0a4Sopenharmony_ci/// let sentence = "That world is growing in this minute."; 166fccd0a4Sopenharmony_ci/// assert_eq!(sentence.to_shouty_snake_case(), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE"); 176fccd0a4Sopenharmony_ci/// ``` 186fccd0a4Sopenharmony_cipub trait ToShoutySnakeCase: ToOwned { 196fccd0a4Sopenharmony_ci /// Convert this type to shouty snake case. 206fccd0a4Sopenharmony_ci fn to_shouty_snake_case(&self) -> Self::Owned; 216fccd0a4Sopenharmony_ci} 226fccd0a4Sopenharmony_ci 236fccd0a4Sopenharmony_ci/// Oh heck, ToShoutySnekCase is an alias for ToShoutySnakeCase. See 246fccd0a4Sopenharmony_ci/// ToShoutySnakeCase for more documentation. 256fccd0a4Sopenharmony_cipub trait ToShoutySnekCase: ToOwned { 266fccd0a4Sopenharmony_ci /// CONVERT THIS TYPE TO SNEK CASE. 276fccd0a4Sopenharmony_ci #[allow(non_snake_case)] 286fccd0a4Sopenharmony_ci fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned; 296fccd0a4Sopenharmony_ci} 306fccd0a4Sopenharmony_ci 316fccd0a4Sopenharmony_ciimpl<T: ?Sized + ToShoutySnakeCase> ToShoutySnekCase for T { 326fccd0a4Sopenharmony_ci fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned { 336fccd0a4Sopenharmony_ci self.to_shouty_snake_case() 346fccd0a4Sopenharmony_ci } 356fccd0a4Sopenharmony_ci} 366fccd0a4Sopenharmony_ci 376fccd0a4Sopenharmony_ciimpl ToShoutySnakeCase for str { 386fccd0a4Sopenharmony_ci fn to_shouty_snake_case(&self) -> Self::Owned { 396fccd0a4Sopenharmony_ci AsShoutySnakeCase(self).to_string() 406fccd0a4Sopenharmony_ci } 416fccd0a4Sopenharmony_ci} 426fccd0a4Sopenharmony_ci 436fccd0a4Sopenharmony_ci/// This wrapper performs a shouty snake case conversion in [`fmt::Display`]. 446fccd0a4Sopenharmony_ci/// 456fccd0a4Sopenharmony_ci/// ## Example: 466fccd0a4Sopenharmony_ci/// 476fccd0a4Sopenharmony_ci/// ``` 486fccd0a4Sopenharmony_ci/// use heck::AsShoutySnakeCase; 496fccd0a4Sopenharmony_ci/// 506fccd0a4Sopenharmony_ci/// let sentence = "That world is growing in this minute."; 516fccd0a4Sopenharmony_ci/// assert_eq!(format!("{}", AsShoutySnakeCase(sentence)), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE"); 526fccd0a4Sopenharmony_ci/// ``` 536fccd0a4Sopenharmony_cipub struct AsShoutySnakeCase<T: AsRef<str>>(pub T); 546fccd0a4Sopenharmony_ci 556fccd0a4Sopenharmony_ciimpl<T: AsRef<str>> fmt::Display for AsShoutySnakeCase<T> { 566fccd0a4Sopenharmony_ci fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 576fccd0a4Sopenharmony_ci transform(self.0.as_ref(), uppercase, |f| write!(f, "_"), f) 586fccd0a4Sopenharmony_ci } 596fccd0a4Sopenharmony_ci} 606fccd0a4Sopenharmony_ci 616fccd0a4Sopenharmony_ci#[cfg(test)] 626fccd0a4Sopenharmony_cimod tests { 636fccd0a4Sopenharmony_ci use super::ToShoutySnakeCase; 646fccd0a4Sopenharmony_ci 656fccd0a4Sopenharmony_ci macro_rules! t { 666fccd0a4Sopenharmony_ci ($t:ident : $s1:expr => $s2:expr) => { 676fccd0a4Sopenharmony_ci #[test] 686fccd0a4Sopenharmony_ci fn $t() { 696fccd0a4Sopenharmony_ci assert_eq!($s1.to_shouty_snake_case(), $s2) 706fccd0a4Sopenharmony_ci } 716fccd0a4Sopenharmony_ci }; 726fccd0a4Sopenharmony_ci } 736fccd0a4Sopenharmony_ci 746fccd0a4Sopenharmony_ci t!(test1: "CamelCase" => "CAMEL_CASE"); 756fccd0a4Sopenharmony_ci t!(test2: "This is Human case." => "THIS_IS_HUMAN_CASE"); 766fccd0a4Sopenharmony_ci t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED_UP_CAMEL_CASE_WITH_SOME_SPACES"); 776fccd0a4Sopenharmony_ci t!(test4: "mixed_up_snake_case with some _spaces" => "MIXED_UP_SNAKE_CASE_WITH_SOME_SPACES"); 786fccd0a4Sopenharmony_ci t!(test5: "kebab-case" => "KEBAB_CASE"); 796fccd0a4Sopenharmony_ci t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY_SNAKE_CASE"); 806fccd0a4Sopenharmony_ci t!(test7: "snake_case" => "SNAKE_CASE"); 816fccd0a4Sopenharmony_ci t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS_CONTAINS_ALL_KINDS_OF_WORD_BOUNDARIES"); 826fccd0a4Sopenharmony_ci #[cfg(feature = "unicode")] 836fccd0a4Sopenharmony_ci t!(test9: "XΣXΣ baffle" => "XΣXΣ_BAFFLE"); 846fccd0a4Sopenharmony_ci t!(test10: "XMLHttpRequest" => "XML_HTTP_REQUEST"); 856fccd0a4Sopenharmony_ci} 86