Skip to content

Commit

Permalink
review fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
patataofcourse committed Aug 3, 2023
1 parent 8e09d22 commit c92409e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
17 changes: 6 additions & 11 deletions godot-macros/src/derive_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ pub fn transform(decl: Declaration) -> ParseResult<TokenStream2> {
_ => unreachable!(),
};

let mut hint_string = String::new();
let hint_string;
if enum_.variants.is_empty() {
return bail!(
enum_.name,
"In order to derive Export, enums must have at least one variant"
);
} else {
for (i, (enum_v, _)) in enum_.variants.inner.iter().enumerate() {
let mut hint_string_segments = Vec::new();
for (enum_v, _) in enum_.variants.inner.iter() {
let v_name = enum_v.name.clone();
let v_disc = if let Some(c) = enum_v.value.clone() {
c.value
Expand All @@ -42,16 +43,9 @@ pub fn transform(decl: Declaration) -> ParseResult<TokenStream2> {
"Property can only be derived on enums with explicit discriminants in all their variants"
);
};
let v_disc_trimmed = v_disc.to_string().trim_matches(['(', ')'].as_slice()).to_string();

hint_string += &format!(
"{v_name}:{}{}",
v_disc.to_string().trim_matches(['(', ')'].as_slice()),
if i < enum_.variants.inner.len() - 1 {
","
} else {
""
}
);
hint_string_segments.push(format!("{v_name}:{v_disc_trimmed}"));

match &enum_v.contents {
StructFields::Unit => {}
Expand All @@ -63,6 +57,7 @@ pub fn transform(decl: Declaration) -> ParseResult<TokenStream2> {
}
};
}
hint_string = hint_string_segments.join(",");
}

let out = quote! {
Expand Down
14 changes: 7 additions & 7 deletions godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,11 @@ pub fn derive_from_variant(input: TokenStream) -> TokenStream {
/// - This will likely stay this way, since `isize`, the default repr type, is not a concept in Godot.
/// - The enum variants must not have any fields - currently only unit variants are supported.
/// - The enum variants must have explicit discriminants, that is, e.g. `A = 2`, not just `A`
/// - This will likely stay this way in order to keep proc-macro dependencies lighter.
///
/// Example :
/// # Example
///
/// ```ignore
/// ```no_run
/// # use godot::prelude::*;
/// #[repr(i32)]
/// #[derive(Debug, Property)]
/// enum TestEnum {
Expand All @@ -482,11 +482,12 @@ pub fn derive_from_variant(input: TokenStream) -> TokenStream {
/// foo: TestEnum
/// }
///
/// // This would not panic.
/// # fn main() {
/// let mut class = TestClass {foo: TestEnum::B}.get_foo();
/// assert!(class.get_foo() == TestEnum::B as i32);
/// assert_eq!(class.get_foo(), TestEnum::B as i32);
/// class.set_foo(TestEnum::A);
/// assert!(class.foo == TestEnum::A as i32);
/// assert_eq!(class.foo, TestEnum::A as i32);
/// # }
/// ```
#[proc_macro_derive(Property)]
pub fn derive_property(input: TokenStream) -> TokenStream {
Expand All @@ -499,7 +500,6 @@ pub fn derive_property(input: TokenStream) -> TokenStream {
/// - Only works for enums, structs aren't supported by this derive macro at the moment.
/// - The enum variants must not have any fields - currently only unit variants are supported.
/// - The enum variants must have explicit discriminants, that is, e.g. `A = 2`, not just `A`
/// - This will likely stay this way in order to keep proc-macro dependencies lighter.
/// ```
#[proc_macro_derive(Export)]
pub fn derive_export(input: TokenStream) -> TokenStream {
Expand Down
1 change: 0 additions & 1 deletion itest/rust/src/property_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ fn derive_export() {
let class: Gd<DeriveExport> = Gd::new_default();

let property = class
.bind()
.get_property_list()
.iter_shared()
.find(|c| c.get_or_nil("name") == "foo".to_variant())
Expand Down

0 comments on commit c92409e

Please sign in to comment.