use thiserror::Error;
use crate::{
archetype::ArchetypeId,
entity::{Entity, EntityDoesNotExistError},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QueryEntityError {
QueryDoesNotMatch(Entity, ArchetypeId),
EntityDoesNotExist(EntityDoesNotExistError),
AliasedMutability(Entity),
}
impl From<EntityDoesNotExistError> for QueryEntityError {
fn from(error: EntityDoesNotExistError) -> Self {
QueryEntityError::EntityDoesNotExist(error)
}
}
impl core::error::Error for QueryEntityError {}
impl core::fmt::Display for QueryEntityError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Self::QueryDoesNotMatch(entity, _) => {
write!(f, "The query does not match entity {entity}")
}
Self::EntityDoesNotExist(error) => {
write!(f, "{error}")
}
Self::AliasedMutability(entity) => {
write!(
f,
"The entity with ID {entity} was requested mutably more than once"
)
}
}
}
}
#[derive(Debug, Error)]
pub enum QuerySingleError {
#[error("No entities fit the query {0}")]
NoEntities(&'static str),
#[error("Multiple entities fit the query {0}")]
MultipleEntities(&'static str),
}
#[cfg(test)]
mod test {
use crate::{prelude::World, query::QueryEntityError};
use bevy_ecs_macros::Component;
#[test]
fn query_does_not_match() {
let mut world = World::new();
#[derive(Component)]
struct Present1;
#[derive(Component)]
struct Present2;
#[derive(Component, Debug, PartialEq)]
struct NotPresent;
let entity = world.spawn((Present1, Present2));
let (entity, archetype_id) = (entity.id(), entity.archetype().id());
let result = world.query::<&NotPresent>().get(&world, entity);
assert_eq!(
result,
Err(QueryEntityError::QueryDoesNotMatch(entity, archetype_id))
);
}
}