1
1
#[ allow( deprecated, unused_imports) ]
2
2
use std:: ascii:: AsciiExt ;
3
3
4
+ use heck:: {
5
+ ToKebabCase , ToLowerCamelCase , ToShoutyKebabCase , ToShoutySnakeCase , ToSnakeCase , ToTrainCase ,
6
+ ToUpperCamelCase ,
7
+ } ;
8
+
4
9
use self :: RenameRule :: * ;
5
10
6
11
/// The different possible ways to change case of fields in a struct, or variants in an enum.
@@ -26,78 +31,56 @@ pub enum RenameRule {
26
31
KebabCase ,
27
32
/// Rename direct children to "SCREAMING-KEBAB-CASE" style.
28
33
ScreamingKebabCase ,
34
+
35
+ /// Rename direct children to "Train-Case" style.
36
+ TrainCase ,
29
37
}
30
38
31
- pub static RENAME_RULES : & [ ( & str , RenameRule ) ] = & [
32
- ( "lowercase" , LowerCase ) ,
33
- ( "UPPERCASE" , UpperCase ) ,
34
- ( "PascalCase" , PascalCase ) ,
35
- ( "camelCase" , CamelCase ) ,
36
- ( "snake_case" , SnakeCase ) ,
37
- ( "SCREAMING_SNAKE_CASE" , ScreamingSnakeCase ) ,
38
- ( "kebab-case" , KebabCase ) ,
39
- ( "SCREAMING-KEBAB-CASE" , ScreamingKebabCase ) ,
39
+ pub const RENAME_RULES : & [ & str ] = & [
40
+ "lowercase" ,
41
+ "UPPERCASE" ,
42
+ "PascalCase" ,
43
+ "camelCase" ,
44
+ "snake_case" ,
45
+ "SCREAMING_SNAKE_CASE" ,
46
+ "kebab-case" ,
47
+ "SCREAMING-KEBAB-CASE" ,
48
+ "Train-Case" ,
40
49
] ;
41
50
42
51
impl RenameRule {
43
- /// Apply a renaming rule to an enum variant, returning the version expected in the source.
44
- pub fn apply_to_variant ( & self , variant : & str ) -> String {
45
- match * self {
46
- PascalCase => variant. to_owned ( ) ,
47
- LowerCase => variant. to_ascii_lowercase ( ) ,
48
- UpperCase => variant. to_ascii_uppercase ( ) ,
49
- CamelCase => variant[ ..1 ] . to_ascii_lowercase ( ) + & variant[ 1 ..] ,
50
- SnakeCase => {
51
- let mut snake = String :: new ( ) ;
52
- for ( i, ch) in variant. char_indices ( ) {
53
- if i > 0 && ch. is_uppercase ( ) {
54
- snake. push ( '_' ) ;
55
- }
56
- snake. push ( ch. to_ascii_lowercase ( ) ) ;
57
- }
58
- snake
59
- }
60
- ScreamingSnakeCase => SnakeCase . apply_to_variant ( variant) . to_ascii_uppercase ( ) ,
61
- KebabCase => SnakeCase . apply_to_variant ( variant) . replace ( '_' , "-" ) ,
62
- ScreamingKebabCase => ScreamingSnakeCase
63
- . apply_to_variant ( variant)
64
- . replace ( '_' , "-" ) ,
52
+ pub fn from_str ( rule : & str ) -> Option < RenameRule > {
53
+ match rule {
54
+ "lowercase" => Some ( LowerCase ) ,
55
+ "UPPERCASE" => Some ( UpperCase ) ,
56
+ "PascalCase" => Some ( PascalCase ) ,
57
+ "camelCase" => Some ( CamelCase ) ,
58
+ "snake_case" => Some ( SnakeCase ) ,
59
+ "SCREAMING_SNAKE_CASE" => Some ( ScreamingSnakeCase ) ,
60
+ "kebab-case" => Some ( KebabCase ) ,
61
+ "SCREAMING-KEBAB-CASE" => Some ( ScreamingKebabCase ) ,
62
+ "Train-Case" => Some ( TrainCase ) ,
63
+ _ => None ,
65
64
}
66
65
}
67
-
68
- /// Apply a renaming rule to a struct field, returning the version expected in the source.
69
- pub fn apply_to_field ( & self , field : & str ) -> String {
66
+ /// Apply a renaming rule to an enum or struct field, returning the version expected in the source.
67
+ pub fn apply_to_field ( & self , variant : & str ) -> String {
70
68
match * self {
71
- LowerCase | SnakeCase => field. to_owned ( ) ,
72
- UpperCase => field. to_ascii_uppercase ( ) ,
73
- PascalCase => {
74
- let mut pascal = String :: new ( ) ;
75
- let mut capitalize = true ;
76
- for ch in field. chars ( ) {
77
- if ch == '_' {
78
- capitalize = true ;
79
- } else if capitalize {
80
- pascal. push ( ch. to_ascii_uppercase ( ) ) ;
81
- capitalize = false ;
82
- } else {
83
- pascal. push ( ch) ;
84
- }
85
- }
86
- pascal
87
- }
88
- CamelCase => {
89
- let pascal = PascalCase . apply_to_field ( field) ;
90
- pascal[ ..1 ] . to_ascii_lowercase ( ) + & pascal[ 1 ..]
91
- }
92
- ScreamingSnakeCase => field. to_ascii_uppercase ( ) ,
93
- KebabCase => field. replace ( '_' , "-" ) ,
94
- ScreamingKebabCase => ScreamingSnakeCase . apply_to_field ( field) . replace ( '_' , "-" ) ,
69
+ LowerCase => variant. to_lowercase ( ) ,
70
+ UpperCase => variant. to_uppercase ( ) ,
71
+ PascalCase => variant. to_upper_camel_case ( ) ,
72
+ CamelCase => variant. to_lower_camel_case ( ) ,
73
+ SnakeCase => variant. to_snake_case ( ) ,
74
+ ScreamingSnakeCase => variant. to_shouty_snake_case ( ) ,
75
+ KebabCase => variant. to_kebab_case ( ) ,
76
+ ScreamingKebabCase => variant. to_shouty_kebab_case ( ) ,
77
+ TrainCase => variant. to_train_case ( ) ,
95
78
}
96
79
}
97
80
}
98
81
99
82
#[ test]
100
- fn rename_variants ( ) {
83
+ fn rename_field ( ) {
101
84
for & ( original, lower, upper, camel, snake, screaming, kebab, screaming_kebab) in & [
102
85
(
103
86
"Outcome" , "outcome" , "OUTCOME" , "outcome" , "outcome" , "OUTCOME" , "outcome" , "OUTCOME" ,
@@ -115,42 +98,11 @@ fn rename_variants() {
115
98
( "A" , "a" , "A" , "a" , "a" , "A" , "a" , "A" ) ,
116
99
( "Z42" , "z42" , "Z42" , "z42" , "z42" , "Z42" , "z42" , "Z42" ) ,
117
100
] {
118
- assert_eq ! ( LowerCase . apply_to_variant( original) , lower) ;
119
- assert_eq ! ( UpperCase . apply_to_variant( original) , upper) ;
120
- assert_eq ! ( PascalCase . apply_to_variant( original) , original) ;
121
- assert_eq ! ( CamelCase . apply_to_variant( original) , camel) ;
122
- assert_eq ! ( SnakeCase . apply_to_variant( original) , snake) ;
123
- assert_eq ! ( ScreamingSnakeCase . apply_to_variant( original) , screaming) ;
124
- assert_eq ! ( KebabCase . apply_to_variant( original) , kebab) ;
125
- assert_eq ! (
126
- ScreamingKebabCase . apply_to_variant( original) ,
127
- screaming_kebab
128
- ) ;
129
- }
130
- }
131
-
132
- #[ test]
133
- fn rename_fields ( ) {
134
- for & ( original, upper, pascal, camel, screaming, kebab, screaming_kebab) in & [
135
- (
136
- "outcome" , "OUTCOME" , "Outcome" , "outcome" , "OUTCOME" , "outcome" , "OUTCOME" ,
137
- ) ,
138
- (
139
- "very_tasty" ,
140
- "VERY_TASTY" ,
141
- "VeryTasty" ,
142
- "veryTasty" ,
143
- "VERY_TASTY" ,
144
- "very-tasty" ,
145
- "VERY-TASTY" ,
146
- ) ,
147
- ( "a" , "A" , "A" , "a" , "A" , "a" , "A" ) ,
148
- ( "z42" , "Z42" , "Z42" , "z42" , "Z42" , "z42" , "Z42" ) ,
149
- ] {
101
+ assert_eq ! ( LowerCase . apply_to_field( original) , lower) ;
150
102
assert_eq ! ( UpperCase . apply_to_field( original) , upper) ;
151
- assert_eq ! ( PascalCase . apply_to_field( original) , pascal ) ;
103
+ assert_eq ! ( PascalCase . apply_to_field( original) , original ) ;
152
104
assert_eq ! ( CamelCase . apply_to_field( original) , camel) ;
153
- assert_eq ! ( SnakeCase . apply_to_field( original) , original ) ;
105
+ assert_eq ! ( SnakeCase . apply_to_field( original) , snake ) ;
154
106
assert_eq ! ( ScreamingSnakeCase . apply_to_field( original) , screaming) ;
155
107
assert_eq ! ( KebabCase . apply_to_field( original) , kebab) ;
156
108
assert_eq ! ( ScreamingKebabCase . apply_to_field( original) , screaming_kebab) ;
0 commit comments