1 | use std::fmt; |
2 | |
3 | use crate::{capitalize, transform}; |
4 | |
5 | /// This trait defines a train case conversion. |
6 | /// |
7 | /// In Train-Case, word boundaries are indicated by hyphens and words start |
8 | /// with Capital Letters. |
9 | /// |
10 | /// ## Example: |
11 | /// |
12 | /// ```rust |
13 | /// use heck::ToTrainCase; |
14 | /// |
15 | /// let sentence = "We are going to inherit the earth." ; |
16 | /// assert_eq!(sentence.to_train_case(), "We-Are-Going-To-Inherit-The-Earth" ); |
17 | /// ``` |
18 | pub trait ToTrainCase: ToOwned { |
19 | /// Convert this type to Train-Case. |
20 | fn to_train_case(&self) -> Self::Owned; |
21 | } |
22 | |
23 | impl ToTrainCase for str { |
24 | fn to_train_case(&self) -> Self::Owned { |
25 | AsTrainCase(self).to_string() |
26 | } |
27 | } |
28 | |
29 | /// This wrapper performs a train case conversion in [`fmt::Display`]. |
30 | /// |
31 | /// ## Example: |
32 | /// |
33 | /// ``` |
34 | /// use heck::AsTrainCase; |
35 | /// |
36 | /// let sentence = "We are going to inherit the earth." ; |
37 | /// assert_eq!(format!("{}" , AsTrainCase(sentence)), "We-Are-Going-To-Inherit-The-Earth" ); |
38 | /// ``` |
39 | pub struct AsTrainCase<T: AsRef<str>>(pub T); |
40 | |
41 | impl<T: AsRef<str>> fmt::Display for AsTrainCase<T> { |
42 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
43 | transform(self.0.as_ref(), with_word:capitalize, |f: &mut Formatter<'_>| write!(f, "-" ), f) |
44 | } |
45 | } |
46 | |
47 | #[cfg (test)] |
48 | mod tests { |
49 | use super::ToTrainCase; |
50 | |
51 | macro_rules! t { |
52 | ($t:ident : $s1:expr => $s2:expr) => { |
53 | #[test] |
54 | fn $t() { |
55 | assert_eq!($s1.to_train_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: "FIELD_NAME11" => "Field-Name11" ); |
72 | t!(test12: "99BOTTLES" => "99bottles" ); |
73 | t!(test13: "FieldNamE11" => "Field-Nam-E11" ); |
74 | t!(test14: "abc123def456" => "Abc123def456" ); |
75 | t!(test16: "abc123DEF456" => "Abc123-Def456" ); |
76 | t!(test17: "abc123Def456" => "Abc123-Def456" ); |
77 | t!(test18: "abc123DEf456" => "Abc123-D-Ef456" ); |
78 | t!(test19: "ABC123def456" => "Abc123def456" ); |
79 | t!(test20: "ABC123DEF456" => "Abc123def456" ); |
80 | t!(test21: "ABC123Def456" => "Abc123-Def456" ); |
81 | t!(test22: "ABC123DEf456" => "Abc123d-Ef456" ); |
82 | t!(test23: "ABC123dEEf456FOO" => "Abc123d-E-Ef456-Foo" ); |
83 | t!(test24: "abcDEF" => "Abc-Def" ); |
84 | t!(test25: "ABcDE" => "A-Bc-De" ); |
85 | } |
86 | |