{"id":18606667,"url":"https://github.com/nyurik/bindgen_helpers","last_synced_at":"2025-04-11T06:25:09.847Z","repository":{"id":260723031,"uuid":"882062050","full_name":"nyurik/bindgen_helpers","owner":"nyurik","description":"Utilities to rename, change case, and fix Rust code generated by bindgen from C headers","archived":false,"fork":false,"pushed_at":"2025-04-09T05:26:17.000Z","size":37,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T06:27:29.461Z","etag":null,"topics":["bindgen","rust-ffi-bindings"],"latest_commit_sha":null,"homepage":"https://docs.rs/bindgen_helpers/latest/bindgen_helpers/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nyurik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T19:52:12.000Z","updated_at":"2025-04-09T05:26:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"7c632f4e-a064-4c03-a3dc-2477d5977697","html_url":"https://github.com/nyurik/bindgen_helpers","commit_stats":null,"previous_names":["nyurik/bindgen_helpers"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyurik%2Fbindgen_helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyurik%2Fbindgen_helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyurik%2Fbindgen_helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nyurik%2Fbindgen_helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nyurik","download_url":"https://codeload.github.com/nyurik/bindgen_helpers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247993996,"owners_count":21030045,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bindgen","rust-ffi-bindings"],"created_at":"2024-11-07T02:26:36.875Z","updated_at":"2025-04-11T06:25:09.832Z","avatar_url":"https://github.com/nyurik.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust bindgen helpers\n\n[![GitHub](https://img.shields.io/badge/github-nyurik/bindgen_helpers-8da0cb?logo=github)](https://github.com/nyurik/bindgen_helpers)\n[![crates.io version](https://img.shields.io/crates/v/bindgen_helpers)](https://crates.io/crates/bindgen_helpers)\n[![docs.rs](https://img.shields.io/docsrs/bindgen_helpers)](https://docs.rs/bindgen_helpers)\n[![license](https://img.shields.io/crates/l/bindgen_helpers)](https://github.com/nyurik/bindgen_helpers/blob/main/LICENSE-APACHE)\n[![CI build](https://github.com/nyurik/bindgen_helpers/actions/workflows/ci.yml/badge.svg)](https://github.com/nyurik/bindgen_helpers/actions)\n[![Codecov](https://img.shields.io/codecov/c/github/nyurik/bindgen_helpers)](https://app.codecov.io/gh/nyurik/bindgen_helpers)\n\nUtilities to rename, change case, and fix Rust code generated from the C headers using [bindgen](https://rust-lang.github.io/rust-bindgen/).\n`Renamer` implements a bindgen callback trait, and currently handles struct/enum/typedef type renames with a `string-\u003estring` hashmap.\nAdditionally, 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.\n\n## Usage\n\n```toml\n# Cargo.toml\n[build-dependencies]\n# Bindgen_helpers re-exports all of bindgen's public API at the root level\n# Do not use bindgen directly to avoid some version conflicts\nbindgen_helpers = \"0.3\"\n```\n\n```rust\n// build.rs\nuse bindgen_helpers::{Builder, Renamer, rename_enum};\n\nfn main() {\n  // true to enable debug output as warnings\n  let mut renamer = Renamer::new(true);\n\n  // rename a single item, e.g. a struct, enum, or a typedef\n  renamer.rename_item(\"my_struct\", \"MyStruct\");\n\n  // rename an enum and its values\n  rename_enum!(\n    renamer,\n    \"my_enum\" =\u003e \"MyEnum\", // rename the enum itself\n    remove: \"^I_SAID_\",    // optionally any number of \"remove\" regexes\n    remove: \"_ENUM$\",\n    case: Pascal,          // optionally set case convert, defaults to \"PascalCase\"\n    \"MV_IT\" =\u003e \"Value1\",   // rename a specific value after pattern removal\n    \"MV_IT2\" =\u003e \"Value2\",  // more specific value renames\n  );\n\n  let bindings = Builder::default()\n    // in real code, use .header(\"path/to/header.h\")\n    .header_contents(\"test.h\", r#\"\n\nstruct my_struct {\n    int a;\n};\n\nenum my_enum {\n\tI_SAID_YES_ENUM,\n\tI_SAID_NO_ENUM,\n\tI_SAID_MV_IT_ENUM,\n\tI_SAID_MV_IT2_ENUM,\n};\n\n\"#)\n    // note that generated regex str includes all the renames, not just enums\n    .rustified_enum(renamer.get_regex_str())\n    .parse_callbacks(Box::new(renamer))\n    .generate().unwrap();\n}\n\n//\n// This is the approximate code that would be generated by the above:\n//\n\n#[repr(C)]\n#[derive(Debug, Copy, Clone)]\npub struct MyStruct {\n  pub a: ::std::os::raw::c_int,\n}\n\n#[repr(u32)]\n#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]\npub enum MyEnum {\n  Yes = 0,\n  No = 1,\n  Value1 = 2,\n  Value2 = 3,\n}\n\n```\n\n\u003c!-- This code would generate the actual test output, but it is not stable enough to always run\n\n  // Output the generated code to a string.\n  // In real code, use .write_to_file(\"bindings.rs\")\n  let mut output = Vec::new();\n  bindings.write(Box::new(\u0026mut output)).unwrap();\n  let output = String::from_utf8(output).unwrap();\n\n  assert_eq!(output, r##\"#[repr(C)]\n#[derive(Debug, Copy, Clone)]\npub struct MyStruct {\n    pub a: ::std::os::raw::c_int,\n}\n#[allow(clippy::unnecessary_operation, clippy::identity_op)]\nconst _: () = {\n    [\"Size of MyStruct\"][::std::mem::size_of::\u003cMyStruct\u003e() - 4usize];\n    [\"Alignment of MyStruct\"][::std::mem::align_of::\u003cMyStruct\u003e() - 4usize];\n    [\"Offset of field: MyStruct::a\"][::std::mem::offset_of!(MyStruct, a) - 0usize];\n};\n#[repr(u32)]\n#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]\npub enum MyEnum {\n    Yes = 0,\n    No = 1,\n    Value1 = 2,\n    Value2 = 3,\n}\n\"##);\n--\u003e\n\nSee the list of all [case variants](https://docs.rs/convert_case/latest/convert_case/enum.Case.html) supported by the `convert_case` crate.\n\n## Development\n\n* This project is easier to develop with [just](https://github.com/casey/just#readme), a modern alternative to `make`.\n  Install it with `cargo install just`.\n* To get a list of available commands, run `just`.\n* To run tests, use `just test`.\n\n## License\n\nLicensed under either of\n\n* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or \u003chttp://www.apache.org/licenses/LICENSE-2.0\u003e)\n* MIT license ([LICENSE-MIT](LICENSE-MIT) or \u003chttp://opensource.org/licenses/MIT\u003e)\n  at your option.\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally\nsubmitted for inclusion in the work by you, as defined in the\nApache-2.0 license, shall be dual licensed as above, without any\nadditional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnyurik%2Fbindgen_helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnyurik%2Fbindgen_helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnyurik%2Fbindgen_helpers/lists"}