]> git.proxmox.com Git - rustc.git/blob - vendor/heck/src/shouty_snake.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / vendor / heck / src / shouty_snake.rs
1 use crate::{transform, uppercase};
2
3 /// This trait defines a shouty snake case conversion.
4 ///
5 /// In SHOUTY_SNAKE_CASE, word boundaries are indicated by underscores and all
6 /// words are in uppercase.
7 ///
8 /// ## Example:
9 ///
10 /// ```rust
11 /// use heck::ShoutySnakeCase;
12 ///
13 /// let sentence = "That world is growing in this minute.";
14 /// assert_eq!(sentence.to_shouty_snake_case(), "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE");
15 /// ```
16 pub trait ShoutySnakeCase: ToOwned {
17 /// Convert this type to shouty snake case.
18 fn to_shouty_snake_case(&self) -> Self::Owned;
19 }
20
21 /// Oh heck, ShoutySnekCase is an alias for ShoutySnakeCase. See ShoutySnakeCase
22 /// for more documentation.
23 pub trait ShoutySnekCase: ToOwned {
24 /// CONVERT THIS TYPE TO SNEK CASE.
25 #[allow(non_snake_case)]
26 fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned;
27 }
28
29 impl<T: ?Sized + ShoutySnakeCase> ShoutySnekCase for T {
30 fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned {
31 self.to_shouty_snake_case()
32 }
33 }
34
35 impl ShoutySnakeCase for str {
36 fn to_shouty_snake_case(&self) -> Self::Owned {
37 transform(self, uppercase, |s| s.push('_'))
38 }
39 }
40
41 #[cfg(test)]
42 mod tests {
43 use super::ShoutySnakeCase;
44
45 macro_rules! t {
46 ($t:ident : $s1:expr => $s2:expr) => {
47 #[test]
48 fn $t() {
49 assert_eq!($s1.to_shouty_snake_case(), $s2)
50 }
51 };
52 }
53
54 t!(test1: "CamelCase" => "CAMEL_CASE");
55 t!(test2: "This is Human case." => "THIS_IS_HUMAN_CASE");
56 t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED_UP_CAMEL_CASE_WITH_SOME_SPACES");
57 t!(test4: "mixed_up_snake_case with some _spaces" => "MIXED_UP_SNAKE_CASE_WITH_SOME_SPACES");
58 t!(test5: "kebab-case" => "KEBAB_CASE");
59 t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY_SNAKE_CASE");
60 t!(test7: "snake_case" => "SNAKE_CASE");
61 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS_CONTAINS_ALL_KINDS_OF_WORD_BOUNDARIES");
62 t!(test9: "XΣXΣ baffle" => "XΣXΣ_BAFFLE");
63 t!(test10: "XMLHttpRequest" => "XML_HTTP_REQUEST");
64 }