@@ -61,6 +61,41 @@ pub struct Config {
61
61
pub log_level : Option < String > ,
62
62
}
63
63
64
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
65
+ pub enum Case {
66
+ Constant ,
67
+ Pascal ,
68
+ Snake ,
69
+ }
70
+
71
+ impl Case {
72
+ pub fn cow_to_case < ' a > ( & self , cow : Cow < ' a , str > ) -> Cow < ' a , str > {
73
+ match self {
74
+ Self :: Constant => match cow {
75
+ Cow :: Borrowed ( s) if s. is_constant_case ( ) => cow,
76
+ _ => cow. to_constant_case ( ) . into ( ) ,
77
+ } ,
78
+ Self :: Pascal => match cow {
79
+ Cow :: Borrowed ( s) if s. is_pascal_case ( ) => cow,
80
+ _ => cow. to_pascal_case ( ) . into ( ) ,
81
+ } ,
82
+ Self :: Snake => match cow {
83
+ Cow :: Borrowed ( s) if s. is_snake_case ( ) => cow,
84
+ _ => cow. to_snake_case ( ) . into ( ) ,
85
+ } ,
86
+ }
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
+ }
97
+ }
98
+
64
99
fn current_dir ( ) -> PathBuf {
65
100
PathBuf :: from ( "." )
66
101
}
@@ -92,9 +127,10 @@ impl Default for Config {
92
127
#[ allow( clippy:: upper_case_acronyms) ]
93
128
#[ allow( non_camel_case_types) ]
94
129
#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize ) ) ]
95
- #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
130
+ #[ derive( Clone , Copy , PartialEq , Eq , Debug , Default ) ]
96
131
pub enum Target {
97
132
#[ cfg_attr( feature = "serde" , serde( rename = "cortex-m" ) ) ]
133
+ #[ default]
98
134
CortexM ,
99
135
#[ cfg_attr( feature = "serde" , serde( rename = "msp430" ) ) ]
100
136
Msp430 ,
@@ -122,32 +158,21 @@ impl Target {
122
158
}
123
159
}
124
160
125
- impl Default for Target {
126
- fn default ( ) -> Self {
127
- Self :: CortexM
128
- }
129
- }
130
-
131
161
#[ cfg_attr(
132
162
feature = "serde" ,
133
163
derive( serde:: Deserialize ) ,
134
164
serde( rename_all = "lowercase" )
135
165
) ]
136
- #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
166
+ #[ derive( Clone , Copy , PartialEq , Eq , Debug , Default ) ]
137
167
pub enum SourceType {
168
+ #[ default]
138
169
Xml ,
139
170
#[ cfg( feature = "yaml" ) ]
140
171
Yaml ,
141
172
#[ cfg( feature = "json" ) ]
142
173
Json ,
143
174
}
144
175
145
- impl Default for SourceType {
146
- fn default ( ) -> Self {
147
- Self :: Xml
148
- }
149
- }
150
-
151
176
impl SourceType {
152
177
/// Make a new [`Source`] from a given extension.
153
178
pub fn from_extension ( s : & str ) -> Option < Self > {
@@ -203,41 +228,31 @@ pub trait ToSanitizedCase {
203
228
204
229
impl ToSanitizedCase for str {
205
230
fn to_sanitized_pascal_case ( & self ) -> Cow < str > {
206
- let s = self . replace ( BLACKLIST_CHARS , "" ) ;
207
-
208
- match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
209
- '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
210
- Cow :: from ( format ! ( "_{}" , s. to_pascal_case( ) ) )
211
- }
212
- _ => Cow :: from ( s. to_pascal_case ( ) ) ,
231
+ let s = Case :: Pascal . sanitize ( self ) ;
232
+ if s. as_bytes ( ) . get ( 0 ) . unwrap_or ( & 0 ) . is_ascii_digit ( ) {
233
+ Cow :: from ( format ! ( "_{}" , s) )
234
+ } else {
235
+ s
213
236
}
214
237
}
215
238
fn to_sanitized_constant_case ( & self ) -> Cow < str > {
216
- let s = self . replace ( BLACKLIST_CHARS , "" ) ;
217
-
218
- match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
219
- '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
220
- Cow :: from ( format ! ( "_{}" , s. to_constant_case( ) ) )
221
- }
222
- _ => Cow :: from ( s. to_constant_case ( ) ) ,
239
+ let s = Case :: Constant . sanitize ( self ) ;
240
+ if s. as_bytes ( ) . get ( 0 ) . unwrap_or ( & 0 ) . is_ascii_digit ( ) {
241
+ Cow :: from ( format ! ( "_{}" , s) )
242
+ } else {
243
+ s
223
244
}
224
245
}
225
246
fn to_sanitized_not_keyword_snake_case ( & self ) -> Cow < str > {
226
247
const INTERNALS : [ & str ; 4 ] = [ "set_bit" , "clear_bit" , "bit" , "bits" ] ;
227
248
228
- let s = self . replace ( BLACKLIST_CHARS , "" ) ;
229
- match s. chars ( ) . next ( ) . unwrap_or ( '\0' ) {
230
- '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
231
- format ! ( "_{}" , s. to_snake_case( ) ) . into ( )
232
- }
233
- _ => {
234
- let s = Cow :: from ( s. to_snake_case ( ) ) ;
235
- if INTERNALS . contains ( & s. as_ref ( ) ) {
236
- s + "_"
237
- } else {
238
- s
239
- }
240
- }
249
+ let s = Case :: Snake . sanitize ( self ) ;
250
+ if s. as_bytes ( ) . get ( 0 ) . unwrap_or ( & 0 ) . is_ascii_digit ( ) {
251
+ format ! ( "_{}" , s) . into ( )
252
+ } else if INTERNALS . contains ( & s. as_ref ( ) ) {
253
+ s + "_"
254
+ } else {
255
+ s
241
256
}
242
257
}
243
258
}
0 commit comments