Skip to content

Commit 364051c

Browse files
committed
Refactor: move enum MAX detection up into domain mapping
1 parent 1ab1d8e commit 364051c

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

godot-codegen/src/generator/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn make_enum_definition_with(
141141
///
142142
/// Returns `None` if `enum_` isn't an indexable enum.
143143
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.
145145
let name = &enum_.name;
146146

147147
Some(quote! {

godot-codegen/src/models/domain/enums.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct Enum {
2222
pub is_private: bool,
2323
pub is_exhaustive: bool,
2424
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>,
2527
}
2628

2729
impl Enum {
@@ -73,15 +75,18 @@ impl Enum {
7375

7476
/// Returns the maximum index of an indexable enum.
7577
///
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 {
7984
return None;
8085
}
8186

8287
// Sort by ordinal value. Allocates for every enum in the JSON, but should be OK (most enums are indexable).
8388
let enumerators = {
84-
let mut enumerators = self.enumerators.clone();
89+
let mut enumerators = enumerators.to_vec();
8590
enumerators.sort_by_key(|v| v.value.to_i64());
8691
enumerators
8792
};

godot-codegen/src/models/domain_mapping.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl Enum {
662662
conv::make_enumerator_names(godot_class_name, &rust_enum_name, godot_enumerator_names)
663663
};
664664

665-
let enumerators = json_enum
665+
let enumerators: Vec<Enumerator> = json_enum
666666
.values
667667
.iter()
668668
.zip(rust_enumerator_names)
@@ -671,6 +671,8 @@ impl Enum {
671671
})
672672
.collect();
673673

674+
let max_index = Enum::find_index_enum_max_impl(is_bitfield, &enumerators);
675+
674676
Self {
675677
name: ident(&rust_enum_name),
676678
godot_name: godot_name.clone(),
@@ -679,6 +681,7 @@ impl Enum {
679681
is_private,
680682
is_exhaustive,
681683
enumerators,
684+
max_index,
682685
}
683686
}
684687
}

0 commit comments

Comments
 (0)