1use core::fmt;
2
3use alloc::{borrow::ToOwned, string::ToString};
4
5use crate::{lowercase, transform};
6
7pub trait ToKebabCase: ToOwned {
20 fn to_kebab_case(&self) -> Self::Owned;
22}
23
24impl ToKebabCase for str {
25 fn to_kebab_case(&self) -> Self::Owned {
26 AsKebabCase(self).to_string()
27 }
28}
29
30pub struct AsKebabCase<T: AsRef<str>>(pub T);
41
42impl<T: AsRef<str>> fmt::Display for AsKebabCase<T> {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 transform(self.0.as_ref(), lowercase, |f| write!(f, "-"), f)
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::ToKebabCase;
51
52 macro_rules! t {
53 ($t:ident : $s1:expr => $s2:expr) => {
54 #[test]
55 fn $t() {
56 assert_eq!($s1.to_kebab_case(), $s2)
57 }
58 };
59 }
60
61 t!(test1: "CamelCase" => "camel-case");
62 t!(test2: "This is Human case." => "this-is-human-case");
63 t!(test3: "MixedUP CamelCase, with some Spaces" => "mixed-up-camel-case-with-some-spaces");
64 t!(test4: "mixed_up_ snake_case with some _spaces" => "mixed-up-snake-case-with-some-spaces");
65 t!(test5: "kebab-case" => "kebab-case");
66 t!(test6: "SHOUTY_SNAKE_CASE" => "shouty-snake-case");
67 t!(test7: "snake_case" => "snake-case");
68 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "this-contains-all-kinds-of-word-boundaries");
69 t!(test9: "XΣXΣ baffle" => "xσxς-baffle");
70 t!(test10: "XMLHttpRequest" => "xml-http-request");
71 t!(test11: "لِنَذْهَبْ إِلَى السِّيْنَمَا" => "لِنَذْهَبْ-إِلَى-السِّيْنَمَا");
72 t!(test12: "ファイルを読み込み" => "ファイルを読み込み");
74 t!(test13: "祝你一天过得愉快" => "祝你一天过得愉快");
75}