From df99ef3a3f1641f4f753bb67de76f99c288532fc Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 13 Apr 2025 16:49:07 +0200 Subject: [PATCH] Add AppError --- Cargo.lock | 33 +++++++++++++++++++++++++++------ Cargo.toml | 1 + src/core/app/app.rs | 21 +++++++++++++-------- src/main.rs | 14 ++++++-------- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e35561c..da7454f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ dependencies = [ "ndk-context", "ndk-sys", "num_enum", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -384,7 +384,7 @@ dependencies = [ "polling", "rustix", "slab", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -863,7 +863,7 @@ dependencies = [ "combine", "jni-sys", "log", - "thiserror", + "thiserror 1.0.69", "walkdir", "windows-sys 0.45.0", ] @@ -975,7 +975,7 @@ dependencies = [ "ndk-sys", "num_enum", "raw-window-handle", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -1537,6 +1537,7 @@ dependencies = [ "env_logger", "glam", "log", + "thiserror 2.0.12", "vulkano", "vulkano-shaders", "winit", @@ -1695,7 +1696,7 @@ dependencies = [ "log", "memmap2", "rustix", - "thiserror", + "thiserror 1.0.69", "wayland-backend", "wayland-client", "wayland-csd-frame", @@ -1738,7 +1739,16 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl 2.0.12", ] [[package]] @@ -1752,6 +1762,17 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.1.8" diff --git a/Cargo.toml b/Cargo.toml index a6ece63..a84f681 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ publish = false [dependencies] anyhow = "1.0" +thiserror = "2.0" winit = { version = "0.30", features = ["rwh_06"] } vulkano = "0.35" diff --git a/src/core/app/app.rs b/src/core/app/app.rs index 12b4be7..21ebd28 100644 --- a/src/core/app/app.rs +++ b/src/core/app/app.rs @@ -7,7 +7,15 @@ pub enum AppExit { Error(Box), } -pub type RunnerFn = Box AppExit>; +pub type RunnerFn = Box AppExit>; + +#[derive(Debug, thiserror::Error)] +pub enum AppError { + #[error("Runner is not set")] + RunnerNotSet, + #[error("Runner returned an error : {0}")] + RunnerError(Box), +} pub struct App { world: World, @@ -35,16 +43,13 @@ impl App { &self.world } - pub fn run(&mut self) -> Result<(), Box> { + pub fn run(mut self) -> Result<(), AppError> { match self.runner.take() { - Some(runner) => match runner() { + Some(runner) => match runner(self) { AppExit::Success => Ok(()), - AppExit::Error(e) => Err(e), + AppExit::Error(e) => Err(AppError::RunnerError(e)), }, - None => Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - "runner is not set", - ))), + None => Err(AppError::RunnerNotSet), } } diff --git a/src/main.rs b/src/main.rs index f09236f..9412fad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,25 +5,23 @@ pub mod core; pub mod game; pub mod vulkan; -fn main() -> Result<(), Box> { +fn main() -> Result<(), impl Error> { env_logger::init(); - run_old_app() + run_new_app() } -fn run_new_app() -> Result<(), Box> { - let mut app = core::app::App::default(); +fn run_new_app() -> Result<(), impl Error> { + let app = core::app::App::default(); app.run() } -fn run_old_app() -> Result<(), Box> { +fn run_old_app() -> Result<(), impl Error> { let event_loop = EventLoop::new().unwrap(); event_loop.set_control_flow(ControlFlow::Poll); let vulkan_context = vulkan::vulkan_context::VulkanContext::from(&event_loop); let mut app = core::old_app::App::from(vulkan_context); - event_loop.run_app(&mut app).map_err(Box::new)?; - - Ok(()) + event_loop.run_app(&mut app) }