1use core::fmt;
2
3use alloc::{borrow::ToOwned, string::ToString};
4
5use crate::{capitalize, transform};
6
7/// This trait defines a train case conversion.
8///
9/// In Train-Case, word boundaries are indicated by hyphens and words start
10/// with Capital Letters.
11///
12/// ## Example:
13///
14/// ```rust
15/// use heck::ToTrainCase;
16///
17/// let sentence = "We are going to inherit the earth.";
18/// assert_eq!(sentence.to_train_case(), "We-Are-Going-To-Inherit-The-Earth");
19/// ```
20pub trait ToTrainCase: ToOwned {
21 /// Convert this type to Train-Case.
22 fn to_train_case(&self) -> Self::Owned;
23}
24
25impl ToTrainCase for str {
26 fn to_train_case(&self) -> Self::Owned {
27 AsTrainCase(self).to_string()
28 }
29}
30
31/// This wrapper performs a train case conversion in [`fmt::Display`].
32///
33/// ## Example:
34///
35/// ```
36/// use heck::AsTrainCase;
37///
38/// let sentence = "We are going to inherit the earth.";
39/// assert_eq!(format!("{}", AsTrainCase(sentence)), "We-Are-Going-To-Inherit-The-Earth");
40/// ```
41pub struct AsTrainCase<T: AsRef<str>>(pub T);
42
43impl<T: AsRef<str>> fmt::Display for AsTrainCase<T> {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 transform(self.0.as_ref(), with_word:capitalize, |f: &mut Formatter<'_>| write!(f, "-"), f)
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::ToTrainCase;
52
53 macro_rules! t {
54 ($t:ident : $s1:expr => $s2:expr) => {
55 #[test]
56 fn $t() {
57 assert_eq!($s1.to_train_case(), $s2)
58 }
59 };
60 }
61
62 t!(test1: "CamelCase" => "Camel-Case");
63 t!(test2: "This is Human case." => "This-Is-Human-Case");
64 t!(test3: "MixedUP CamelCase, with some Spaces" => "Mixed-Up-Camel-Case-With-Some-Spaces");
65 t!(test4: "mixed_up_ snake_case with some _spaces" => "Mixed-Up-Snake-Case-With-Some-Spaces");
66 t!(test5: "kebab-case" => "Kebab-Case");
67 t!(test6: "SHOUTY_SNAKE_CASE" => "Shouty-Snake-Case");
68 t!(test7: "snake_case" => "Snake-Case");
69 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "This-Contains-All-Kinds-Of-Word-Boundaries");
70 #[cfg(feature = "unicode")]
71 t!(test9: "XΣXΣ baffle" => "Xσxς-Baffle");
72 t!(test10: "XMLHttpRequest" => "Xml-Http-Request");
73 t!(test11: "FIELD_NAME11" => "Field-Name11");
74 t!(test12: "99BOTTLES" => "99bottles");
75 t!(test13: "FieldNamE11" => "Field-Nam-E11");
76 t!(test14: "abc123def456" => "Abc123def456");
77 t!(test16: "abc123DEF456" => "Abc123-Def456");
78 t!(test17: "abc123Def456" => "Abc123-Def456");
79 t!(test18: "abc123DEf456" => "Abc123-D-Ef456");
80 t!(test19: "ABC123def456" => "Abc123def456");
81 t!(test20: "ABC123DEF456" => "Abc123def456");
82 t!(test21: "ABC123Def456" => "Abc123-Def456");
83 t!(test22: "ABC123DEf456" => "Abc123d-Ef456");
84 t!(test23: "ABC123dEEf456FOO" => "Abc123d-E-Ef456-Foo");
85 t!(test24: "abcDEF" => "Abc-Def");
86 t!(test25: "ABcDE" => "A-Bc-De");
87}
88