@@ -69,31 +69,6 @@ pub enum Case {
69
69
}
70
70
71
71
impl Case {
72
- pub fn to_case < ' a > ( & self , s : & ' a str ) -> Cow < ' a , str > {
73
- match self {
74
- Self :: Constant => {
75
- if s. is_constant_case ( ) {
76
- s. into ( )
77
- } else {
78
- s. to_constant_case ( ) . into ( )
79
- }
80
- }
81
- Self :: Pascal => {
82
- if s. is_pascal_case ( ) {
83
- s. into ( )
84
- } else {
85
- s. to_pascal_case ( ) . into ( )
86
- }
87
- }
88
- Self :: Snake => {
89
- if s. is_snake_case ( ) {
90
- s. into ( )
91
- } else {
92
- s. to_snake_case ( ) . into ( )
93
- }
94
- }
95
- }
96
- }
97
72
pub fn cow_to_case < ' a > ( & self , cow : Cow < ' a , str > ) -> Cow < ' a , str > {
98
73
match self {
99
74
Self :: Constant => match cow {
@@ -110,6 +85,15 @@ impl Case {
110
85
} ,
111
86
}
112
87
}
88
+ pub fn sanitize < ' a > ( & self , s : & ' a str ) -> Cow < ' a , str > {
89
+ let s = if s. contains ( BLACKLIST_CHARS ) {
90
+ Cow :: Owned ( s. replace ( BLACKLIST_CHARS , "" ) )
91
+ } else {
92
+ s. into ( )
93
+ } ;
94
+
95
+ self . cow_to_case ( s)
96
+ }
113
97
}
114
98
115
99
fn current_dir ( ) -> PathBuf {
@@ -244,48 +228,31 @@ pub trait ToSanitizedCase {
244
228
245
229
impl ToSanitizedCase for str {
246
230
fn to_sanitized_pascal_case ( & self ) -> Cow < str > {
247
- let s = if self . contains ( BLACKLIST_CHARS ) {
248
- Cow :: Owned ( self . replace ( BLACKLIST_CHARS , "" ) )
249
- } else {
250
- self . into ( )
251
- } ;
252
-
231
+ let s = Case :: Pascal . sanitize ( self ) ;
253
232
if s. as_bytes ( ) . get ( 0 ) . unwrap_or ( & 0 ) . is_ascii_digit ( ) {
254
- Cow :: from ( format ! ( "_{}" , Case :: Pascal . to_case ( & s ) ) )
233
+ Cow :: from ( format ! ( "_{}" , s ) )
255
234
} else {
256
- Case :: Pascal . cow_to_case ( s )
235
+ s
257
236
}
258
237
}
259
238
fn to_sanitized_constant_case ( & self ) -> Cow < str > {
260
- let s = if self . contains ( BLACKLIST_CHARS ) {
261
- Cow :: Owned ( self . replace ( BLACKLIST_CHARS , "" ) )
262
- } else {
263
- self . into ( )
264
- } ;
265
-
239
+ let s = Case :: Constant . sanitize ( self ) ;
266
240
if s. as_bytes ( ) . get ( 0 ) . unwrap_or ( & 0 ) . is_ascii_digit ( ) {
267
- Cow :: from ( format ! ( "_{}" , Case :: Constant . to_case ( & s ) ) )
241
+ Cow :: from ( format ! ( "_{}" , s ) )
268
242
} else {
269
- Case :: Constant . cow_to_case ( s )
243
+ s
270
244
}
271
245
}
272
246
fn to_sanitized_not_keyword_snake_case ( & self ) -> Cow < str > {
273
247
const INTERNALS : [ & str ; 4 ] = [ "set_bit" , "clear_bit" , "bit" , "bits" ] ;
274
248
275
- let s = if self . contains ( BLACKLIST_CHARS ) {
276
- Cow :: Owned ( self . replace ( BLACKLIST_CHARS , "" ) )
277
- } else {
278
- self . into ( )
279
- } ;
249
+ let s = Case :: Snake . sanitize ( self ) ;
280
250
if s. as_bytes ( ) . get ( 0 ) . unwrap_or ( & 0 ) . is_ascii_digit ( ) {
281
- format ! ( "_{}" , Case :: Snake . to_case( & s) ) . into ( )
251
+ format ! ( "_{}" , s) . into ( )
252
+ } else if INTERNALS . contains ( & s. as_ref ( ) ) {
253
+ s + "_"
282
254
} else {
283
- let s = Case :: Snake . cow_to_case ( s) ;
284
- if INTERNALS . contains ( & s. as_ref ( ) ) {
285
- s + "_"
286
- } else {
287
- s
288
- }
255
+ s
289
256
}
290
257
}
291
258
}
0 commit comments