Skip to content

Commit 3efd58e

Browse files
committed
Fix bitmask parsing
1 parent d0abd08 commit 3efd58e

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

mavlink-bindgen/src/parser.rs

+30-18
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,21 @@ impl MavProfile {
5353
}
5454

5555
/// 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
5857
fn update_enums(mut self) -> Self {
5958
for msg in self.messages.values() {
6059
for field in &msg.fields {
6160
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());
7071
}
7172
}
7273
}
@@ -287,8 +288,11 @@ pub struct MavEnum {
287288
pub name: String,
288289
pub description: Option<String>,
289290
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
292296
}
293297

294298
impl MavEnum {
@@ -333,7 +337,7 @@ impl MavEnum {
333337
let tmp = TokenStream::from_str(&tmp_value.to_string()).unwrap();
334338
value = quote!(#tmp);
335339
};
336-
if self.bitfield.is_some() {
340+
if self.primitive.is_some() {
337341
quote! {
338342
#description
339343
const #name = #value;
@@ -375,13 +379,13 @@ impl MavEnum {
375379
let description = quote!();
376380

377381
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);
380384
enum_def = quote! {
381385
bitflags!{
382386
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
383387
#description
384-
pub struct #enum_name: #width {
388+
pub struct #enum_name: #primitive {
385389
#(#defs)*
386390
}
387391
}
@@ -1136,6 +1140,8 @@ pub fn parse_profile(
11361140
if attr.key.into_inner() == b"name" {
11371141
mavenum.name = to_pascal_case(attr.value);
11381142
//mavenum.name = attr.value.clone();
1143+
} else if attr.key.into_inner() == b"bitmask" {
1144+
mavenum.bitmask = true;
11391145
}
11401146
}
11411147
Some(&MavXmlElement::Entry) => {
@@ -1195,8 +1201,14 @@ pub fn parse_profile(
11951201
field.mavtype = MavType::parse_type(&r#type).unwrap();
11961202
}
11971203
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+
}
12001212
}
12011213
b"display" => {
12021214
field.display =

0 commit comments

Comments
 (0)