Skip to content

Commit

Permalink
Add option to generate safe and unsafe conversions for rustified enums
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Sep 3, 2024
1 parent 9a8e5ca commit 797ac8b
Show file tree
Hide file tree
Showing 60 changed files with 1,116 additions and 146 deletions.
38 changes: 24 additions & 14 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use bindgen::callbacks::TypeKind;
use bindgen::{
builder, Abi, AliasVariation, Builder, CodegenConfig, EnumVariation,
FieldVisibilityKind, Formatter, MacroTypeVariation, NonCopyUnionStyle,
RegexSet, RustTarget, DEFAULT_ANON_FIELDS_PREFIX, RUST_TARGET_STRINGS,
RegexSet, RustEnumOptions, RustTarget, DEFAULT_ANON_FIELDS_PREFIX,
RUST_TARGET_STRINGS,
};
use clap::error::{Error, ErrorKind};
use clap::{CommandFactory, Parser};
Expand Down Expand Up @@ -77,6 +78,21 @@ fn parse_abi_override(abi_override: &str) -> Result<(Abi, String), Error> {
Ok((abi, regex.to_owned()))
}

fn parse_rustified_enum(
rustified_enum: &str,
) -> Result<(RustEnumOptions, String), Error> {
let (regex, options) = match rustified_enum.rsplit_once('=') {
Some((regex, options)) => (regex, options),
None => (rustified_enum, ""),
};

let options = options
.parse()
.map_err(|err| Error::raw(ErrorKind::InvalidValue, err))?;

Ok((options, regex.to_owned()))
}

fn parse_custom_derive(
custom_derive: &str,
) -> Result<(Vec<String>, String), Error> {
Expand Down Expand Up @@ -150,12 +166,11 @@ struct BindgenCommand {
/// Mark any enum whose name matches REGEX as a global newtype.
#[arg(long, value_name = "REGEX")]
newtype_global_enum: Vec<String>,
/// Mark any enum whose name matches REGEX as a Rust enum.
#[arg(long, value_name = "REGEX")]
rustified_enum: Vec<String>,
/// Mark any enum whose name matches REGEX as a non-exhaustive Rust enum.
#[arg(long, value_name = "REGEX")]
rustified_non_exhaustive_enum: Vec<String>,
/// Mark any enum whose name matches the provided regex as a Rust enum. This parameter takes
/// options in the shape REGEX or REGEX=OPTIONS where OPTIONS can be a comma separated list of
/// options from non_exhaustive, try_from_raw, and from_raw_unchecked.
#[arg(long, value_parser = parse_rustified_enum)]
rustified_enum: Vec<(RustEnumOptions, String)>,
/// Mark any enum whose name matches REGEX as a series of constants.
#[arg(long, value_name = "REGEX")]
constified_enum: Vec<String>,
Expand Down Expand Up @@ -523,7 +538,6 @@ where
newtype_enum,
newtype_global_enum,
rustified_enum,
rustified_non_exhaustive_enum,
constified_enum,
constified_enum_module,
default_macro_constant_type,
Expand Down Expand Up @@ -690,12 +704,8 @@ where
builder = builder.newtype_global_enum(regex);
}

for regex in rustified_enum {
builder = builder.rustified_enum(regex);
}

for regex in rustified_non_exhaustive_enum {
builder = builder.rustified_non_exhaustive_enum(regex);
for (options, regex) in rustified_enum {
builder = builder.rustified_enum(options, regex);
}

for regex in constified_enum {
Expand Down
2 changes: 2 additions & 0 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ fn setup_macro_test() {
.enable_cxx_namespaces()
.default_enum_style(EnumVariation::Rust {
non_exhaustive: false,
safe_conversion: false,
unsafe_conversion: false,
})
.raw_line("pub use self::root::*;")
.raw_line("extern { fn my_prefixed_function_to_remove(i: i32); }")
Expand Down
5 changes: 5 additions & 0 deletions bindgen-tests/tests/expectations/tests/anon_enum.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bindgen-tests/tests/expectations/tests/anon_enum_allowlist.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bindgen-tests/tests/expectations/tests/anon_enum_trait.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindgen-tests/tests/expectations/tests/anon_union.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindgen-tests/tests/expectations/tests/anon_union_1_0.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bindgen-tests/tests/expectations/tests/bitfield_align_2.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bindgen-tests/tests/expectations/tests/const_enum_unnamed.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bindgen-tests/tests/expectations/tests/constant-evaluate.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions bindgen-tests/tests/expectations/tests/constify-enum.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindgen-tests/tests/expectations/tests/derive-custom-cli.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bindgen-tests/tests/expectations/tests/empty-enum.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-default-rust.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-doc-rusty.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-no-debug-rust.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-undefault.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum_alias.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 797ac8b

Please sign in to comment.