heck/
shouty_snake.rs
1use std::fmt;
2
3use crate::{transform, uppercase};
4
5pub trait ToShoutySnakeCase: ToOwned {
19 fn to_shouty_snake_case(&self) -> Self::Owned;
21}
22
23pub trait ToShoutySnekCase: ToOwned {
26 #[allow(non_snake_case)]
28 fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned;
29}
30
31impl<T: ?Sized + ToShoutySnakeCase> ToShoutySnekCase for T {
32 fn TO_SHOUTY_SNEK_CASE(&self) -> Self::Owned {
33 self.to_shouty_snake_case()
34 }
35}
36
37impl ToShoutySnakeCase for str {
38 fn to_shouty_snake_case(&self) -> Self::Owned {
39 AsShoutySnakeCase(self).to_string()
40 }
41}
42
43pub struct AsShoutySnakeCase<T: AsRef<str>>(pub T);
54
55impl<T: AsRef<str>> fmt::Display for AsShoutySnakeCase<T> {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 transform(self.0.as_ref(), uppercase, |f| write!(f, "_"), f)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::ToShoutySnakeCase;
64
65 macro_rules! t {
66 ($t:ident : $s1:expr => $s2:expr) => {
67 #[test]
68 fn $t() {
69 assert_eq!($s1.to_shouty_snake_case(), $s2)
70 }
71 };
72 }
73
74 t!(test1: "CamelCase" => "CAMEL_CASE");
75 t!(test2: "This is Human case." => "THIS_IS_HUMAN_CASE");
76 t!(test3: "MixedUP CamelCase, with some Spaces" => "MIXED_UP_CAMEL_CASE_WITH_SOME_SPACES");
77 t!(test4: "mixed_up_snake_case with some _spaces" => "MIXED_UP_SNAKE_CASE_WITH_SOME_SPACES");
78 t!(test5: "kebab-case" => "KEBAB_CASE");
79 t!(test6: "SHOUTY_SNAKE_CASE" => "SHOUTY_SNAKE_CASE");
80 t!(test7: "snake_case" => "SNAKE_CASE");
81 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "THIS_CONTAINS_ALL_KINDS_OF_WORD_BOUNDARIES");
82 #[cfg(feature = "unicode")]
83 t!(test9: "XΣXΣ baffle" => "XΣXΣ_BAFFLE");
84 t!(test10: "XMLHttpRequest" => "XML_HTTP_REQUEST");
85}