From e3c15ff743f482cb869fa2fdf278a8371f4b7d17 Mon Sep 17 00:00:00 2001 From: kavoor Date: Wed, 13 Apr 2022 01:34:53 -0700 Subject: [PATCH] Support rename rule for union body members --- src/bindgen/config.rs | 5 +++++ src/bindgen/ir/enumeration.rs | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 3560bb2c9..d0dbf2205 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -559,6 +559,10 @@ impl StructConfig { pub struct EnumConfig { /// The rename rule to apply to the name of enum variants pub rename_variants: RenameRule, + /// The rename rule to apply to the names of the union fields in C/C++ + /// generated from the Rust enum. Applied before rename_variants + /// rename rule. Defaults to SnakeCase. + pub rename_variant_name_fields: RenameRule, /// Whether to add a `Sentinel` value at the end of every enum /// This is useful in Gecko for IPC serialization pub add_sentinel: bool, @@ -600,6 +604,7 @@ impl Default for EnumConfig { fn default() -> EnumConfig { EnumConfig { rename_variants: RenameRule::None, + rename_variant_name_fields: RenameRule::SnakeCase, add_sentinel: false, prefix_with_name: false, derive_helper_methods: false, diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index aa19bff7b..a9b1080b5 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -149,11 +149,12 @@ impl EnumVariant { if let Some(b) = enum_annotations.bool("derive-ostream") { annotations.add_default("derive-ostream", AnnotationValue::Bool(b)); } + let body_rule = config.enumeration.rename_variant_name_fields; let body = match variant.fields { syn::Fields::Unit => VariantBody::Empty(annotations), syn::Fields::Named(ref fields) => { let path = Path::new(format!("{}_Body", variant.ident)); - let name = RenameRule::SnakeCase + let name = body_rule .apply( &variant.ident.unraw().to_string(), IdentifierType::StructMember, @@ -179,7 +180,7 @@ impl EnumVariant { } syn::Fields::Unnamed(ref fields) => { let path = Path::new(format!("{}_Body", variant.ident)); - let name = RenameRule::SnakeCase + let name = body_rule .apply( &variant.ident.unraw().to_string(), IdentifierType::StructMember,