summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2025-03-11 14:55:07 -0400
committerLLLL Colonq <llll@colonq>2025-03-11 14:55:07 -0400
commit2a7b0690f4dbc33c9fc2b0b1eeb814b44eb20c30 (patch)
tree2d48e393109c61fe3ce591d634f2f80e9a9774df
parent22fbeca88a781aca7bcc965ac8189244497671d6 (diff)
Update
-rw-r--r--Cargo.lock59
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs25
-rw-r--r--src/state.rs12
-rw-r--r--src/utils.rs33
5 files changed, 94 insertions, 37 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e2d1d2c..be721d2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -20,9 +20,9 @@ checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046"
[[package]]
name = "addr2line"
-version = "0.22.0"
+version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
+checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb"
dependencies = [
"gimli",
]
@@ -160,12 +160,6 @@ dependencies = [
]
[[package]]
-name = "anyhow"
-version = "1.0.97"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
-
-[[package]]
name = "arrayref"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -197,9 +191,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
-version = "0.3.73"
+version = "0.3.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a"
+checksum = "26b05800d2e817c8b3b4b54abd461726265fa9789ae34330622f2db9ee696f9d"
dependencies = [
"addr2line",
"cc",
@@ -389,6 +383,19 @@ dependencies = [
]
[[package]]
+name = "color-eyre"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55146f5e46f237f7423d74111267d4597b59b0dad0ffaf7303bce9945d843ad5"
+dependencies = [
+ "backtrace",
+ "eyre",
+ "indenter",
+ "once_cell",
+ "owo-colors",
+]
+
+[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -714,6 +721,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365"
[[package]]
+name = "eyre"
+version = "0.6.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
+dependencies = [
+ "indenter",
+ "once_cell",
+]
+
+[[package]]
name = "fastrand"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -888,9 +905,9 @@ dependencies = [
[[package]]
name = "gimli"
-version = "0.29.0"
+version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
+checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "glam"
@@ -1180,6 +1197,12 @@ dependencies = [
]
[[package]]
+name = "indenter"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
+
+[[package]]
name = "indexmap"
version = "2.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1560,9 +1583,9 @@ checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33"
[[package]]
name = "object"
-version = "0.36.5"
+version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e"
+checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441"
dependencies = [
"memchr",
]
@@ -1665,6 +1688,12 @@ dependencies = [
]
[[package]]
+name = "owo-colors"
+version = "3.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
+
+[[package]]
name = "parking_lot"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2456,10 +2485,10 @@ dependencies = [
name = "teleia"
version = "0.1.0"
dependencies = [
- "anyhow",
"bimap",
"bitflags 2.6.0",
"bytes",
+ "color-eyre",
"console_error_panic_hook",
"console_log",
"directories",
diff --git a/Cargo.toml b/Cargo.toml
index 25be8fd..9cbffb4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -31,7 +31,7 @@ bimap = "*" # bijective maps
reqwest = "*" # http requests
bytes = "*" # bytes for http responses
bitflags = "*" # bitwise flags
-anyhow = "*" # generic error type handling
+color-eyre = { version = "*", default-features = false } # error reporting and formatting
[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = {version = "=0.29.15", features = ["serde"]} # windowing and events
diff --git a/src/lib.rs b/src/lib.rs
index 3c00719..9bbc60d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,8 +13,8 @@ pub mod audio;
pub mod net;
pub mod save;
-pub use utils::{erm, Erm};
-pub use anyhow::Context as ErmContext;
+pub use utils::{erm, install_error_handler, Erm};
+pub use color_eyre::eyre::WrapErr;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;
@@ -54,7 +54,7 @@ where
}
#[cfg(not(target_arch = "wasm32"))]
-pub async fn run<'a, F, G, Fut>(title: &str, w: u32, h: u32, options: Options, gnew: F)
+pub async fn run<'a, F, G, Fut>(title: &str, w: u32, h: u32, options: Options, gnew: F) -> Erm<()>
where
Fut: std::future::Future<Output = G>,
G: state::Game + 'static,
@@ -63,6 +63,7 @@ where
env_logger::Builder::new()
.filter(None, log::LevelFilter::Info)
.init();
+ color_eyre::install().expect("failed to install panic handler");
log::info!("hello computer, starting up...");
@@ -172,19 +173,15 @@ where
},
}
}
- st.run_update(&ctx, game);
- st.run_render(&ctx, game);
+ st.run_update(&ctx, game)?;
+ st.run_render(&ctx, game)?;
ctx.window.borrow_mut().swap_buffers();
}
-
- // event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);
- // event_loop.run(|event, elwt| {
- // event_loop_body::<G>(event, elwt);
- // }).expect("window closed");
+ Ok(())
}
#[cfg(target_arch = "wasm32")]
-pub async fn run<'a, F, G, Fut>(w: u32, h: u32, options: Options, gnew: F)
+pub async fn run<'a, F, G, Fut>(w: u32, h: u32, options: Options, gnew: F) -> Erm<()>
where
Fut: std::future::Future<Output = G>,
G: state::Game + 'static,
@@ -193,6 +190,7 @@ where
console_log::init_with_level(log::Level::Debug).unwrap();
console_error_panic_hook::set_once();
tracing_wasm::set_as_global_default();
+ color_eyre::install().expect("failed to install panic handler");
log::info!("hello computer, starting up...");
@@ -303,8 +301,8 @@ where
}
// f.poll();
}
- st.run_update(&ctx, game);
- st.run_render(&ctx, game);
+ st.run_update(&ctx, game)?;
+ st.run_render(&ctx, game)?;
ctx.window.request_redraw();
},
@@ -312,4 +310,5 @@ where
}
});
});
+ Ok(())
}
diff --git a/src/state.rs b/src/state.rs
index 7b2de9e..1316cf2 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -483,7 +483,7 @@ impl State {
game.request_return(ctx, self, res);
}
- pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game {
+ pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) -> utils::Erm<()> where G: Game {
let now = now(ctx);
let diff = now - self.last;
@@ -496,20 +496,19 @@ impl State {
self.acc -= DELTA_TIME;
self.tick += 1;
- // TODO: handle failure here in a nicer way
- game.update(ctx, self).expect("game update failed");
+ game.update(ctx, self)?;
// if a lot of time has elapsed (e.g. if window is unfocused and not
// running update loop), prevent "death spiral"
if self.acc >= DELTA_TIME { self.acc = 0.0 }
}
+ Ok(())
}
- pub fn run_render<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game {
+ pub fn run_render<G>(&mut self, ctx: &context::Context, game: &mut G) -> utils::Erm<()> where G: Game {
self.render_framebuffer.bind(&ctx);
- // TODO: handle failure here in a nicer way
- game.render(ctx, self).expect("game render failed");
+ game.render(ctx, self)?;
self.screen.bind(&ctx);
ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 0.0));
@@ -517,5 +516,6 @@ impl State {
self.shader_upscale.bind(&ctx);
self.render_framebuffer.bind_texture(&ctx);
ctx.render_no_geometry();
+ Ok(())
}
}
diff --git a/src/utils.rs b/src/utils.rs
index b2d1868..5d89310 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,9 +1,38 @@
use serde::{Serialize, Deserialize};
-pub type Erm<T> = anyhow::Result<T>;
+pub type Erm<T> = color_eyre::Result<T>;
pub fn erm<E, T>(e: E) -> Erm<T> where E: std::error::Error + std::marker::Send + std::marker::Sync + 'static {
- Err(anyhow::Error::from(e))
+ Err(e.into())
+}
+
+pub struct ErrorHandler;
+impl color_eyre::eyre::EyreHandler for ErrorHandler {
+ fn debug(
+ &self,
+ error: &(dyn std::error::Error + 'static),
+ f: &mut core::fmt::Formatter<'_>,
+ ) -> core::fmt::Result {
+ if f.alternate() {
+ return core::fmt::Debug::fmt(error, f);
+ }
+ let mut first = true;
+ if let Some(s) = error.source() {
+ let errors: Vec<_> = std::iter::successors(Some(s), |e| (*e).source()).collect();
+ for err in errors.iter().rev() {
+ writeln!(f)?; write!(f, "{}{}", if first {""} else {" - "}, err)?;
+ first = false;
+ }
+ }
+ writeln!(f)?; write!(f, "{}{}", if first {""} else {" - "}, error)?;
+ Ok(())
+ }
+}
+
+pub fn install_error_handler() {
+ let (panic_hook, _) = color_eyre::config::HookBuilder::default().into_hooks();
+ panic_hook.install();
+ color_eyre::eyre::set_hook(Box::new(move |_| Box::new(ErrorHandler))).expect("failed to install error handler");
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]