@@ -32,6 +32,7 @@ pub struct Config {
32
32
pub reexport_core_peripherals : bool ,
33
33
pub reexport_interrupt : bool ,
34
34
pub ident_formats : IdentFormats ,
35
+ pub ident_formats_theme : Option < IdentFormatsTheme > ,
35
36
pub base_address_shift : u64 ,
36
37
}
37
38
@@ -136,6 +137,26 @@ pub enum Case {
136
137
Snake ,
137
138
}
138
139
140
+ impl Case {
141
+ pub fn parse ( c : & str ) -> Result < Option < Self > , IdentFormatError > {
142
+ Ok ( match c {
143
+ "" | "unchanged" | "svd" => None ,
144
+ "p" | "pascal" | "type" => Some ( Case :: Pascal ) ,
145
+ "s" | "snake" | "lower" => Some ( Case :: Snake ) ,
146
+ "c" | "constant" | "upper" => Some ( Case :: Constant ) ,
147
+ _ => {
148
+ return Err ( IdentFormatError :: UnknownCase ( c. into ( ) ) ) ;
149
+ }
150
+ } )
151
+ }
152
+ }
153
+
154
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
155
+ pub enum IdentFormatError {
156
+ UnknownCase ( String ) ,
157
+ Other ,
158
+ }
159
+
139
160
#[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
140
161
#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize ) , serde( default ) ) ]
141
162
pub struct IdentFormat {
@@ -170,67 +191,99 @@ impl IdentFormat {
170
191
self . suffix = suffix. into ( ) ;
171
192
self
172
193
}
194
+ pub fn parse ( s : & str ) -> Result < Self , IdentFormatError > {
195
+ let mut f = s. split ( ":" ) ;
196
+ match ( f. next ( ) , f. next ( ) , f. next ( ) ) {
197
+ ( Some ( p) , Some ( c) , Some ( s) ) => {
198
+ let case = Case :: parse ( c) ?;
199
+ Ok ( Self {
200
+ case,
201
+ prefix : p. into ( ) ,
202
+ suffix : s. into ( ) ,
203
+ } )
204
+ }
205
+ ( Some ( p) , Some ( c) , None ) => {
206
+ let case = Case :: parse ( c) ?;
207
+ Ok ( Self {
208
+ case,
209
+ prefix : p. into ( ) ,
210
+ suffix : "" . into ( ) ,
211
+ } )
212
+ }
213
+ ( Some ( c) , None , None ) => {
214
+ let case = Case :: parse ( c) ?;
215
+ Ok ( Self {
216
+ case,
217
+ prefix : "" . into ( ) ,
218
+ suffix : "" . into ( ) ,
219
+ } )
220
+ }
221
+ _ => Err ( IdentFormatError :: Other ) ,
222
+ }
223
+ }
173
224
}
174
225
175
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
226
+ #[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
176
227
#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize ) , serde( default ) ) ]
177
228
pub struct IdentFormats ( HashMap < String , IdentFormat > ) ;
178
229
179
- impl Default for IdentFormats {
180
- fn default ( ) -> Self {
181
- let mut map = HashMap :: new ( ) ;
182
-
183
- map. insert ( "field_accessor" . into ( ) , IdentFormat :: default ( ) . snake_case ( ) ) ;
184
- map. insert (
185
- "field_reader" . into ( ) ,
186
- IdentFormat :: default ( ) . constant_case ( ) . suffix ( "_R" ) ,
187
- ) ;
188
- map. insert (
189
- "field_writer" . into ( ) ,
190
- IdentFormat :: default ( ) . constant_case ( ) . suffix ( "_W" ) ,
191
- ) ;
192
- map. insert (
193
- "enum_name" . into ( ) ,
194
- IdentFormat :: default ( ) . constant_case ( ) . suffix ( "_A" ) ,
195
- ) ;
196
- map. insert (
197
- "enum_write_name" . into ( ) ,
198
- IdentFormat :: default ( ) . constant_case ( ) . suffix ( "_AW" ) ,
199
- ) ;
200
- map. insert ( "enum_value" . into ( ) , IdentFormat :: default ( ) . constant_case ( ) ) ;
201
- map. insert (
202
- "enum_value_accessor" . into ( ) ,
203
- IdentFormat :: default ( ) . snake_case ( ) ,
204
- ) ;
205
- map. insert ( "interrupt" . into ( ) , IdentFormat :: default ( ) . constant_case ( ) ) ;
206
- map. insert ( "cluster" . into ( ) , IdentFormat :: default ( ) . constant_case ( ) ) ;
207
- map. insert (
208
- "cluster_accessor" . into ( ) ,
209
- IdentFormat :: default ( ) . snake_case ( ) ,
210
- ) ;
211
- map. insert ( "cluster_mod" . into ( ) , IdentFormat :: default ( ) . snake_case ( ) ) ;
212
- map. insert ( "register" . into ( ) , IdentFormat :: default ( ) . constant_case ( ) ) ;
213
- map. insert (
214
- "register_spec" . into ( ) ,
215
- IdentFormat :: default ( ) . pascal_case ( ) . suffix ( "_SPEC" ) ,
216
- ) ;
217
- map. insert (
218
- "register_accessor" . into ( ) ,
219
- IdentFormat :: default ( ) . snake_case ( ) ,
220
- ) ;
221
- map. insert ( "register_mod" . into ( ) , IdentFormat :: default ( ) . snake_case ( ) ) ;
222
- map. insert ( "peripheral" . into ( ) , IdentFormat :: default ( ) . constant_case ( ) ) ;
223
- map. insert (
224
- "peripheral_singleton" . into ( ) ,
225
- IdentFormat :: default ( ) . constant_case ( ) ,
226
- ) ;
227
- map. insert ( "peripheral_mod" . into ( ) , IdentFormat :: default ( ) . snake_case ( ) ) ;
228
- map. insert (
229
- "peripheral_feature" . into ( ) ,
230
- IdentFormat :: default ( ) . snake_case ( ) ,
231
- ) ;
232
-
233
- Self ( map)
230
+ impl IdentFormats {
231
+ fn common ( ) -> Self {
232
+ let snake = IdentFormat :: default ( ) . snake_case ( ) ;
233
+ Self ( HashMap :: from ( [
234
+ ( "field_accessor" . into ( ) , snake. clone ( ) ) ,
235
+ ( "register_accessor" . into ( ) , snake. clone ( ) ) ,
236
+ ( "enum_value_accessor" . into ( ) , snake. clone ( ) ) ,
237
+ ( "cluster_accessor" . into ( ) , snake. clone ( ) ) ,
238
+ ( "register_mod" . into ( ) , snake. clone ( ) ) ,
239
+ ( "cluster_mod" . into ( ) , snake. clone ( ) ) ,
240
+ ( "peripheral_mod" . into ( ) , snake. clone ( ) ) ,
241
+ ( "peripheral_feature" . into ( ) , snake) ,
242
+ ] ) )
243
+ }
244
+
245
+ pub fn default_theme ( ) -> Self {
246
+ let mut map = Self :: common ( ) ;
247
+
248
+ let pascal = IdentFormat :: default ( ) . pascal_case ( ) ;
249
+ map. extend ( [
250
+ ( "field_reader" . into ( ) , pascal. clone ( ) . suffix ( "R" ) ) ,
251
+ ( "field_writer" . into ( ) , pascal. clone ( ) . suffix ( "W" ) ) ,
252
+ ( "enum_name" . into ( ) , pascal. clone ( ) ) ,
253
+ ( "enum_write_name" . into ( ) , pascal. clone ( ) . suffix ( "WO" ) ) ,
254
+ ( "enum_value" . into ( ) , pascal. clone ( ) ) ,
255
+ ( "interrupt" . into ( ) , IdentFormat :: default ( ) ) ,
256
+ ( "register" . into ( ) , pascal. clone ( ) ) ,
257
+ ( "cluster" . into ( ) , pascal. clone ( ) ) ,
258
+ ( "register_spec" . into ( ) , pascal. clone ( ) . suffix ( "Spec" ) ) ,
259
+ ( "peripheral" . into ( ) , pascal) ,
260
+ (
261
+ "peripheral_singleton" . into ( ) ,
262
+ IdentFormat :: default ( ) . snake_case ( ) ,
263
+ ) ,
264
+ ] ) ;
265
+
266
+ map
267
+ }
268
+ pub fn legacy_theme ( ) -> Self {
269
+ let mut map = Self :: common ( ) ;
270
+
271
+ let constant = IdentFormat :: default ( ) . constant_case ( ) ;
272
+ map. extend ( [
273
+ ( "field_reader" . into ( ) , constant. clone ( ) . suffix ( "_R" ) ) ,
274
+ ( "field_writer" . into ( ) , constant. clone ( ) . suffix ( "_W" ) ) ,
275
+ ( "enum_name" . into ( ) , constant. clone ( ) . suffix ( "_A" ) ) ,
276
+ ( "enum_write_name" . into ( ) , constant. clone ( ) . suffix ( "_AW" ) ) ,
277
+ ( "enum_value" . into ( ) , constant. clone ( ) ) ,
278
+ ( "interrupt" . into ( ) , constant. clone ( ) ) ,
279
+ ( "cluster" . into ( ) , constant. clone ( ) ) ,
280
+ ( "register" . into ( ) , constant. clone ( ) ) ,
281
+ ( "register_spec" . into ( ) , constant. clone ( ) . suffix ( "_SPEC" ) ) ,
282
+ ( "peripheral" . into ( ) , constant. clone ( ) ) ,
283
+ ( "peripheral_singleton" . into ( ) , constant) ,
284
+ ] ) ;
285
+
286
+ map
234
287
}
235
288
}
236
289
@@ -245,3 +298,13 @@ impl DerefMut for IdentFormats {
245
298
& mut self . 0
246
299
}
247
300
}
301
+
302
+ #[ cfg_attr(
303
+ feature = "serde" ,
304
+ derive( serde:: Deserialize ) ,
305
+ serde( rename_all = "lowercase" )
306
+ ) ]
307
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
308
+ pub enum IdentFormatsTheme {
309
+ Legacy ,
310
+ }
0 commit comments