@@ -7,6 +7,15 @@ use super::context::{BindgenContext, ItemId};
7
7
use super :: item:: Item ;
8
8
use super :: ty:: TypeKind ;
9
9
10
+ /// An enum representing custom handling that can be given to a variant.
11
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
12
+ pub enum EnumVariantCustomBehavior {
13
+ /// This variant will be constified, that is, forced to generate a constant.
14
+ Constify ,
15
+ /// This variant will be hidden entirely from the resulting enum.
16
+ Hide ,
17
+ }
18
+
10
19
/// A C/C++ enumeration.
11
20
#[ derive( Debug ) ]
12
21
pub struct Enum {
@@ -80,16 +89,25 @@ impl Enum {
80
89
} ;
81
90
if let Some ( val) = value {
82
91
let name = cursor. spelling ( ) ;
83
- let should_constify = ctx. type_chooser ( )
84
- . map_or ( false , |c| {
85
- c. constify_enum_variant ( type_name, & name, val)
86
- } ) ||
87
- Annotations :: new ( & cursor) . map_or ( false , |anno| {
88
- anno. constify_enum_variant ( )
92
+ let custom_behavior = ctx. type_chooser ( )
93
+ . and_then ( |t| {
94
+ t. enum_variant_behavior ( type_name, & name, val)
95
+ } )
96
+ . or_else ( || {
97
+ Annotations :: new ( & cursor) . and_then ( |anno| {
98
+ if anno. hide ( ) {
99
+ Some ( EnumVariantCustomBehavior :: Hide )
100
+ } else if anno. constify_enum_variant ( ) {
101
+ Some ( EnumVariantCustomBehavior :: Constify )
102
+ } else {
103
+ None
104
+ }
105
+ } )
89
106
} ) ;
107
+
90
108
let comment = cursor. raw_comment ( ) ;
91
109
variants. push (
92
- EnumVariant :: new ( name, comment, val, should_constify ) ) ;
110
+ EnumVariant :: new ( name, comment, val, custom_behavior ) ) ;
93
111
}
94
112
}
95
113
CXChildVisit_Continue
@@ -110,10 +128,8 @@ pub struct EnumVariant {
110
128
/// The integer value of the variant.
111
129
val : EnumVariantValue ,
112
130
113
- /// Whether this value should explicitly be constified.
114
- ///
115
- /// This allows us to solve situations as described in #392.
116
- force_constify : bool ,
131
+ /// The custom behavior this variant may have, if any.
132
+ custom_behavior : Option < EnumVariantCustomBehavior > ,
117
133
}
118
134
119
135
/// A constant value assigned to an enumeration variant.
@@ -131,13 +147,13 @@ impl EnumVariant {
131
147
pub fn new ( name : String ,
132
148
comment : Option < String > ,
133
149
val : EnumVariantValue ,
134
- force_constify : bool )
150
+ custom_behavior : Option < EnumVariantCustomBehavior > )
135
151
-> Self {
136
152
EnumVariant {
137
153
name : name,
138
154
comment : comment,
139
155
val : val,
140
- force_constify : force_constify ,
156
+ custom_behavior : custom_behavior ,
141
157
}
142
158
}
143
159
@@ -154,6 +170,14 @@ impl EnumVariant {
154
170
/// Returns whether this variant should be enforced to be a constant by code
155
171
/// generation.
156
172
pub fn force_constification ( & self ) -> bool {
157
- self . force_constify
173
+ self . custom_behavior
174
+ . map_or ( false , |b| b == EnumVariantCustomBehavior :: Constify )
175
+ }
176
+
177
+ /// Returns whether the current variant should be hidden completely from the
178
+ /// resulting rust enum.
179
+ pub fn hidden ( & self ) -> bool {
180
+ self . custom_behavior
181
+ . map_or ( false , |b| b == EnumVariantCustomBehavior :: Hide )
158
182
}
159
183
}
0 commit comments