File tree Expand file tree Collapse file tree 3 files changed +14
-6
lines changed Expand file tree Collapse file tree 3 files changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -141,7 +141,7 @@ pub fn make_enum_definition_with(
141
141
///
142
142
/// Returns `None` if `enum_` isn't an indexable enum.
143
143
fn make_enum_index_impl ( enum_ : & Enum ) -> Option < TokenStream > {
144
- let enum_max = enum_. find_index_enum_max ( ) ? ;
144
+ let enum_max = enum_. max_index ? ; // Do nothing if enum isn't sequential with a MAX constant.
145
145
let name = & enum_. name ;
146
146
147
147
Some ( quote ! {
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ pub struct Enum {
22
22
pub is_private : bool ,
23
23
pub is_exhaustive : bool ,
24
24
pub enumerators : Vec < Enumerator > ,
25
+ /// If the enum is sequential and has a `*_MAX` constant (Godot name), this is the index of it.
26
+ pub max_index : Option < usize > ,
25
27
}
26
28
27
29
impl Enum {
@@ -73,15 +75,18 @@ impl Enum {
73
75
74
76
/// Returns the maximum index of an indexable enum.
75
77
///
76
- /// Return `None` if `self` isn't an indexable enum. Meaning it is either a bitfield, or it is an enum that can't be used as an index.
77
- pub fn find_index_enum_max ( & self ) -> Option < usize > {
78
- if self . is_bitfield {
78
+ /// Returns `None` if this is a bitfield, or an enum that isn't sequential with a `*_MAX` enumerator.
79
+ pub fn find_index_enum_max_impl (
80
+ is_bitfield : bool ,
81
+ enumerators : & [ Enumerator ] ,
82
+ ) -> Option < usize > {
83
+ if is_bitfield {
79
84
return None ;
80
85
}
81
86
82
87
// Sort by ordinal value. Allocates for every enum in the JSON, but should be OK (most enums are indexable).
83
88
let enumerators = {
84
- let mut enumerators = self . enumerators . clone ( ) ;
89
+ let mut enumerators = enumerators. to_vec ( ) ;
85
90
enumerators. sort_by_key ( |v| v. value . to_i64 ( ) ) ;
86
91
enumerators
87
92
} ;
Original file line number Diff line number Diff line change @@ -662,7 +662,7 @@ impl Enum {
662
662
conv:: make_enumerator_names ( godot_class_name, & rust_enum_name, godot_enumerator_names)
663
663
} ;
664
664
665
- let enumerators = json_enum
665
+ let enumerators: Vec < Enumerator > = json_enum
666
666
. values
667
667
. iter ( )
668
668
. zip ( rust_enumerator_names)
@@ -671,6 +671,8 @@ impl Enum {
671
671
} )
672
672
. collect ( ) ;
673
673
674
+ let max_index = Enum :: find_index_enum_max_impl ( is_bitfield, & enumerators) ;
675
+
674
676
Self {
675
677
name : ident ( & rust_enum_name) ,
676
678
godot_name : godot_name. clone ( ) ,
@@ -679,6 +681,7 @@ impl Enum {
679
681
is_private,
680
682
is_exhaustive,
681
683
enumerators,
684
+ max_index,
682
685
}
683
686
}
684
687
}
You can’t perform that action at this time.
0 commit comments