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