1 | use alloc::{ |
2 | borrow::ToOwned, |
3 | fmt, |
4 | string::{String, ToString}, |
5 | }; |
6 | |
7 | use crate::{lowercase, transform}; |
8 | |
9 | /// This trait defines a snake case conversion. |
10 | /// |
11 | /// In snake_case, word boundaries are indicated by underscores. |
12 | /// |
13 | /// ## Example: |
14 | /// |
15 | /// ```rust |
16 | /// use heck::ToSnakeCase; |
17 | /// |
18 | /// let sentence = "We carry a new world here, in our hearts." ; |
19 | /// assert_eq!(sentence.to_snake_case(), "we_carry_a_new_world_here_in_our_hearts" ); |
20 | /// ``` |
21 | pub trait ToSnakeCase: ToOwned { |
22 | /// Convert this type to snake case. |
23 | fn to_snake_case(&self) -> Self::Owned; |
24 | } |
25 | |
26 | /// Oh heck, `SnekCase` is an alias for [`ToSnakeCase`]. See ToSnakeCase for |
27 | /// more documentation. |
28 | pub trait ToSnekCase: ToOwned { |
29 | /// Convert this type to snek case. |
30 | fn to_snek_case(&self) -> Self::Owned; |
31 | } |
32 | |
33 | impl<T: ?Sized + ToSnakeCase> ToSnekCase for T { |
34 | fn to_snek_case(&self) -> Self::Owned { |
35 | self.to_snake_case() |
36 | } |
37 | } |
38 | |
39 | impl ToSnakeCase for str { |
40 | fn to_snake_case(&self) -> String { |
41 | AsSnakeCase(self).to_string() |
42 | } |
43 | } |
44 | |
45 | /// This wrapper performs a snake case conversion in [`fmt::Display`]. |
46 | /// |
47 | /// ## Example: |
48 | /// |
49 | /// ``` |
50 | /// use heck::AsSnakeCase; |
51 | /// |
52 | /// let sentence = "We carry a new world here, in our hearts." ; |
53 | /// assert_eq!(format!("{}" , AsSnakeCase(sentence)), "we_carry_a_new_world_here_in_our_hearts" ); |
54 | /// ``` |
55 | pub struct AsSnakeCase<T: AsRef<str>>(pub T); |
56 | |
57 | impl<T: AsRef<str>> fmt::Display for AsSnakeCase<T> { |
58 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
59 | transform(self.0.as_ref(), with_word:lowercase, |f: &mut Formatter<'_>| write!(f, "_" ), f) |
60 | } |
61 | } |
62 | |
63 | #[cfg (test)] |
64 | mod tests { |
65 | use super::ToSnakeCase; |
66 | |
67 | macro_rules! t { |
68 | ($t:ident : $s1:expr => $s2:expr) => { |
69 | #[test] |
70 | fn $t() { |
71 | assert_eq!($s1.to_snake_case(), $s2) |
72 | } |
73 | }; |
74 | } |
75 | |
76 | t!(test1: "CamelCase" => "camel_case" ); |
77 | t!(test2: "This is Human case." => "this_is_human_case" ); |
78 | t!(test3: "MixedUP CamelCase, with some Spaces" => "mixed_up_camel_case_with_some_spaces" ); |
79 | t!(test4: "mixed_up_ snake_case with some _spaces" => "mixed_up_snake_case_with_some_spaces" ); |
80 | t!(test5: "kebab-case" => "kebab_case" ); |
81 | t!(test6: "SHOUTY_SNAKE_CASE" => "shouty_snake_case" ); |
82 | t!(test7: "snake_case" => "snake_case" ); |
83 | t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "this_contains_all_kinds_of_word_boundaries" ); |
84 | t!(test9: "XΣXΣ baffle" => "xσxς_baffle" ); |
85 | t!(test10: "XMLHttpRequest" => "xml_http_request" ); |
86 | t!(test11: "FIELD_NAME11" => "field_name11" ); |
87 | t!(test12: "99BOTTLES" => "99bottles" ); |
88 | t!(test13: "FieldNamE11" => "field_nam_e11" ); |
89 | t!(test14: "abc123def456" => "abc123def456" ); |
90 | t!(test16: "abc123DEF456" => "abc123_def456" ); |
91 | t!(test17: "abc123Def456" => "abc123_def456" ); |
92 | t!(test18: "abc123DEf456" => "abc123_d_ef456" ); |
93 | t!(test19: "ABC123def456" => "abc123def456" ); |
94 | t!(test20: "ABC123DEF456" => "abc123def456" ); |
95 | t!(test21: "ABC123Def456" => "abc123_def456" ); |
96 | t!(test22: "ABC123DEf456" => "abc123d_ef456" ); |
97 | t!(test23: "ABC123dEEf456FOO" => "abc123d_e_ef456_foo" ); |
98 | t!(test24: "abcDEF" => "abc_def" ); |
99 | t!(test25: "ABcDE" => "a_bc_de" ); |
100 | } |
101 | |