@@ -53,20 +53,21 @@ impl MavProfile {
53
53
}
54
54
55
55
/// Go over all fields in the messages, and if you encounter an enum,
56
- /// update this enum with information about whether it is a bitmask, and what
57
- /// is the desired width of such.
56
+ /// which is a bitmask, set the bitmask size based on field size
58
57
fn update_enums ( mut self ) -> Self {
59
58
for msg in self . messages . values ( ) {
60
59
for field in & msg. fields {
61
60
if let Some ( enum_name) = & field. enumtype {
62
- // it is a bitmask
63
- if let Some ( "bitmask" ) = & field. display . as_deref ( ) {
64
- // find the corresponding enum
65
- for enm in self . enums . values_mut ( ) {
66
- if enm. name == * enum_name {
67
- // this is the right enum
68
- enm. bitfield = Some ( field. mavtype . rust_primitive_type ( ) ) ;
69
- }
61
+ // find the corresponding enum
62
+ if let Some ( enm) = self . enums . get_mut ( enum_name) {
63
+ // Handle legacy definition where bitmask is defined as display="bitmask"
64
+ if field. display == Some ( "bitmask" . to_string ( ) ) {
65
+ enm. bitmask = true ;
66
+ }
67
+
68
+ // it is a bitmask
69
+ if enm. bitmask {
70
+ enm. primitive = Some ( field. mavtype . rust_primitive_type ( ) ) ;
70
71
}
71
72
}
72
73
}
@@ -287,8 +288,11 @@ pub struct MavEnum {
287
288
pub name : String ,
288
289
pub description : Option < String > ,
289
290
pub entries : Vec < MavEnumEntry > ,
290
- /// If contains Some, the string represents the type witdh for bitflags
291
- pub bitfield : Option < String > ,
291
+ /// If contains Some, the string represents the primitive type (size) for bitflags.
292
+ /// If no fields use this enum, the bitmask is true, but primitive is None. In this case
293
+ /// regular enum is generated as primitive is unknown.
294
+ pub primitive : Option < String > ,
295
+ pub bitmask : bool
292
296
}
293
297
294
298
impl MavEnum {
@@ -333,7 +337,7 @@ impl MavEnum {
333
337
let tmp = TokenStream :: from_str ( & tmp_value. to_string ( ) ) . unwrap ( ) ;
334
338
value = quote ! ( #tmp) ;
335
339
} ;
336
- if self . bitfield . is_some ( ) {
340
+ if self . primitive . is_some ( ) {
337
341
quote ! {
338
342
#description
339
343
const #name = #value;
@@ -375,13 +379,13 @@ impl MavEnum {
375
379
let description = quote ! ( ) ;
376
380
377
381
let enum_def;
378
- if let Some ( width ) = self . bitfield . clone ( ) {
379
- let width = format_ident ! ( "{}" , width ) ;
382
+ if let Some ( primitive ) = self . primitive . clone ( ) {
383
+ let primitive = format_ident ! ( "{}" , primitive ) ;
380
384
enum_def = quote ! {
381
385
bitflags!{
382
386
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
383
387
#description
384
- pub struct #enum_name: #width {
388
+ pub struct #enum_name: #primitive {
385
389
#( #defs) *
386
390
}
387
391
}
@@ -1136,6 +1140,8 @@ pub fn parse_profile(
1136
1140
if attr. key . into_inner ( ) == b"name" {
1137
1141
mavenum. name = to_pascal_case ( attr. value ) ;
1138
1142
//mavenum.name = attr.value.clone();
1143
+ } else if attr. key . into_inner ( ) == b"bitmask" {
1144
+ mavenum. bitmask = true ;
1139
1145
}
1140
1146
}
1141
1147
Some ( & MavXmlElement :: Entry ) => {
@@ -1195,8 +1201,14 @@ pub fn parse_profile(
1195
1201
field. mavtype = MavType :: parse_type ( & r#type) . unwrap ( ) ;
1196
1202
}
1197
1203
b"enum" => {
1198
- field. enumtype = Some ( to_pascal_case ( attr. value ) ) ;
1199
- //field.enumtype = Some(attr.value.clone());
1204
+ field. enumtype = Some ( to_pascal_case ( & attr. value ) ) ;
1205
+
1206
+ // Update field display if enum is a bitmask
1207
+ if let Some ( e) = profile. enums . get ( field. enumtype . as_ref ( ) . unwrap ( ) ) {
1208
+ if e. bitmask {
1209
+ field. display = Some ( "bitmask" . to_string ( ) ) ;
1210
+ }
1211
+ }
1200
1212
}
1201
1213
b"display" => {
1202
1214
field. display =
0 commit comments