Skip to content

Commit 61fdd8d

Browse files
committed
added support for parsing i32, u32, and i64 constants
1 parent 9920e2b commit 61fdd8d

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

godot-codegen/src/api_parser.rs

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

125+
pub enum ConstValue {
126+
I32(i32),
127+
U32(u32),
128+
I64(i64),
129+
}
130+
125131
impl EnumConstant {
126132
pub fn to_enum_ord(&self) -> i32 {
127133
self.value.try_into().unwrap_or_else(|_| {
@@ -141,13 +147,14 @@ impl EnumConstant {
141147
})
142148
}
143149

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-
})
150+
pub fn to_constant(&self) -> ConstValue {
151+
if let Ok(value) = i32::try_from(self.value) {
152+
ConstValue::I32(value)
153+
} else if let Ok(value) = u32::try_from(self.value) {
154+
ConstValue::U32(value)
155+
} else {
156+
ConstValue::I64(self.value)
157+
}
151158
}
152159
}
153160

godot-codegen/src/util.rs

Lines changed: 10 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,16 @@ 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::U32(value) => quote!(#vis const #name: u32 = #value;),
372+
ConstValue::I64(value) => quote!(#vis const #name: i64 = #value;),
374373
}
375374
}
376375

0 commit comments

Comments
 (0)