Bindgen remove redundant enum name prefixing

I'm using bindgen to generate bindings for a C header library with code like

typedef enum LIB_RESULT
{
    LIB_OK,
    LIB_ERR_BADCOMMAND,
    LIB_ERR_INTERNAL,
    LIB_ERR_INVALID_FLOAT,
    LIB_ERR_MEMORY,
    // etc
    LIB_RESULT_FORCEINT = 65536
} LIB_RESULT;

Running this through bindgen, I get

pub type LIB_RESULT = c_int;
pub const LIB_RESULT_LIB_OK: LIB_RESULT = 0;
// etc
pub const LIB_RESULT_LIB_RESULT_FORCEINT: LIB_RESULT = 65536;

Even more fun, theres

typedef enum LIB_OUTPUTTYPE
{
    LIB_OUTPUTTYPE_AUTODETECT,
    LIB_OUTPUTTYPE_UNKNOWN,
    // etc
    LIB_OUTPUTTYPE_FORCEINT = 65536
}

so we get Rust names like LIB_OUTPUTTYPE_LIB_OUTPUTTYPE_UNKNOWN.

This library has the C enum names pre-prefixed, and I want to prevent bindgen from adding the extra unnecessary prefix. Can I do this?

I already tried ParseCallbacks, but it passes the name as in the C header and even if I return Some(original_variant_name), bindgen still prepends the enum name.

To be clear: I just want it to generate pub const LIB_OK: LIB_RESULT = 0; (and the other variants).

Add an ability to trim prefixes/postfixes of enums · Issue #777 · rust-lang/rust-bindgen · GitHub is related, but the opposite direction: it wants to strip the prefix that is written in the C header.

The mangling prefix is added here: rust-bindgen/mod.rs at f34e4103f304500c96d332f5cecc9067c980d0f9 · rust-lang/rust-bindgen · GitHub but I can't tell how to not have a mangling prefix.

And of course as soon as I post the issue I find the toggle

.prepend_enum_name(false)

2 Likes