Skip to content

Commit 4c6c997

Browse files
committed
ir: Allow renaming variants using the replaces="" annotation or a custom callback.
1 parent 27641c2 commit 4c6c997

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

src/callbacks.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
3636
}
3737

3838
/// This function should return whether, given the a given enum variant
39-
/// name, and value, returns whether this enum variant will forcibly be a
40-
/// constant.
39+
/// name, and value, this enum variant will forcibly be a constant.
4140
fn enum_variant_behavior(
4241
&self,
4342
_enum_name: Option<&str>,
44-
_variant_name: &str,
43+
_original_variant_name: &str,
4544
_variant_value: EnumVariantValue,
4645
) -> Option<EnumVariantCustomBehavior> {
4746
None
4847
}
48+
49+
/// Allows to rename an enum variant, replacing `_original_variant_name`.
50+
fn enum_variant_name(
51+
&self,
52+
_enum_name: Option<&str>,
53+
_original_variant_name: &str,
54+
_variant_value: EnumVariantValue,
55+
) -> Option<String> {
56+
None
57+
}
4958
}

src/ir/enum_ty.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,31 @@ impl Enum {
9898
};
9999
if let Some(val) = value {
100100
let name = cursor.spelling();
101+
let annotations = Annotations::new(&cursor);
101102
let custom_behavior = ctx.parse_callbacks()
102-
.and_then(
103-
|t| t.enum_variant_behavior(type_name, &name, val),
104-
)
103+
.and_then(|callbacks| {
104+
callbacks.enum_variant_behavior(type_name, &name, val)
105+
})
105106
.or_else(|| {
106-
Annotations::new(&cursor).and_then(
107-
|anno| if anno.hide() {
108-
Some(EnumVariantCustomBehavior::Hide)
109-
} else if anno.constify_enum_variant() {
110-
Some(EnumVariantCustomBehavior::Constify)
111-
} else {
112-
None
113-
},
114-
)
107+
let annotations = annotations.as_ref()?;
108+
if annotations.hide() {
109+
Some(EnumVariantCustomBehavior::Hide)
110+
} else if annotations.constify_enum_variant() {
111+
Some(EnumVariantCustomBehavior::Constify)
112+
} else {
113+
None
114+
}
115115
});
116116

117+
let name = ctx.parse_callbacks()
118+
.and_then(|callbacks| {
119+
callbacks.enum_variant_name(type_name, &name, val)
120+
})
121+
.or_else(|| {
122+
annotations.as_ref()?.use_instead_of()?.last().cloned()
123+
})
124+
.unwrap_or(name);
125+
117126
let comment = cursor.raw_comment();
118127
variants.push(EnumVariant::new(
119128
name,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
/// <div rustbindgen replaces="PASS"></div>
6+
///
7+
/// Should see PASS below.
8+
pub const OGRErr_PASS: OGRErr = 0;
9+
/// <div rustbindgen replaces="OGRERR_NONE"></div>
10+
///
11+
/// Should see OGRERR_NONE instead of CUSTOM_OGRERR_NONE below.
12+
pub const OGRErr_OGRERR_NONE: OGRErr = 1;
13+
/// <div rustbindgen replaces="OGRErr"></div>
14+
pub type OGRErr = u32;

tests/headers/enum-variant-replaces.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/** Type for a OGR error */
3+
typedef enum
4+
{
5+
OGRERR_NONE, /**< Success */
6+
OGRERR_NOT_ENOUGH_DATA, /**< Not enough data to deserialize */
7+
OGRERR_NOT_ENOUGH_MEMORY, /**< Not enough memory */
8+
OGRERR_UNSUPPORTED_GEOMETRY_TYPE, /**< Unsupported geometry type */
9+
OGRERR_UNSUPPORTED_OPERATION, /**< Unsupported operation */
10+
OGRERR_CORRUPT_DATA, /**< Corrupt data */
11+
OGRERR_FAILURE, /**< Failure */
12+
OGRERR_UNSUPPORTED_SRS, /**< Unsupported SRS */
13+
OGRERR_INVALID_HANDLE, /**< Invalid handle */
14+
OGRERR_NON_EXISTING_FEATURE /**< Non existing feature. Added in GDAL 2.0 */
15+
} OGRErr;
16+
17+
/**
18+
* <div rustbindgen replaces="OGRErr"></div>
19+
*/
20+
typedef enum
21+
{
22+
/**
23+
* <div rustbindgen replaces="PASS"></div>
24+
*
25+
* Should see PASS below.
26+
*/
27+
FAIL,
28+
/**
29+
* <div rustbindgen replaces="OGRERR_NONE"></div>
30+
*
31+
* Should see OGRERR_NONE instead of CUSTOM_OGRERR_NONE below.
32+
*/
33+
CUSTOM_OGRERR_NONE
34+
} StrictOGRErr;

0 commit comments

Comments
 (0)