use std::fmt;
#[derive(Debug, Clone)]
pub enum EcsError {
EntityNotFound,
ComponentNotFound,
ArchetypeNotFound,
InvalidEntity,
CommandError(String),
SystemCycleDetected,
ScheduleError(String),
SystemNotFound,
EventQueueOverflow,
SerializationError(String),
DeserializationError(String),
ResourceNotFound(String),
ResourceLoadError(String),
ResourceMemoryOverflow(String),
ResourceDeallocError(String),
AssetLoadError(String),
AssetNotFound(String),
BatchTooLarge,
HierarchyError(String),
ResourceAlreadyExists(std::any::TypeId),
EntityCapacityExhausted,
ComponentRegistrationFailed(std::any::TypeId),
IoError(String),
SpawnError(SpawnError),
ValidationError(String),
HotReloadPanic,
}
#[derive(Debug, Clone)]
pub enum SpawnError {
EntityCapacityExhausted { attempted: usize, capacity: usize },
ComponentRegistrationFailed(String),
ArchetypeCreationFailed {
component_count: usize,
reason: String,
},
}
impl fmt::Display for SpawnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SpawnError::EntityCapacityExhausted {
attempted,
capacity,
} => {
write!(
f,
"Entity capacity exhausted: attempted to spawn {attempted}, max is {capacity}"
)
}
SpawnError::ComponentRegistrationFailed(reason) => {
write!(f, "Failed to register component: {reason}")
}
SpawnError::ArchetypeCreationFailed {
component_count,
reason,
} => {
write!(
f,
"Failed to create archetype for {component_count} components: {reason}"
)
}
}
}
}
impl fmt::Display for EcsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EcsError::EntityNotFound => write!(f, "Entity not found"),
EcsError::ComponentNotFound => write!(f, "Component not found"),
EcsError::ArchetypeNotFound => write!(f, "Archetype not found"),
EcsError::InvalidEntity => write!(f, "Invalid entity ID"),
EcsError::CommandError(msg) => write!(f, "Command error: {msg}"),
EcsError::SystemCycleDetected => write!(f, "System dependency cycle detected"),
EcsError::ScheduleError(msg) => write!(f, "Schedule error: {msg}"),
EcsError::SystemNotFound => write!(f, "System not found"),
EcsError::EventQueueOverflow => write!(f, "Event queue overflow"),
EcsError::SerializationError(msg) => write!(f, "Serialization error: {msg}"),
EcsError::DeserializationError(msg) => write!(f, "Deserialization error: {msg}"),
EcsError::ResourceNotFound(msg) => write!(f, "Resource not found: {msg}"),
EcsError::ResourceLoadError(msg) => write!(f, "Resource load error: {msg}"),
EcsError::ResourceMemoryOverflow(msg) => write!(f, "Resource memory overflow: {msg}"),
EcsError::ResourceDeallocError(msg) => write!(f, "Resource deallocation error: {msg}"),
EcsError::AssetLoadError(msg) => write!(f, "Asset load error: {msg}"),
EcsError::AssetNotFound(msg) => write!(f, "Asset not found: {msg}"),
EcsError::BatchTooLarge => write!(f, "Batch size too large (max 10,000,000)"),
EcsError::HierarchyError(msg) => write!(f, "Hierarchy error: {msg}"),
EcsError::ResourceAlreadyExists(type_id) => {
write!(f, "Resource already exists: {type_id:?}")
}
EcsError::EntityCapacityExhausted => write!(f, "Entity capacity exhausted"),
EcsError::ComponentRegistrationFailed(_) => write!(f, "Component registration failed"),
EcsError::IoError(msg) => write!(f, "IO error: {msg}"),
EcsError::SpawnError(spawn_err) => write!(f, "Spawn error: {spawn_err}"),
EcsError::ValidationError(msg) => write!(f, "Validation error: {msg}"),
EcsError::HotReloadPanic => write!(f, "Panic during hot-reload execution"),
}
}
}
impl std::error::Error for EcsError {}
impl From<std::io::Error> for EcsError {
fn from(err: std::io::Error) -> Self {
EcsError::IoError(err.to_string())
}
}
impl From<SpawnError> for EcsError {
fn from(err: SpawnError) -> Self {
EcsError::SpawnError(err)
}
}
pub type Result<T> = std::result::Result<T, EcsError>;