Skip to content

Commit

Permalink
Add helpful error when target is not added in derive macro
Browse files Browse the repository at this point in the history
  • Loading branch information
aevyrie committed Jul 20, 2024
1 parent f22378d commit 68b2c94
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Added the `E: EntityEvent` bound to `EventlistenerPlugin<E>`, to move compile errors from adding the plugin, to the event itself.
- Fixed a benchmark bug.
- Added helpful error message when `EntityEvent` derive macro fails to find `#[target]`.

# 0.8.0

Expand Down
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_eventlistener_derive"
version = "0.8.0"
version = "0.8.1"
edition = "2021"
description = "Event listeners and callbacks for bevy"
license = "MIT OR Apache-2.0"
Expand Down
14 changes: 8 additions & 6 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ pub fn derive(input: TokenStream) -> TokenStream {
// Get attributes #[..] on each field
for attr in field.attrs.iter() {
// Parse the attribute
match attr.meta {
// Find the duplicated idents
syn::Meta::Path(ref path) if path.get_ident().unwrap() == "target" => {
target = Some(field.ident.clone());
if let syn::Meta::Path(ref path) = attr.meta {
if let Some(ident) = path.get_ident() {
if ident == "target" {
target = Some(field.ident.clone());
}
}
_ => (),
}
}
}
Expand All @@ -44,7 +44,9 @@ pub fn derive(input: TokenStream) -> TokenStream {
_ => panic!("Must be a struct"),
}

let target = target.unwrap();
let Some(target) = target else {
panic!("Missing `#[target] attribute. You must annotate the field with the target Entity, or instead manually implement EntityEvent.")
};

let gen = quote! {
impl #impl_generics EntityEvent for #name #ty_generics #where_clause {
Expand Down

0 comments on commit 68b2c94

Please sign in to comment.