Replace anyhow with thiserror for oodle_loader

This commit is contained in:
Truman Kilen 2025-01-17 18:27:22 -06:00
parent 255486b962
commit dbe16c9001
4 changed files with 28 additions and 19 deletions

8
Cargo.lock generated
View file

@ -68,12 +68,6 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "anyhow"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
[[package]]
name = "assert_cmd"
version = "2.0.16"
@ -668,10 +662,10 @@ checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
name = "oodle_loader"
version = "0.2.2"
dependencies = [
"anyhow",
"hex",
"libloading",
"sha2",
"thiserror",
"ureq",
]

View file

@ -10,5 +10,5 @@ edition.workspace = true
libloading = "0.8"
ureq = "2.12"
hex = { workspace = true }
anyhow = "1.0.95"
sha2 = "0.10.8"
thiserror = "2.0.11"

View file

@ -1,10 +1,10 @@
use anyhow::{bail, Result};
use std::{
io::{Read, Write},
sync::OnceLock,
};
type Result<T, E = Error> = std::result::Result<T, E>;
pub use oodle_lz::{CompressionLevel, Compressor};
mod oodle_lz {
@ -125,6 +125,22 @@ fn url() -> String {
)
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("hash mismatch expected: {expected} got {found}")]
HashMismatch { expected: String, found: String },
#[error("Oodle compression failed")]
CompressionFailed,
#[error("Oodle initialization failed previously")]
InitializationFailed,
#[error("IO error {0:?}")]
Io(#[from] std::io::Error),
#[error("ureq error {0:?}")]
Ureq(#[from] ureq::Error),
#[error("libloading error {0:?}")]
LibLoading(#[from] libloading::Error),
}
fn check_hash(buffer: &[u8]) -> Result<()> {
use sha2::{Digest, Sha256};
@ -132,11 +148,10 @@ fn check_hash(buffer: &[u8]) -> Result<()> {
hasher.update(buffer);
let hash = hex::encode(hasher.finalize());
if hash != OODLE_PLATFORM.hash {
anyhow::bail!(
"Oodle library hash mismatch: expected {} got {}",
OODLE_PLATFORM.hash,
hash
);
return Err(Error::HashMismatch {
expected: OODLE_PLATFORM.hash.into(),
found: hash,
});
}
Ok(())
@ -189,7 +204,7 @@ impl Oodle {
);
if len == -1 {
bail!("Oodle compression failed");
return Err(Error::CompressionFailed);
}
let len = len as usize;
@ -244,7 +259,7 @@ fn load_oodle() -> Result<Oodle> {
}
}
pub fn oodle() -> Result<&'static Oodle, Box<dyn std::error::Error>> {
pub fn oodle() -> Result<&'static Oodle> {
let mut result = None;
let oodle = OODLE.get_or_init(|| match load_oodle() {
Err(err) => {
@ -259,7 +274,7 @@ pub fn oodle() -> Result<&'static Oodle, Box<dyn std::error::Error>> {
// error during initialization
(Some(result), _) => result?,
// no error because initialization was tried and failed before
_ => Err(anyhow::anyhow!("oodle failed to initialized previously").into()),
_ => Err(Error::InitializationFailed),
}
}

View file

@ -11,7 +11,7 @@ pub const MAGIC: u32 = 0x5A6F12E1;
#[cfg(feature = "oodle")]
mod oodle {
pub type OodleGetter = fn() -> Result<&'static oodle_loader::Oodle, Box<dyn std::error::Error>>;
pub type OodleGetter = fn() -> Result<&'static oodle_loader::Oodle, oodle_loader::Error>;
pub type OodleDecompress = fn(comp_buf: &[u8], raw_buf: &mut [u8]) -> i32;
}