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