{"id":15657404,"url":"https://github.com/mrgvsv/bevy_enum_filter","last_synced_at":"2025-04-30T07:29:24.669Z","repository":{"id":60891027,"uuid":"546468014","full_name":"MrGVSV/bevy_enum_filter","owner":"MrGVSV","description":"Filter by enum variant in Bevy queries","archived":false,"fork":false,"pushed_at":"2024-02-26T10:27:11.000Z","size":34,"stargazers_count":26,"open_issues_count":3,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T03:24:27.181Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MrGVSV.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-06T05:56:31.000Z","updated_at":"2024-12-11T08:40:17.000Z","dependencies_parsed_at":"2024-10-23T05:09:37.896Z","dependency_job_id":null,"html_url":"https://github.com/MrGVSV/bevy_enum_filter","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":0.4444444444444444,"last_synced_commit":"75375df78b0dc910c77c38d3fea191cc72f6759b"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGVSV%2Fbevy_enum_filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGVSV%2Fbevy_enum_filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGVSV%2Fbevy_enum_filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrGVSV%2Fbevy_enum_filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MrGVSV","download_url":"https://codeload.github.com/MrGVSV/bevy_enum_filter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251660251,"owners_count":21623214,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T13:06:43.319Z","updated_at":"2025-04-30T07:29:24.649Z","avatar_url":"https://github.com/MrGVSV.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bevy_enum_filter\n\n[![Crates.io](https://img.shields.io/crates/v/bevy_enum_filter)](https://crates.io/crates/bevy_enum_filter)\n[![Docs](https://img.shields.io/docsrs/bevy_enum_filter)](https://docs.rs/bevy_enum_filter/) \n[![License](https://img.shields.io/crates/l/bevy_enum_filter)](./LICENSE.md) \n\nFilter queries by enum variants!\n\nIn Rust, enum variants *aren't* types. This means we normally can't filter for them in a Bevy `Query`. The alternative, then is to use a set of \"marker\" components. This works well enough, but we miss out on the semantics of using an enum (and the ability to perform enum-specific operations such as `match`-ing).\n\nThis crate allows us to query for entities based on the *specific* variant of an enum component:\n\n```rust\nuse bevy::prelude::*;\nuse bevy_enum_filter::prelude::*;\n\n#[derive(Component, EnumFilter)]\nenum ItemType {\n  Equippable(usize),\n  Weapon(usize),\n  Potion(usize)\n}\n\nfn apply_potion(item_query: Query\u003c(Entity, \u0026ItemType), Added\u003cEnum!(ItemType::Potion)\u003e\u003e) {\n  // ...\n}\n\nfn main() {\n  App::new()\n    // ...\n    .add_enum_filter::\u003cItemType\u003e()\n    .add_systems(Update, apply_potion)\n    .run()\n}\n```\n\n## 📲 Installation\n\nAdd the following to the `[dependencies]` section of your `Cargo.toml`.\n\n```text\nbevy_enum_filter = \"0.3.0\"\n```\n\n## 🤨 How it works\n\n*Surprise! It's just marker structs!*\n\nDeriving `EnumFilter` generates a module containing a marker struct per variant. The module is named using the enum's name (snake-cased) followed by `_filters`. For example, our `ItemType` enum generates the following module:\n\n```rust\n// Auto-generated!\nmod item_type_filters {\n  #[derive(bevy::prelude::Component)]\n  pub struct Equippable;\n  #[derive(bevy::prelude::Component)]\n  pub struct Weapon;\n  #[derive(bevy::prelude::Component)]\n  pub struct Potion;\n}\n```\n\nWhen we then register our enum using `app.add_enum_filter`, we are adding a system that watches for changes (additions/mutations) related to that enum component. The system will then add or remove the appropriate marker struct whenever there's a change.\n\nThe `Enum!` macro then takes the given enum path and grabs the corresponding marker struct from the module. So `Enum!(ItemType::Potion)` corresponds to the `item_type_filters::Potion` type.\n\n\u003e 📢: This is why you *must* have your generated module in scope!\n\n##### Caveats\n\nBecause this is basically just change detection under the hood, it's important to remember when the filter actually takes effect. By default, the system added by `app.add_enum_filter` runs in the `PostUpdate` stage. This means you will not see the filter work until the that stage is complete.\n\n\u003e  And remember that any components changed in `Update` would not see their filters work until `PostUpdate` anyway since we need to cross a stage boundary for commands to flush. This means we realistically only lose out on a single stage.\n\nIf you need it to run after a certain system or within a certain stage, you could always add the `watch_for_enum` system yourself.\n\n## 🕊 Bevy Compatibility\n\n| bevy   | bevy_enum_filter |\n| :----- | ---------------- |\n| 0.8.1  | 0.1.0            |\n| 0.11.x | 0.2.0            |\n| 0.12.x | 0.3.0            |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrgvsv%2Fbevy_enum_filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrgvsv%2Fbevy_enum_filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrgvsv%2Fbevy_enum_filter/lists"}