cbindgen
cbindgen copied to clipboard
Is it possible add a prefix to the enum variants as well?
by experimenting I saw is possible to rename the enum type so:
#[repr(C)]
enum Foo {
Bar,
Baz
}
is mapped to:
enum PrefixFoo {
BAR,
BAZ
}
While ideally you'd like to have:
enum PrefixFoo {
PREFIX_BAR,
PREFIX_BAZ
}
https://github.com/eqrion/cbindgen/pull/237 addresses it.
I would like to be able to add a custom prefix to enum variants which is separate to the main enum name. Is that currently possible? It seems like the referenced PR possibly doesn't address it?
Edit: to be clearer, I'm hoping to achieve the follow:
pub enum ErrorCode {
None = 0,
Generic = 1,
Closed = 2,
Open = 3,
}
to
typedef enum
{
MYPROJ_ERR_NONE = 0,
MYPROJ_ERR_GENERIC = 1,
MYPROJ_ERR_CLOSED = 2,
MYPROJ_ERR_OPEN = 3,
} myproj_errorcode_t;
To keep backwards compatibility with a current C/C++ API and I'm not sure how best to achieve it with the current settings.
I am using prefix = "myprog_" but that doesn't impact the variants and the prefix_with_name would not achieve the result I'm going for, I don't think.
I think something like:
/// cbindgen:variant_prefix=MYPROJ_ERR
/// cbindgen:rename_variants=ScreamingSnakeCase
pub enum ErrorCode {
None = 0,
Generic = 1,
Closed = 2,
Open = 3,
}
Combined with
[export]
prefix = "myproj_"
[export.rename]
"ErrorCode" = "errorcode_t"
Might do the trick. Where variant_prefix would be a separate setting which would win over or join with prefix_with_name if combined. I guess.
I've ended up using prefix_with_name and ScreamingSnakeCase and then the all caps prefix becomes an identifiable string that I can use in String.replace to put in the actual variant prefix that I want.
It means I have to use the cbindgen Builder and write the output to a string and then find-and-replace before writing the bindings to a file but that is no big deal. I think it would be a reasonable feature though (some kind of variant_prefix string, that is) and I'd be happy to give it a go if there is consensus on that.