Skip to content

Commit 340541b

Browse files
authored
handle godot setting invalid types (#14)
Godot might set an invalid type like null when the value is not nullable.
1 parent 32c0f62 commit 340541b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

derive/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,24 @@ fn derive_set_fields<'a>(public_fields: impl Iterator<Item = &'a FieldOpts> + 'a
269269
Err(err) => return err.write_errors(),
270270
};
271271

272-
let variant_value = quote!(#godot_types::prelude::FromGodot::from_variant(&value));
272+
let variant_value = quote!(#godot_types::prelude::FromGodot::try_from_variant(&value));
273273

274274
let assignment = match opts.set {
275-
Some(setter) => quote_spanned!(setter.span() => #setter(self, #variant_value)),
276-
None => quote!(self.#field_ident = #variant_value),
275+
Some(setter) => quote_spanned!(setter.span() => #setter(self, local_value)),
276+
None => quote!(self.#field_ident = local_value),
277277
};
278278

279279
quote_spanned! {
280280
field_ident.span() =>
281-
#field_name => #assignment,
281+
#field_name => {
282+
let local_value = match #variant_value {
283+
Ok(v) => v,
284+
Err(_) => return false,
285+
};
286+
287+
#assignment;
288+
true
289+
},
282290
}
283291
})
284292
.collect();
@@ -288,10 +296,8 @@ fn derive_set_fields<'a>(public_fields: impl Iterator<Item = &'a FieldOpts> + 'a
288296
match name.to_string().as_str() {
289297
#set_field_dispatch
290298

291-
_ => return false,
299+
_ => false,
292300
}
293-
294-
true
295301
}
296302
}
297303
}

0 commit comments

Comments
 (0)