{"id":21985578,"url":"https://github.com/ickshonpe/conditional_commands","last_synced_at":"2026-03-15T09:44:31.688Z","repository":{"id":40319590,"uuid":"497883411","full_name":"ickshonpe/Conditional_Commands","owner":"ickshonpe","description":"Conditional component, bundle, and child insertion without the need for an intermediate EntityCommands binding","archived":false,"fork":false,"pushed_at":"2022-11-15T14:05:12.000Z","size":21,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-07T09:24:54.599Z","etag":null,"topics":["bevy","ecs","extensions","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ickshonpe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-30T09:55:04.000Z","updated_at":"2023-11-19T14:17:19.000Z","dependencies_parsed_at":"2023-01-22T11:45:57.075Z","dependency_job_id":null,"html_url":"https://github.com/ickshonpe/Conditional_Commands","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ickshonpe/Conditional_Commands","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ickshonpe%2FConditional_Commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ickshonpe%2FConditional_Commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ickshonpe%2FConditional_Commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ickshonpe%2FConditional_Commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ickshonpe","download_url":"https://codeload.github.com/ickshonpe/Conditional_Commands/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ickshonpe%2FConditional_Commands/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266535695,"owners_count":23944275,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["bevy","ecs","extensions","rust"],"created_at":"2024-11-29T18:14:05.650Z","updated_at":"2026-03-15T09:44:31.658Z","avatar_url":"https://github.com/ickshonpe.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Conditional Commands\n\nThis crate implements three extension traits that allow for conditional component, bundle and child insertion without the need for an intermediate ```EntityCommands``` or ```EntityMut``` binding.\n* ```ConditionalInsertBundleExt``` for ```EntityCommands``` and ```EntityMut```\\\n    Methods:\n    - ```insert_if```\n    - ```insert_if_else```\n    - ```insert_some```\n    - ```insert_some_or```\n* ```ConditionalChildBuilderExt``` for ```EntityCommands```\\\n    Methods:\n    - ```with_children_if```\n* ```ConditionalWorldChildBuilderExt``` for ```EntityMut```\\\n    Methods:\n    - ```with_children_if```\n\nSupports Bevy 0.9\n\n#\n\n## Usage\n\nTo add to a project either use:\n```\ncargo add conditional_commands\n```\n\nor manually add to your Cargo.toml:\n```toml\n[dependencies]\nconditional_commands = \"0.9.0\"\n```\n\n## A Motivating But Contrived ECS Fizz-Buzz Example\n\n```rust\nuse bevy::prelude::*;\nuse conditional_commands::*;\n\n#[derive(Component)]\nstruct FizzBuzzer;\n\n#[derive(Component)]\nstruct Number(usize);\n\n#[derive(Component)]\nstruct Fizz;\n\n#[derive(Component)]\nstruct Buzz;\n\nfn fizz_buzz\u003cconst N: usize\u003e(\n    mut commands: Commands\n) {\n    for n in 1 ..= N {\n        let mut entity_commands = commands.spawn(FizzBuzzer);\n        match (n % 3, n % 5) {\n            (0, 0) =\u003e { entity_commands.insert((Fizz, Buzz)); },\n            (0, _) =\u003e { entity_commands.insert(Fizz); },\n            (_, 0) =\u003e { entity_commands.insert(Buzz); },\n            _ =\u003e { entity_commands.insert(Number(n)); }\n        }\n    }\n}\n```\n\nWith Conditional Commands the intermediate EntityCommands binding in no longer required.\n\n```rust\nfn fizz_buzz\u003cconst N: usize\u003e(\n    mut commands: Commands\n) {\n    for n in 1 ..= N {\n        commands\n        .spawn(FizzBuzzer)\n        .insert_if(0 \u003c n % 3 \u0026\u0026 0 \u003c n % 5, || Number(n))\n        .insert_if(n % 3 == 0, || Fizz)\n        .insert_if(n % 5 == 0, || Buzz);\n    }\n}\n```\n\n`ConditionalInsertBundleExt` is also implemented for `EntityMut`:\n\n```rust\n#[derive(Component)]\nstruct Even;\n\n#[derive(Component)]\nstruct Odd;\n\nfn exclusive_system(world: \u0026mut World) {\n    for n in 0..10 {\n        world.spawn_empty().insert_if_else(n % 2 == 0, || Even, || Odd);\n    }\n}    \n```\nBundles passed to the `_else`/`_or` methods don't need to be the same type,\nas seen in the above example with the `Even` and `Odd` components.\n\nUse `insert_some` to insert the inner value of an optional bundle, if present.\n\n```rust\ncommands.spawn(MyBundle)\n    .insert_some(Some(OtherBundle::default()))\n    .insert_some_or(None::\u003cMyThing\u003e, AlternativeThing::default);\n```\n\n## Examples\n\n```\ncargo run --example exclusive\ncargo run --example fizz_buzz\ncargo run --example insert_if\ncargo run --example insert_if_2\ncargo run --example with_children_if\n\n```\n#\n## Notes\n\n* I haven't done any benchmarking. For performance critical systems it should be better to spawn entire bundles at once. \n\n* Earlier versions of this crate have both eager and lazy versions of each insertion method. I'm not sure the eager versions had any advantages (beyond no `||`), so they are gone.\n\n* I wasn't able to quite finesse the lifetimes to get a single generic child builder trait for both ```EntityCommands``` and ```EntityMut```.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fickshonpe%2Fconditional_commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fickshonpe%2Fconditional_commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fickshonpe%2Fconditional_commands/lists"}