Skip to content

Commit c21ef9f

Browse files
authored
Merge pull request #530 from Lange-Studios/const-out-of-range-fix
Added support for parsing i32, u32, i64, and u64 constants
2 parents 0eb3f7a + 2ec1c96 commit c21ef9f

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

godot-codegen/src/api_parser.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ pub struct EnumConstant {
122122
pub value: i64,
123123
}
124124

125+
pub enum ConstValue {
126+
I32(i32),
127+
I64(i64),
128+
}
129+
125130
impl EnumConstant {
126131
pub fn to_enum_ord(&self) -> i32 {
127132
self.value.try_into().unwrap_or_else(|_| {
@@ -141,13 +146,12 @@ impl EnumConstant {
141146
})
142147
}
143148

144-
pub fn to_constant(&self) -> i32 {
145-
self.value.try_into().unwrap_or_else(|_| {
146-
panic!(
147-
"constant {} = {} is out of range for i32, please report this",
148-
self.name, self.value
149-
)
150-
})
149+
pub fn to_constant(&self) -> ConstValue {
150+
if let Ok(value) = i32::try_from(self.value) {
151+
ConstValue::I32(value)
152+
} else {
153+
ConstValue::I64(self.value)
154+
}
151155
}
152156
}
153157

godot-codegen/src/util.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
use crate::api_parser::{
9-
BuiltinClassMethod, Class, ClassConstant, ClassMethod, Enum, UtilityFunction,
9+
BuiltinClassMethod, Class, ClassConstant, ClassMethod, ConstValue, Enum, UtilityFunction,
1010
};
1111
use crate::special_cases::is_builtin_scalar;
1212
use crate::{Context, GodotTy, ModName, RustTy, TyName};
@@ -360,17 +360,15 @@ pub fn make_enum_definition(enum_: &Enum) -> TokenStream {
360360

361361
pub fn make_constant_definition(constant: &ClassConstant) -> TokenStream {
362362
let name = ident(&constant.name);
363-
let value = constant.to_constant();
364-
365-
if constant.name.starts_with("NOTIFICATION_") {
366-
// Already exposed through enums
367-
quote! {
368-
pub(crate) const #name: i32 = #value;
369-
}
363+
let vis = if constant.name.starts_with("NOTIFICATION_") {
364+
quote! { pub(crate) }
370365
} else {
371-
quote! {
372-
pub const #name: i32 = #value;
373-
}
366+
quote! { pub }
367+
};
368+
369+
match constant.to_constant() {
370+
ConstValue::I32(value) => quote! { #vis const #name: i32 = #value; },
371+
ConstValue::I64(value) => quote! { #vis const #name: i64 = #value; },
374372
}
375373
}
376374

0 commit comments

Comments
 (0)