refactor: add thiserror/anyhow for asset error handling

This commit is contained in:
2025-07-23 19:47:44 -05:00
parent 50afd8c09f
commit 11e89a63d0
3 changed files with 43 additions and 10 deletions

32
Cargo.lock generated
View File

@@ -11,6 +11,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "anyhow"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.5.0" version = "1.5.0"
@@ -164,6 +170,7 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
name = "pacman" name = "pacman"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"lazy_static", "lazy_static",
"libc", "libc",
"once_cell", "once_cell",
@@ -171,6 +178,7 @@ dependencies = [
"rand", "rand",
"sdl2", "sdl2",
"spin_sleep", "spin_sleep",
"thiserror 1.0.69",
"tracing", "tracing",
"tracing-error", "tracing-error",
"tracing-subscriber", "tracing-subscriber",
@@ -188,7 +196,7 @@ dependencies = [
"integer-sqrt", "integer-sqrt",
"num-traits", "num-traits",
"rustc-hash", "rustc-hash",
"thiserror", "thiserror 2.0.12",
] ]
[[package]] [[package]]
@@ -375,13 +383,33 @@ dependencies = [
"unicode-ident", "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]] [[package]]
name = "thiserror" name = "thiserror"
version = "2.0.12" version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [ 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]] [[package]]

View File

@@ -15,6 +15,8 @@ spin_sleep = "1.3.2"
rand = "0.9.2" rand = "0.9.2"
pathfinding = "4.14" pathfinding = "4.14"
once_cell = "1.21.3" once_cell = "1.21.3"
thiserror = "1.0"
anyhow = "1.0"
[target.'cfg(target_os = "windows")'.dependencies.winapi] [target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3" version = "0.3"

View File

@@ -3,16 +3,16 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::io; use std::io;
use thiserror::Error;
#[derive(Debug)] #[derive(Error, Debug)]
pub enum AssetError { pub enum AssetError {
Io(io::Error), #[error("IO error: {0}")]
} Io(#[from] io::Error),
#[error("Asset not found: {0}")]
impl From<io::Error> for AssetError { NotFound(String),
fn from(e: io::Error) -> Self { #[error("Invalid asset format: {0}")]
AssetError::Io(e) InvalidFormat(String),
}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -63,6 +63,9 @@ mod imp {
use std::path::Path; use std::path::Path;
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> { pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
let path = Path::new("assets").join(asset.path()); let path = Path::new("assets").join(asset.path());
if !path.exists() {
return Err(AssetError::NotFound(asset.path().to_string()));
}
let bytes = fs::read(&path)?; let bytes = fs::read(&path)?;
Ok(Cow::Owned(bytes)) Ok(Cow::Owned(bytes))
} }