Skip to content

Commit 1f9c20c

Browse files
committed
feat(EnumHint): add EnumHintEntry to store entry info
1 parent 825ad68 commit 1f9c20c

File tree

4 files changed

+53
-58
lines changed

4 files changed

+53
-58
lines changed

gdnative-core/src/export/property/hint.rs

+40-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Strongly typed property hints.
22
3-
use std::fmt::{self, Write};
3+
use std::fmt::{self, Display, Write};
44
use std::ops::RangeInclusive;
55

66
use crate::core_types::GodotString;
@@ -116,21 +116,21 @@ where
116116
/// ```
117117
#[derive(Clone, Eq, PartialEq, Debug, Default)]
118118
pub struct EnumHint {
119-
values: Vec<(String, Option<i64>)>,
119+
values: Vec<EnumHintEntry>,
120120
}
121121

122122
impl EnumHint {
123123
#[inline]
124124
pub fn new(values: Vec<String>) -> Self {
125-
let values = values.into_iter().map(|v| (v, None)).collect();
125+
let values = values.into_iter().map(EnumHintEntry::new).collect();
126126
EnumHint { values }
127127
}
128128

129129
#[inline]
130-
pub fn with_numbers(values: Vec<(String, i64)>) -> Self {
130+
pub fn with_values(values: Vec<(String, i64)>) -> Self {
131131
let values = values
132132
.into_iter()
133-
.map(|(key, val)| (key, Some(val)))
133+
.map(|(key, value)| EnumHintEntry::with_value(key, value))
134134
.collect();
135135
EnumHint { values }
136136
}
@@ -140,28 +140,52 @@ impl EnumHint {
140140
let mut s = String::new();
141141

142142
let mut iter = self.values.iter();
143-
let write_item = |s: &mut String, item: &(String, Option<i64>)| match item {
144-
(key, Some(val)) => {
145-
write!(s, "{key}:{val}")
146-
}
147-
(key, None) => {
148-
write!(s, "{key}")
149-
}
150-
};
151143

152144
if let Some(first) = iter.next() {
153-
write_item(&mut s, first).unwrap();
145+
write!(s, "{first}").unwrap();
154146
}
155147

156148
for rest in iter {
157149
write!(s, ",").unwrap();
158-
write_item(&mut s, rest).unwrap();
150+
write!(s, "{rest}").unwrap();
159151
}
160152

161153
s.into()
162154
}
163155
}
164156

157+
#[derive(Clone, PartialEq, Eq, Debug)]
158+
pub struct EnumHintEntry {
159+
key: String,
160+
value: Option<i64>,
161+
}
162+
163+
impl EnumHintEntry {
164+
#[inline]
165+
pub fn new(key: String) -> Self {
166+
Self { key, value: None }
167+
}
168+
169+
#[inline]
170+
pub fn with_value(key: String, value: i64) -> Self {
171+
Self {
172+
key,
173+
value: Some(value),
174+
}
175+
}
176+
}
177+
178+
impl Display for EnumHintEntry {
179+
#[inline]
180+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181+
write!(f, "{}", self.key)?;
182+
if let Some(value) = self.value {
183+
write!(f, ":{}", value)?;
184+
}
185+
Ok(())
186+
}
187+
}
188+
165189
/// Possible hints for integers.
166190
#[derive(Clone, Debug)]
167191
#[non_exhaustive]
@@ -495,6 +519,6 @@ godot_test!(test_enum_hint_without_mapping {
495519
});
496520

497521
godot_test!(test_enum_hint_with_mapping {
498-
let hint = EnumHint::with_numbers(vec![("Foo".into(), 42), ("Bar".into(), 67)]);
522+
let hint = EnumHint::with_values(vec![("Foo".into(), 42), ("Bar".into(), 67)]);
499523
assert_eq!(hint.to_godot_hint_string().to_string(), "Foo:42,Bar:67".to_string(),);
500524
});

gdnative-derive/src/export.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::crate_gdnative_core;
12
use proc_macro2::{Span, TokenStream as TokenStream2};
23
use syn::spanned::Spanned;
34
use syn::{DeriveInput, Fields};
@@ -44,21 +45,23 @@ fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenS
4445
quote! { (stringify!(#key).to_string(), #val) }
4546
})
4647
.collect::<Vec<_>>();
48+
let gdnative_core = crate_gdnative_core();
4749

4850
let impl_block = quote! {
49-
impl ::gdnative::export::Export for #enum_ty {
50-
type Hint = ::gdnative::export::hint::IntHint<i64>;
51-
#[inline]
52-
fn export_info(hint: Option<Self::Hint>) -> ::gdnative::export::ExportInfo {
53-
if let Some(hint) = hint {
54-
return hint.export_info();
55-
} else {
51+
const _: () = {
52+
pub enum NoHint {}
53+
54+
impl #gdnative_core::export::Export for #enum_ty {
55+
type Hint = NoHint;
56+
57+
#[inline]
58+
fn export_info(_hint: Option<Self::Hint>) -> #gdnative_core::export::ExportInfo {
5659
let mappings = vec![ #(#mappings),* ];
57-
let enum_hint = ::gdnative::export::hint::EnumHint::with_numbers(mappings);
58-
return ::gdnative::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
60+
let enum_hint = #gdnative_core::export::hint::EnumHint::with_values(mappings);
61+
return #gdnative_core::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
5962
}
6063
}
61-
}
64+
};
6265
};
6366

6467
Ok(impl_block)

test/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod test_as_arg;
88
mod test_async;
99
mod test_constructor;
1010
mod test_derive;
11-
mod test_export_enum;
1211
mod test_free_ub;
1312
mod test_generic_class;
1413
mod test_indexed_props;
@@ -53,7 +52,6 @@ pub extern "C" fn run_tests(
5352
status &= test_vararray_return::run_tests();
5453
status &= test_variant_call_args::run_tests();
5554
status &= test_variant_ops::run_tests();
56-
status &= test_export_enum::run_tests();
5755

5856
Variant::new(status).leak()
5957
}

test/src/test_export_enum.rs

-30
This file was deleted.

0 commit comments

Comments
 (0)