1use std::fmt;
2
3use crate::{lowercase, transform};
4
5pub trait ToKebabCase: ToOwned {
18 fn to_kebab_case(&self) -> Self::Owned;
20}
21
22impl ToKebabCase for str {
23 fn to_kebab_case(&self) -> Self::Owned {
24 AsKebabCase(self).to_string()
25 }
26}
27
28pub struct AsKebabCase<T: AsRef<str>>(pub T);
39
40impl<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)
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::ToKebabCase;
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 }
56 };
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");
67 #[cfg(feature = "unicode")]
68 t!(test9: "XΣXΣ baffle" => "xσxς-baffle");
69 t!(test10: "XMLHttpRequest" => "xml-http-request");
70}