1use thiserror::Error;
2
3use crate::{
4 archetype::ArchetypeId,
5 entity::{Entity, EntityDoesNotExistError},
6};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum QueryEntityError {
12 QueryDoesNotMatch(Entity, ArchetypeId),
16 EntityDoesNotExist(EntityDoesNotExistError),
18 AliasedMutability(Entity),
22}
23
24impl From<EntityDoesNotExistError> for QueryEntityError {
25 fn from(error: EntityDoesNotExistError) -> Self {
26 QueryEntityError::EntityDoesNotExist(error)
27 }
28}
29
30impl core::error::Error for QueryEntityError {}
31
32impl core::fmt::Display for QueryEntityError {
33 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34 match *self {
35 Self::QueryDoesNotMatch(entity, _) => {
36 write!(f, "The query does not match entity {entity}")
37 }
38 Self::EntityDoesNotExist(error) => {
39 write!(f, "{error}")
40 }
41 Self::AliasedMutability(entity) => {
42 write!(
43 f,
44 "The entity with ID {entity} was requested mutably more than once"
45 )
46 }
47 }
48 }
49}
50
51#[derive(Debug, Error)]
54pub enum QuerySingleError {
55 #[error("No entities fit the query {0}")]
57 NoEntities(&'static str),
58 #[error("Multiple entities fit the query {0}")]
60 MultipleEntities(&'static str),
61}
62
63#[cfg(test)]
64mod test {
65 use crate::{prelude::World, query::QueryEntityError};
66 use bevy_ecs_macros::Component;
67
68 #[test]
69 fn query_does_not_match() {
70 let mut world = World::new();
71
72 #[derive(Component)]
73 struct Present1;
74 #[derive(Component)]
75 struct Present2;
76 #[derive(Component, Debug, PartialEq)]
77 struct NotPresent;
78
79 let entity = world.spawn((Present1, Present2));
80
81 let (entity, archetype_id) = (entity.id(), entity.archetype().id());
82
83 let result = world.query::<&NotPresent>().get(&world, entity);
84
85 assert_eq!(
86 result,
87 Err(QueryEntityError::QueryDoesNotMatch(entity, archetype_id))
88 );
89 }
90}