Skip to content

Commit 44a7a8e

Browse files
committed
Add callback to allow renaming result error enum
1 parent 241278c commit 44a7a8e

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

bindgen-tests/tests/expectations/tests/parsecb-result-error-enum-name.rs

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// bindgen-flags: --result-error-enum MyResult
2+
// bindgen-parse-callbacks: result-error-enum-rename
3+
4+
enum MyResult {
5+
MyResultOk = 0,
6+
MyResultInvalid,
7+
MyResultAnotherError,
8+
};
9+
10+
typedef enum MyResult AnotherResult;
11+
12+
enum MyResult some_function(void);
13+
14+
AnotherResult another_function(void);

bindgen-tests/tests/parse_callbacks/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ impl ParseCallbacks for EnumVariantRename {
7272
}
7373
}
7474

75+
#[derive(Debug)]
76+
struct ResultErrorEnumRename;
77+
78+
impl ParseCallbacks for ResultErrorEnumRename {
79+
fn result_error_enum_name(
80+
&self,
81+
original_enum_name: &str,
82+
) -> Option<(String, String)> {
83+
if let Some(base) = original_enum_name.strip_suffix("Result") {
84+
Some((original_enum_name.to_string(), format!("{base}Error")))
85+
} else {
86+
None
87+
}
88+
}
89+
}
90+
7591
#[derive(Debug)]
7692
struct BlocklistedTypeImplementsTrait;
7793

@@ -149,6 +165,7 @@ impl ParseCallbacks for WrapAsVariadicFn {
149165
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
150166
match cb {
151167
"enum-variant-rename" => Box::new(EnumVariantRename),
168+
"result-error-enum-rename" => Box::new(ResultErrorEnumRename),
152169
"blocklisted-type-implements-trait" => {
153170
Box::new(BlocklistedTypeImplementsTrait)
154171
}

bindgen/callbacks.rs

+15
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ pub trait ParseCallbacks: fmt::Debug {
7979
None
8080
}
8181

82+
/// Allows to renum the types for a result_error_enum.
83+
///
84+
/// The original enum is translated to a `Result<(), NonZero<ErrorEnum>>` representation.
85+
/// The first string allows renaming the type alias for the Result.
86+
/// The second string allows renaming the `ErrorEnum`.
87+
///
88+
/// By default, the Result typealias will be the original enum name, and the
89+
/// error enum will have an `Error` suffix appended to the original name.
90+
fn result_error_enum_name(
91+
&self,
92+
_original_enum_name: &str,
93+
) -> Option<(String, String)> {
94+
None
95+
}
96+
8297
/// Allows to rename an enum variant, replacing `_original_variant_name`.
8398
fn enum_variant_name(
8499
&self,

bindgen/codegen/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -3316,6 +3316,7 @@ impl<'a> EnumBuilder<'a> {
33163316
/// the representation, and which variation it should be generated as.
33173317
fn new(
33183318
name: &'a str,
3319+
ctx: &BindgenContext,
33193320
mut attrs: Vec<proc_macro2::TokenStream>,
33203321
repr: syn::Type,
33213322
enum_variation: EnumVariation,
@@ -3329,8 +3330,12 @@ impl<'a> EnumBuilder<'a> {
33293330
is_global,
33303331
is_result_type: true,
33313332
} => {
3332-
// Todo: This identifier perhaps could be determined by a ParseCallback.
3333-
let error_ident = format_ident!("{}Error", ident);
3333+
let (result_alias, error_name) = ctx
3334+
.options()
3335+
.last_callback(|c| c.result_error_enum_name(&name))
3336+
.unwrap_or((name.to_string(), format!("{name}Error")));
3337+
let ident = Ident::new(&result_alias, Span::call_site());
3338+
let error_ident = Ident::new(&error_name, Span::call_site());
33343339
EnumBuilder::NewType {
33353340
canonical_name: name,
33363341
tokens: quote! {
@@ -3837,7 +3842,7 @@ impl CodeGenerator for Enum {
38373842
let has_typedef = ctx.is_enum_typedef_combo(item.id());
38383843

38393844
let mut builder =
3840-
EnumBuilder::new(&name, attrs, repr, variation, has_typedef);
3845+
EnumBuilder::new(&name, ctx, attrs, repr, variation, has_typedef);
38413846

38423847
// A map where we keep a value -> variant relation.
38433848
let mut seen_values = HashMap::<_, Ident>::default();

0 commit comments

Comments
 (0)