use std::fmt;
#[derive(Debug, Clone)]
pub enum EngineError {
WindowCreation(String),
GpuInit(String),
AssetLoad { path: String, reason: String },
InvalidHandle(String),
SceneNotFound(String),
Command { name: String, reason: String },
Other(String),
}
impl fmt::Display for EngineError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::WindowCreation(msg) => write!(f, "window creation failed: {msg}"),
Self::GpuInit(msg) => write!(f, "GPU initialization failed: {msg}"),
Self::AssetLoad { path, reason } => {
write!(f, "failed to load asset '{path}': {reason}")
}
Self::InvalidHandle(msg) => write!(f, "invalid handle: {msg}"),
Self::SceneNotFound(name) => write!(f, "scene not found: '{name}'"),
Self::Command { name, reason } => {
write!(f, "command '{name}' failed: {reason}")
}
Self::Other(msg) => write!(f, "error: {msg}"),
}
}
}
impl std::error::Error for EngineError {}
pub type EngineResult<T> = Result<T, EngineError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorSource {
Render,
Input,
Asset,
Audio,
Core,
Game,
}
#[derive(Clone)]
pub struct ErrorEvent {
pub source: ErrorSource,
pub error: EngineError,
pub recovered: bool,
}
impl ErrorEvent {
#[must_use]
pub const fn new(source: ErrorSource, error: EngineError) -> Self {
Self {
source,
error,
recovered: false,
}
}
#[must_use]
pub const fn recovered(mut self) -> Self {
self.recovered = true;
self
}
}
impl bevy_ecs::event::Event for ErrorEvent {
type Trigger<'a> = bevy_ecs::event::GlobalTrigger;
}