allow renaming rules for C enums, structs, and typedefs#3191
Open
nyurik wants to merge 2 commits intorust-lang:mainfrom
Open
allow renaming rules for C enums, structs, and typedefs#3191nyurik wants to merge 2 commits intorust-lang:mainfrom
nyurik wants to merge 2 commits intorust-lang:mainfrom
Conversation
248e590 to
f865a04
Compare
f902679 to
f930c2e
Compare
```rust
// true to enable debug output as warnings
let mut ren = Renamer::new(true);
// rename a single item, e.g. a struct, enum, or a typedef
ren.rename_item("my_struct", "MyStruct");
// rename an enum and its values
rename_enum!(
ren,
"my_enum" => "MyEnum", // rename the enum itself
remove: "^I_SAID_", // optionally any number of "remove" regexes
remove: "_ENUM$",
case: Pascal, // optionally set case convert, defaults to "PascalCase"
"MV_IT" => "Value1", // rename a specific value after pattern removal
"MV_IT2" => "Value2", // more specific value renames
);
let bindings = Builder::default()
// in real code, use .header("path/to/header.h")
.header_contents("test.h", r#"
struct my_struct {
int a;
};
enum my_enum {
I_SAID_YES_ENUM,
I_SAID_NO_ENUM,
I_SAID_MV_IT_ENUM,
I_SAID_MV_IT2_ENUM,
};
"#)
.rustified_enum(ren.get_regex_str())
.parse_callbacks(Box::new(ren))
.generate().unwrap();
}
/////////// generated code
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct MyStruct {
pub a: ::std::os::raw::c_int,
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum MyEnum {
Yes = 0,
No = 1,
Value1 = 2,
Value2 = 3,
}
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add utilities to rename, change case, and fix Rust code generated from the C headers.
Renamerimplements a bindgen callback trait. It handles struct/enum/typedef type renames with astring->stringhashmap. Additionally, it can rename the enum variant names by removing regex matches, and change identifier case toPascalCaseto be consistent with the Rust canonical style.This is a port of the https://crates.io/crates/bindgen_helpers into a built-in feature, as it is fairly difficult to maintain a standalone bindgen plugin due to numerous dependency-hell-ish issues I discovered when using it.