1 | use std::fmt; |
2 | |
3 | use crate::{transform, uppercase}; |
4 | |
5 | /// This trait defines a shouty kebab case conversion. |
6 | /// |
7 | /// In SHOUTY-KEBAB-CASE, word boundaries are indicated by hyphens and all |
8 | /// words are in uppercase. |
9 | /// |
10 | /// ## Example: |
11 | /// |
12 | /// ```rust |
13 | /// use heck::ToShoutyKebabCase; |
14 | /// |
15 | /// let sentence = "We are going to inherit the earth." ; |
16 | /// assert_eq!(sentence.to_shouty_kebab_case(), "WE-ARE-GOING-TO-INHERIT-THE-EARTH" ); |
17 | /// ``` |
18 | pub trait ToShoutyKebabCase: ToOwned { |
19 | /// Convert this type to shouty kebab case. |
20 | fn to_shouty_kebab_case(&self) -> Self::Owned; |
21 | } |
22 | |
23 | impl ToShoutyKebabCase for str { |
24 | fn to_shouty_kebab_case(&self) -> Self::Owned { |
25 | AsShoutyKebabCase(self).to_string() |
26 | } |
27 | } |
28 | |
29 | /// This wrapper performs a kebab case conversion in [`fmt::Display`]. |
30 | /// |
31 | /// ## Example: |
32 | /// |
33 | /// ``` |
34 | /// use heck::AsShoutyKebabCase; |
35 | /// |
36 | /// let sentence = "We are going to inherit the earth." ; |
37 | /// assert_eq!(format!("{}" , AsShoutyKebabCase(sentence)), "WE-ARE-GOING-TO-INHERIT-THE-EARTH" ); |
38 | /// ``` |
39 | pub struct AsShoutyKebabCase<T: AsRef<str>>(pub T); |
40 | |
41 | impl<T: AsRef<str>> fmt::Display for AsShoutyKebabCase<T> { |
42 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
43 | transform(self.0.as_ref(), with_word:uppercase, |f: &mut Formatter<'_>| write!(f, "-" ), f) |
44 | } |
45 | } |
46 | |
47 | #[cfg (test)] |
48 | mod tests { |
49 | use super::ToShoutyKebabCase; |
50 | |
51 | macro_rules! t { |
52 | ($t:ident : $s1:expr => $s2:expr) => { |
53 | #[test] |
54 | fn $t() { |
55 | assert_eq!($s1.to_shouty_kebab_case(), $s2) |
56 | } |
57 | }; |
58 | } |
59 | |
60 | t!(test1: "CamelCase" => "CAMEL-CASE" ); |
61 | t!(test2: "This is Human case." => "THIS-IS-HUMAN-CASE" ); |
62 | t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED-UP-CAMEL-CASE-WITH-SOME-SPACES" ); |
63 | t!(test4: "mixed_up_ snake_case with some _spaces" => "MIXED-UP-SNAKE-CASE-WITH-SOME-SPACES" ); |
64 | t!(test5: "kebab-case" => "KEBAB-CASE" ); |
65 | t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY-SNAKE-CASE" ); |
66 | t!(test7: "snake_case" => "SNAKE-CASE" ); |
67 | t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS-CONTAINS-ALL-KINDS-OF-WORD-BOUNDARIES" ); |
68 | #[cfg (feature = "unicode" )] |
69 | t!(test9: "XΣXΣ baffle" => "XΣXΣ-BAFFLE" ); |
70 | t!(test10: "XMLHttpRequest" => "XML-HTTP-REQUEST" ); |
71 | t!(test11: "SHOUTY-KEBAB-CASE" => "SHOUTY-KEBAB-CASE" ); |
72 | } |
73 | |