Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update syn to 2.0, clean up error handling, add closure support to #[with(..)] in derive. #343

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Gabeperson
Copy link

@Gabeperson Gabeperson commented Dec 30, 2024

This PR updates syn version to 2.0, adds span reporting for some derive errors, and adds closure support for #[with(..)].

Bevy uses syn 2.0 as well, so once the derive-getters dependency is updated to a version that uses syn 2.0 as well, users won't need to compile both syn 1.0 and 2.0 libraries.

I also changed a few functions to return errors instead of panicking so users get span information for some errors. (I might do another pass in the future to add error messages to all derive errors)

I also added closure support for the #[with(..)] attribute. Example below:

Previously, you'd have to make a separate function for every field you want custom constructors for. This is messy and reduces readability.

#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
    #[sprite_sheet]
    sprite_sheet: Sprite,
    player: Player,
    #[with(player_rigid_body)]
    rigid_body: RigidBody,
    #[with(player_collider)]
    collider: Collider,
    #[with(player_friction)]
    combine: Friction,
    #[with(player_locked)]
    locked: LockedAxes,
}

fn player_locked(_: &EntityInstance) -> LockedAxes {
    LockedAxes::ROTATION_LOCKED
}

fn player_friction(_: &EntityInstance) -> Friction {
    Friction::new(0.).with_combine_rule(CoefficientCombine::Min)
}

fn player_rigid_body(_: &EntityInstance) -> RigidBody {
    RigidBody::Dynamic
}

fn player_collider(_: &EntityInstance) -> Collider {
    Collider::rectangle(16., 16.)
}

With closure support, this can be shortened:

#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
    #[sprite_sheet]
    sprite_sheet: Sprite,
    player: Player,
    #[with(|_| RigidBody::Dynamic)]
    rigid_body: RigidBody,
    #[with(|_| Collider::rectangle(16., 16.))]
    collider: Collider,
    #[with(|_| Friction::new(0.).with_combine_rule(CoefficientCombine::Min))]
    combine: Friction,
    #[with(|_| LockedAxes::ROTATION_LOCKED)]
    locked: LockedAxes,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant