From 11e89a63d081e5e9f4e376546517fba72cd50c59 Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 23 Jul 2025 19:47:44 -0500 Subject: [PATCH] refactor: add thiserror/anyhow for asset error handling --- Cargo.lock | 32 ++++++++++++++++++++++++++++++-- Cargo.toml | 2 ++ src/asset.rs | 19 +++++++++++-------- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ec2150..53fea5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anyhow" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" + [[package]] name = "autocfg" version = "1.5.0" @@ -164,6 +170,7 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" name = "pacman" version = "0.1.0" dependencies = [ + "anyhow", "lazy_static", "libc", "once_cell", @@ -171,6 +178,7 @@ dependencies = [ "rand", "sdl2", "spin_sleep", + "thiserror 1.0.69", "tracing", "tracing-error", "tracing-subscriber", @@ -188,7 +196,7 @@ dependencies = [ "integer-sqrt", "num-traits", "rustc-hash", - "thiserror", + "thiserror 2.0.12", ] [[package]] @@ -375,13 +383,33 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "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", + "thiserror-impl 2.0.12", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ca1d382..ac7d440 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ spin_sleep = "1.3.2" rand = "0.9.2" pathfinding = "4.14" once_cell = "1.21.3" +thiserror = "1.0" +anyhow = "1.0" [target.'cfg(target_os = "windows")'.dependencies.winapi] version = "0.3" diff --git a/src/asset.rs b/src/asset.rs index 4b70d81..15d559a 100644 --- a/src/asset.rs +++ b/src/asset.rs @@ -3,16 +3,16 @@ use std::borrow::Cow; use std::io; +use thiserror::Error; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum AssetError { - Io(io::Error), -} - -impl From for AssetError { - fn from(e: io::Error) -> Self { - AssetError::Io(e) - } + #[error("IO error: {0}")] + Io(#[from] io::Error), + #[error("Asset not found: {0}")] + NotFound(String), + #[error("Invalid asset format: {0}")] + InvalidFormat(String), } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -63,6 +63,9 @@ mod imp { use std::path::Path; pub fn get_asset_bytes(asset: Asset) -> Result, AssetError> { let path = Path::new("assets").join(asset.path()); + if !path.exists() { + return Err(AssetError::NotFound(asset.path().to_string())); + } let bytes = fs::read(&path)?; Ok(Cow::Owned(bytes)) }