https://github.com/nyurik/bindgen_helpers
Utilities to rename, change case, and fix Rust code generated by bindgen from C headers
https://github.com/nyurik/bindgen_helpers
bindgen rust-ffi-bindings
Last synced: 19 days ago
JSON representation
Utilities to rename, change case, and fix Rust code generated by bindgen from C headers
- Host: GitHub
- URL: https://github.com/nyurik/bindgen_helpers
- Owner: nyurik
- License: apache-2.0
- Created: 2024-11-01T19:52:12.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-04-09T05:26:17.000Z (21 days ago)
- Last Synced: 2025-04-09T06:27:29.461Z (21 days ago)
- Topics: bindgen, rust-ffi-bindings
- Language: Rust
- Homepage: https://docs.rs/bindgen_helpers/latest/bindgen_helpers/
- Size: 36.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Rust bindgen helpers
[](https://github.com/nyurik/bindgen_helpers)
[](https://crates.io/crates/bindgen_helpers)
[](https://docs.rs/bindgen_helpers)
[](https://github.com/nyurik/bindgen_helpers/blob/main/LICENSE-APACHE)
[](https://github.com/nyurik/bindgen_helpers/actions)
[](https://app.codecov.io/gh/nyurik/bindgen_helpers)Utilities to rename, change case, and fix Rust code generated from the C headers using [bindgen](https://rust-lang.github.io/rust-bindgen/).
`Renamer` implements a bindgen callback trait, and currently handles struct/enum/typedef type renames with a `string->string` hashmap.
Additionally, it can rename the enum variant names by removing regex matches, and change identifier case to `PascalCase` to be consistent with the Rust canonical style.## Usage
```toml
# Cargo.toml
[build-dependencies]
# Bindgen_helpers re-exports all of bindgen's public API at the root level
# Do not use bindgen directly to avoid some version conflicts
bindgen_helpers = "0.3"
``````rust
// build.rs
use bindgen_helpers::{Builder, Renamer, rename_enum};fn main() {
// true to enable debug output as warnings
let mut renamer = Renamer::new(true);// rename a single item, e.g. a struct, enum, or a typedef
renamer.rename_item("my_struct", "MyStruct");// rename an enum and its values
rename_enum!(
renamer,
"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,
};"#)
// note that generated regex str includes all the renames, not just enums
.rustified_enum(renamer.get_regex_str())
.parse_callbacks(Box::new(renamer))
.generate().unwrap();
}//
// This is the approximate code that would be generated by the above:
//#[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,
}```
See the list of all [case variants](https://docs.rs/convert_case/latest/convert_case/enum.Case.html) supported by the `convert_case` crate.
## Development
* This project is easier to develop with [just](https://github.com/casey/just#readme), a modern alternative to `make`.
Install it with `cargo install just`.
* To get a list of available commands, run `just`.
* To run tests, use `just test`.## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or )
* MIT license ([LICENSE-MIT](LICENSE-MIT) or )
at your option.### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the
Apache-2.0 license, shall be dual licensed as above, without any
additional terms or conditions.