]>
Commit | Line | Data |
---|---|---|
923072b8 FG |
1 | use std::fmt; |
2 | ||
5869c6ff XL |
3 | use crate::{lowercase, transform}; |
4 | ||
0731742a XL |
5 | /// This trait defines a kebab case conversion. |
6 | /// | |
7 | /// In kebab-case, word boundaries are indicated by hyphens. | |
8 | /// | |
9 | /// ## Example: | |
10 | /// | |
11 | /// ```rust | |
923072b8 | 12 | /// use heck::ToKebabCase; |
0731742a | 13 | /// |
5869c6ff XL |
14 | /// let sentence = "We are going to inherit the earth."; |
15 | /// assert_eq!(sentence.to_kebab_case(), "we-are-going-to-inherit-the-earth"); | |
0731742a | 16 | /// ``` |
923072b8 | 17 | pub trait ToKebabCase: ToOwned { |
0731742a XL |
18 | /// Convert this type to kebab case. |
19 | fn to_kebab_case(&self) -> Self::Owned; | |
20 | } | |
21 | ||
923072b8 | 22 | impl ToKebabCase for str { |
0731742a | 23 | fn to_kebab_case(&self) -> Self::Owned { |
923072b8 FG |
24 | AsKebabCase(self).to_string() |
25 | } | |
26 | } | |
27 | ||
28 | /// This wrapper performs a kebab case conversion in [`fmt::Display`]. | |
29 | /// | |
30 | /// ## Example: | |
31 | /// | |
32 | /// ``` | |
33 | /// use heck::AsKebabCase; | |
34 | /// | |
35 | /// let sentence = "We are going to inherit the earth."; | |
36 | /// assert_eq!(format!("{}", AsKebabCase(sentence)), "we-are-going-to-inherit-the-earth"); | |
37 | /// ``` | |
38 | pub struct AsKebabCase<T: AsRef<str>>(pub T); | |
39 | ||
40 | impl<T: AsRef<str>> fmt::Display for AsKebabCase<T> { | |
41 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
42 | transform(self.0.as_ref(), lowercase, |f| write!(f, "-"), f) | |
0731742a XL |
43 | } |
44 | } | |
45 | ||
46 | #[cfg(test)] | |
47 | mod tests { | |
923072b8 | 48 | use super::ToKebabCase; |
0731742a XL |
49 | |
50 | macro_rules! t { | |
51 | ($t:ident : $s1:expr => $s2:expr) => { | |
52 | #[test] | |
53 | fn $t() { | |
54 | assert_eq!($s1.to_kebab_case(), $s2) | |
55 | } | |
136023e0 | 56 | }; |
0731742a XL |
57 | } |
58 | ||
59 | t!(test1: "CamelCase" => "camel-case"); | |
60 | t!(test2: "This is Human case." => "this-is-human-case"); | |
61 | t!(test3: "MixedUP CamelCase, with some Spaces" => "mixed-up-camel-case-with-some-spaces"); | |
62 | t!(test4: "mixed_up_ snake_case with some _spaces" => "mixed-up-snake-case-with-some-spaces"); | |
63 | t!(test5: "kebab-case" => "kebab-case"); | |
64 | t!(test6: "SHOUTY_SNAKE_CASE" => "shouty-snake-case"); | |
65 | t!(test7: "snake_case" => "snake-case"); | |
66 | t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "this-contains-all-kinds-of-word-boundaries"); | |
923072b8 | 67 | #[cfg(feature = "unicode")] |
0731742a XL |
68 | t!(test9: "XΣXΣ baffle" => "xσxς-baffle"); |
69 | t!(test10: "XMLHttpRequest" => "xml-http-request"); | |
70 | } |