Crates.io | bindgen_helpers |
lib.rs | bindgen_helpers |
version | |
source | src |
created_at | 2024-11-02 04:52:31.646993 |
updated_at | 2024-12-10 23:39:15.394653 |
description | Utilities to rename, change case, and fix Rust code generated by bindgen from C headers |
homepage | |
repository | https://github.com/nyurik/bindgen_helpers |
max_upload_size | |
id | 1432618 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Utilities to rename, change case, and fix Rust code generated from the C headers using 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.
# 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"
// 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 supported by the convert_case
crate.
make
.
Install it with cargo install just
.just
.just test
.git push
, it will run a few validations, including cargo fmt
, cargo clippy
, and cargo test
.
Use git push --no-verify
to skip these checks.Licensed under either of
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.