use crate::storage::StorageId;
use crate::EntityId;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::fmt::{Debug, Display, Formatter};
#[cfg(feature = "std")]
use std::error::Error;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Borrow {
Unique,
Shared,
WrongThread,
MultipleThreads,
}
#[cfg(feature = "std")]
impl Error for Borrow {}
impl Debug for Borrow {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::Unique => f.write_str("Cannot mutably borrow while already borrowed."),
Self::Shared => f.write_str("Cannot immutably borrow while already mutably borrowed."),
Self::WrongThread => {
f.write_str("Can't access from another thread because it's !Send and !Sync.")
}
Self::MultipleThreads => f.write_str(
"Can't access from multiple threads at the same time because it's !Sync.",
),
}
}
}
impl Display for Borrow {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GetStorage {
#[allow(missing_docs)]
AllStoragesBorrow(Borrow),
#[allow(missing_docs)]
StorageBorrow {
name: Option<&'static str>,
id: StorageId,
borrow: Borrow,
},
#[allow(missing_docs)]
Entities(Borrow),
#[allow(missing_docs)]
MissingStorage {
name: Option<&'static str>,
id: StorageId,
},
}
#[cfg(feature = "std")]
impl Error for GetStorage {}
impl Debug for GetStorage {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::AllStoragesBorrow(borrow) => match borrow {
Borrow::Unique => f.write_str("Cannot mutably borrow AllStorages while it's already borrowed (AllStorages is borrowed to access any storage)."),
Borrow::Shared => {
f.write_str("Cannot immutably borrow AllStorages while it's already mutably borrowed.")
},
_ => unreachable!(),
},
Self::StorageBorrow {name, id, borrow} => if let Some(name) = name {
match borrow {
Borrow::Unique => f.write_fmt(format_args!("Cannot mutably borrow {} storage while it's already borrowed.", name)),
Borrow::Shared => {
f.write_fmt(format_args!("Cannot immutably borrow {} storage while it's already mutably borrowed.", name))
},
Borrow::MultipleThreads => f.write_fmt(format_args!("Cannot borrow {} storage from multiple thread at the same time because it's !Sync.", name)),
Borrow::WrongThread => f.write_fmt(format_args!("Cannot borrow {} storage from other thread than the one it was created in because it's !Send and !Sync.", name)),
}
} else {
match borrow {
Borrow::Unique => f.write_fmt(format_args!("Cannot mutably borrow {:?} storage while it's already borrowed.", id)),
Borrow::Shared => {
f.write_fmt(format_args!("Cannot immutably borrow {:?} storage while it's already mutably borrowed.", id))
},
Borrow::MultipleThreads => f.write_fmt(format_args!("Cannot borrow {:?} storage from multiple thread at the same time because it's !Sync.", id)),
Borrow::WrongThread => f.write_fmt(format_args!("Cannot borrow {:?} storage from other thread than the one it was created in because it's !Send and !Sync.", id)),
}
}
Self::Entities(borrow) => match borrow {
Borrow::Unique => f.write_str("Cannot mutably borrow Entities storage while it's already borrowed."),
Borrow::Shared => {
f.write_str("Cannot immutably borrow Entities storage while it's already mutably borrowed.")
},
_ => unreachable!(),
},
Self::MissingStorage { name, id } => if let Some(name) = name {
f.write_fmt(format_args!("{} storage was not found in the World. You can register unique storage with: world.add_unique(your_unique);", name))
} else {
f.write_fmt(format_args!("{:?} storage was not found in the World. You can register unique storage with: world.add_unique(your_unique);", id))
}
}
}
}
impl Display for GetStorage {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum NewEntity {
AllStoragesBorrow(Borrow),
Entities(Borrow),
}
#[cfg(feature = "std")]
impl Error for NewEntity {}
impl Debug for NewEntity {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::AllStoragesBorrow(borrow) => match borrow {
Borrow::Unique => f.write_str("Cannot mutably borrow all storages while it's already borrowed (this include component storage)."),
Borrow::Shared => {
f.write_str("Cannot immutably borrow all storages while it's already mutably borrowed.")
},
_ => unreachable!(),
},
Self::Entities(borrow) => match borrow {
Borrow::Unique => f.write_str("Cannot mutably borrow entities while it's already borrowed."),
_ => unreachable!(),
},
}
}
}
impl Display for NewEntity {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum AddComponent {
#[allow(missing_docs)]
EntityIsNotAlive,
}
#[cfg(feature = "std")]
impl Error for AddComponent {}
impl Debug for AddComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::EntityIsNotAlive => f.write_str("Entity has to be alive to add component to it."),
}
}
}
impl Display for AddComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, PartialEq, Eq)]
pub enum AddWorkload {
AlreadyExists,
Borrow,
UnknownWorkload(Cow<'static, str>, Cow<'static, str>),
}
#[cfg(feature = "std")]
impl Error for AddWorkload {}
impl Debug for AddWorkload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::AlreadyExists => f.write_str("A workload with this name already exists."),
Self::Borrow => {
f.write_str("Cannot mutably borrow the scheduler while it's already borrowed.")
}
Self::UnknownWorkload(workload, unknown_workload) => f.write_fmt(format_args!(
"Could not find {} workload while building {}'s batches.",
unknown_workload, workload
)),
}
}
}
impl Display for AddWorkload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum SetDefaultWorkload {
Borrow,
MissingWorkload,
}
#[cfg(feature = "std")]
impl Error for SetDefaultWorkload {}
impl Debug for SetDefaultWorkload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::Borrow => {
f.write_str("Cannot mutably borrow scheduler while it's already borrowed.")
}
Self::MissingWorkload => f.write_str("No workload with this name exists."),
}
}
}
impl Display for SetDefaultWorkload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
pub enum RunWorkload {
Scheduler,
Run((&'static str, Run)),
MissingWorkload,
}
impl RunWorkload {
#[cfg(feature = "std")]
pub fn custom_error(self) -> Option<Box<dyn Error + Send + Sync>> {
match self {
Self::Run((_, Run::Custom(error))) => Some(error),
_ => None,
}
}
#[cfg(not(feature = "std"))]
pub fn custom_error(self) -> Option<Box<dyn core::any::Any + Send>> {
match self {
Self::Run((_, Run::Custom(error))) => Some(error),
_ => None,
}
}
}
#[cfg(feature = "std")]
impl Error for RunWorkload {}
impl Debug for RunWorkload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::Scheduler => {
f.write_str("Cannot borrow the scheduler while it's already mutably borrowed.")
}
Self::MissingWorkload => f.write_str("No workload with this name exists."),
Self::Run((system_name, run)) => {
f.write_fmt(format_args!("System {} failed: {:?}", system_name, run))
}
}
}
}
impl Display for RunWorkload {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
pub enum Run {
GetStorage(GetStorage),
#[cfg(feature = "std")]
Custom(Box<dyn Error + Send + Sync>),
#[cfg(not(feature = "std"))]
Custom(Box<dyn core::any::Any + Send>),
}
impl From<GetStorage> for Run {
fn from(get_storage: GetStorage) -> Self {
Run::GetStorage(get_storage)
}
}
impl Run {
#[cfg(feature = "std")]
pub(crate) fn from_custom<E: Into<Box<dyn Error + Send + Sync>>>(error: E) -> Self {
Run::Custom(error.into())
}
#[cfg(not(feature = "std"))]
pub(crate) fn from_custom<E: core::any::Any + Send>(error: E) -> Self {
Run::Custom(Box::new(error))
}
}
#[cfg(feature = "std")]
impl Error for Run {}
impl Debug for Run {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::GetStorage(get_storage) => Debug::fmt(&get_storage, f),
Self::Custom(err) => {
f.write_fmt(format_args!("run failed with a custom error, {:?}.", err))
}
}
}
}
impl Display for Run {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct MissingComponent {
pub id: EntityId,
pub name: &'static str,
}
#[cfg(feature = "std")]
impl Error for MissingComponent {}
impl Debug for MissingComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
f.write_fmt(format_args!(
"{:?} does not have a {} component.",
self.id, self.name
))
}
}
impl Display for MissingComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum InvalidSystem {
AllStorages,
MultipleViews,
MultipleViewsMut,
}
#[cfg(feature = "std")]
impl Error for InvalidSystem {}
impl Debug for InvalidSystem {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::AllStorages => f.write_str("A system borrowing both AllStorages and a storage can't run. You can borrow the storage inside the system with AllStorages::borrow or AllStorages::run instead."),
Self::MultipleViews => f.write_str("Multiple views of the same storage including an exclusive borrow, consider removing the shared borrow."),
Self::MultipleViewsMut => f.write_str("Multiple exclusive views of the same storage, consider removing one."),
}
}
}
impl Display for InvalidSystem {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum UniqueRemove {
AllStorages,
MissingUnique(&'static str),
StorageBorrow((&'static str, Borrow)),
}
#[cfg(feature = "std")]
impl Error for UniqueRemove {}
impl Debug for UniqueRemove {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::AllStorages => f.write_str("Cannot borrow AllStorages while it's already exclusively borrowed."),
Self::MissingUnique(name) => f.write_fmt(format_args!("No unique storage exists for {}.\n", name)),
Self::StorageBorrow((name, borrow)) => match borrow {
Borrow::Unique => f.write_fmt(format_args!("Cannot mutably borrow {} storage while it's already borrowed.", name)),
Borrow::WrongThread => f.write_fmt(format_args!("Cannot borrow {} storage from other thread than the one it was created in because it's !Send and !Sync.", name)),
_ => unreachable!()
}
}
}
}
impl Display for UniqueRemove {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Apply {
#[allow(missing_docs)]
IdenticalIds,
MissingComponent(EntityId),
}
#[cfg(feature = "std")]
impl Error for Apply {}
impl Debug for Apply {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
Self::IdenticalIds => f.write_str("Cannot use apply with identical components."),
Self::MissingComponent(id) => f.write_fmt(format_args!(
"Entity {:?} does not have any component in this storage.",
id
)),
}
}
}
impl Display for Apply {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
Debug::fmt(self, f)
}
}