diff --git a/godot-codegen/src/api_parser.rs b/godot-codegen/src/api_parser.rs index e35c7e638..3d7625f90 100644 --- a/godot-codegen/src/api_parser.rs +++ b/godot-codegen/src/api_parser.rs @@ -122,6 +122,11 @@ pub struct EnumConstant { pub value: i64, } +pub enum ConstValue { + I32(i32), + I64(i64), +} + impl EnumConstant { pub fn to_enum_ord(&self) -> i32 { self.value.try_into().unwrap_or_else(|_| { @@ -141,13 +146,12 @@ impl EnumConstant { }) } - pub fn to_constant(&self) -> i32 { - self.value.try_into().unwrap_or_else(|_| { - panic!( - "constant {} = {} is out of range for i32, please report this", - self.name, self.value - ) - }) + pub fn to_constant(&self) -> ConstValue { + if let Ok(value) = i32::try_from(self.value) { + ConstValue::I32(value) + } else { + ConstValue::I64(self.value) + } } } diff --git a/godot-codegen/src/util.rs b/godot-codegen/src/util.rs index cb933c191..82ef8bc8d 100644 --- a/godot-codegen/src/util.rs +++ b/godot-codegen/src/util.rs @@ -6,7 +6,7 @@ */ use crate::api_parser::{ - BuiltinClassMethod, Class, ClassConstant, ClassMethod, Enum, UtilityFunction, + BuiltinClassMethod, Class, ClassConstant, ClassMethod, ConstValue, Enum, UtilityFunction, }; use crate::special_cases::is_builtin_scalar; use crate::{Context, GodotTy, ModName, RustTy, TyName}; @@ -360,17 +360,15 @@ pub fn make_enum_definition(enum_: &Enum) -> TokenStream { pub fn make_constant_definition(constant: &ClassConstant) -> TokenStream { let name = ident(&constant.name); - let value = constant.to_constant(); - - if constant.name.starts_with("NOTIFICATION_") { - // Already exposed through enums - quote! { - pub(crate) const #name: i32 = #value; - } + let vis = if constant.name.starts_with("NOTIFICATION_") { + quote! { pub(crate) } } else { - quote! { - pub const #name: i32 = #value; - } + quote! { pub } + }; + + match constant.to_constant() { + ConstValue::I32(value) => quote! { #vis const #name: i32 = #value; }, + ConstValue::I64(value) => quote! { #vis const #name: i64 = #value; }, } }