use core::fmt::Display;
use crate::{component::ComponentDesc, Entity};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
NoSuchEntity(Entity),
MissingComponent(MissingComponent),
DoesNotMatch(Entity),
Filtered(Entity),
IncompleteBatch,
EntityOccupied(Entity),
}
impl Error {
pub(crate) fn into_anyhow(self) -> anyhow::Error {
#[cfg(not(feature = "std"))]
return anyhow::Error::msg(self);
#[cfg(feature = "std")]
return anyhow::Error::new(self);
}
pub(crate) fn try_into_missing_component(self) -> core::result::Result<MissingComponent, Self> {
if let Self::MissingComponent(v) = self {
Ok(v)
} else {
Err(self)
}
}
}
impl From<MissingComponent> for Error {
fn from(value: MissingComponent) -> Self {
Self::MissingComponent(value)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MissingComponent {
pub id: Entity,
pub desc: ComponentDesc,
}
pub type Result<T> = core::result::Result<T, Error>;
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::NoSuchEntity(id) => write!(f, "Entity {id} does not exist"),
Error::MissingComponent(inner) => Display::fmt(inner, f),
Error::DoesNotMatch(id) => {
write!(f, "Entity {id} did not match the query")
}
Error::Filtered(id) => {
write!(f, "Entity {id} did not match the dynamic filter predicate")
}
Error::IncompleteBatch => write!(
f,
"Attempt to spawn batch with insufficient number of components"
),
Error::EntityOccupied(current) => {
write!(f, "Attempt to spawn new entity occupied id {current}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for MissingComponent {}
impl Display for MissingComponent {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"Entity {} does not have the component {:?}",
self.id, self.desc
)
}
}