Skip to content

Commit

Permalink
feat(EnumHint): add EnumHintEntry to store entry info
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogay committed Oct 13, 2023
1 parent 825ad68 commit 1f9c20c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 58 deletions.
56 changes: 40 additions & 16 deletions gdnative-core/src/export/property/hint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Strongly typed property hints.

use std::fmt::{self, Write};
use std::fmt::{self, Display, Write};
use std::ops::RangeInclusive;

use crate::core_types::GodotString;
Expand Down Expand Up @@ -116,21 +116,21 @@ where
/// ```
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct EnumHint {
values: Vec<(String, Option<i64>)>,
values: Vec<EnumHintEntry>,
}

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

#[inline]
pub fn with_numbers(values: Vec<(String, i64)>) -> Self {
pub fn with_values(values: Vec<(String, i64)>) -> Self {
let values = values
.into_iter()
.map(|(key, val)| (key, Some(val)))
.map(|(key, value)| EnumHintEntry::with_value(key, value))
.collect();
EnumHint { values }
}
Expand All @@ -140,28 +140,52 @@ impl EnumHint {
let mut s = String::new();

let mut iter = self.values.iter();
let write_item = |s: &mut String, item: &(String, Option<i64>)| match item {
(key, Some(val)) => {
write!(s, "{key}:{val}")
}
(key, None) => {
write!(s, "{key}")
}
};

if let Some(first) = iter.next() {
write_item(&mut s, first).unwrap();
write!(s, "{first}").unwrap();
}

for rest in iter {
write!(s, ",").unwrap();
write_item(&mut s, rest).unwrap();
write!(s, "{rest}").unwrap();
}

s.into()
}
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EnumHintEntry {
key: String,
value: Option<i64>,
}

impl EnumHintEntry {
#[inline]
pub fn new(key: String) -> Self {
Self { key, value: None }
}

#[inline]
pub fn with_value(key: String, value: i64) -> Self {
Self {
key,
value: Some(value),
}
}
}

impl Display for EnumHintEntry {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.key)?;
if let Some(value) = self.value {
write!(f, ":{}", value)?;
}
Ok(())
}
}

/// Possible hints for integers.
#[derive(Clone, Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -495,6 +519,6 @@ godot_test!(test_enum_hint_without_mapping {
});

godot_test!(test_enum_hint_with_mapping {
let hint = EnumHint::with_numbers(vec![("Foo".into(), 42), ("Bar".into(), 67)]);
let hint = EnumHint::with_values(vec![("Foo".into(), 42), ("Bar".into(), 67)]);
assert_eq!(hint.to_godot_hint_string().to_string(), "Foo:42,Bar:67".to_string(),);
});
23 changes: 13 additions & 10 deletions gdnative-derive/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::crate_gdnative_core;
use proc_macro2::{Span, TokenStream as TokenStream2};
use syn::spanned::Spanned;
use syn::{DeriveInput, Fields};
Expand Down Expand Up @@ -44,21 +45,23 @@ fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenS
quote! { (stringify!(#key).to_string(), #val) }
})
.collect::<Vec<_>>();
let gdnative_core = crate_gdnative_core();

let impl_block = quote! {
impl ::gdnative::export::Export for #enum_ty {
type Hint = ::gdnative::export::hint::IntHint<i64>;
#[inline]
fn export_info(hint: Option<Self::Hint>) -> ::gdnative::export::ExportInfo {
if let Some(hint) = hint {
return hint.export_info();
} else {
const _: () = {
pub enum NoHint {}

impl #gdnative_core::export::Export for #enum_ty {
type Hint = NoHint;

#[inline]
fn export_info(_hint: Option<Self::Hint>) -> #gdnative_core::export::ExportInfo {
let mappings = vec![ #(#mappings),* ];
let enum_hint = ::gdnative::export::hint::EnumHint::with_numbers(mappings);
return ::gdnative::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
let enum_hint = #gdnative_core::export::hint::EnumHint::with_values(mappings);
return #gdnative_core::export::hint::IntHint::<i64>::Enum(enum_hint).export_info();
}
}
}
};
};

Ok(impl_block)
Expand Down
2 changes: 0 additions & 2 deletions test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod test_as_arg;
mod test_async;
mod test_constructor;
mod test_derive;
mod test_export_enum;
mod test_free_ub;
mod test_generic_class;
mod test_indexed_props;
Expand Down Expand Up @@ -53,7 +52,6 @@ pub extern "C" fn run_tests(
status &= test_vararray_return::run_tests();
status &= test_variant_call_args::run_tests();
status &= test_variant_ops::run_tests();
status &= test_export_enum::run_tests();

Variant::new(status).leak()
}
Expand Down
30 changes: 0 additions & 30 deletions test/src/test_export_enum.rs

This file was deleted.

0 comments on commit 1f9c20c

Please sign in to comment.