Skip to content

Added support for parsing i32, u32, i64, and u64 constants #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions godot-codegen/src/api_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|_| {
Expand All @@ -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)
}
}
}

Expand Down
20 changes: 9 additions & 11 deletions godot-codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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; },
}
}

Expand Down