1use bevy_utils::prelude::DebugName;
2
3use crate::{
4 archetype::ArchetypeId,
5 entity::{Entity, EntityNotSpawnedError},
6};
7
8#[derive(thiserror::Error, Clone, Copy, Debug, PartialEq, Eq)]
11pub enum QueryEntityError {
12 #[error("The query does not match entity {0}")]
16 QueryDoesNotMatch(Entity, ArchetypeId),
17 #[error("{0}")]
19 NotSpawned(#[from] EntityNotSpawnedError),
20 #[error("The entity with ID {0} was requested mutably more than once")]
24 AliasedMutability(Entity),
25}
26
27#[derive(Debug, thiserror::Error)]
30pub enum QuerySingleError {
31 #[error("No entities fit the query {0}")]
33 NoEntities(DebugName),
34 #[error("Multiple entities fit the query {0}")]
36 MultipleEntities(DebugName),
37}
38
39#[cfg(test)]
40mod test {
41 use crate::{prelude::World, query::QueryEntityError};
42 use bevy_ecs_macros::Component;
43
44 #[test]
45 fn query_does_not_match() {
46 let mut world = World::new();
47
48 #[derive(Component)]
49 struct Present1;
50 #[derive(Component)]
51 struct Present2;
52 #[derive(Component, Debug, PartialEq)]
53 struct NotPresent;
54
55 let entity = world.spawn((Present1, Present2));
56
57 let (entity, archetype_id) = (entity.id(), entity.archetype().id());
58
59 let result = world.query::<&NotPresent>().get(&world, entity);
60
61 assert_eq!(
62 result,
63 Err(QueryEntityError::QueryDoesNotMatch(entity, archetype_id))
64 );
65 }
66}