1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
pub trait SnakeCase: ToOwned {
fn to_snake_case(&self) -> Self::Owned;
}
pub trait SnekCase: ToOwned {
fn to_snek_case(&self) -> Self::Owned;
}
impl<T: SnakeCase> SnekCase for T {
fn to_snek_case(&self) -> Self::Owned {
self.to_snake_case()
}
}
impl SnakeCase for str {
fn to_snake_case(&self) -> String {
::transform(self, ::lowercase, |s| s.push('_'))
}
}
#[cfg(test)]
mod tests {
use super::SnakeCase;
macro_rules! t {
($t:ident : $s1:expr => $s2:expr) => {
#[test]
fn $t() {
assert_eq!($s1.to_snake_case(), $s2)
}
}
}
t!(test1: "CamelCase" => "camel_case");
t!(test2: "This is Human case." => "this_is_human_case");
t!(test3: "MixedUP CamelCase, with some Spaces" => "mixed_up_camel_case_with_some_spaces");
t!(test4: "mixed_up snake_case with some _spaces" => "mixed_up_snake_case_with_some_spaces");
t!(test5: "kebab-case" => "kebab_case");
t!(test6: "SHOUTY_SNAKE_CASE" => "shouty_snake_case");
t!(test7: "snake_case" => "snake_case");
t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "this_contains_all_kinds_of_word_boundaries");
t!(test9: "XΣXΣ baffle" => "xσxς_baffle");
t!(test10: "XMLHttpRequest" => "xml_http_request");
}