From 5104b41878cb189dd8b9728f03910b7a5cc3796c Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Thu, 5 Jan 2023 20:20:05 +0000 Subject: [PATCH] code cleanup --- Cargo.toml | 1 - examples/read.rs | 4 +--- src/error.rs | 10 +++++++--- src/footer.rs | 11 ++--------- src/lib.rs | 6 +++--- src/pak.rs | 5 +++-- 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a775799..9897587 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ edition = "2021" [dependencies] byteorder = "*" strum = { version = "*", features = ["derive"] } -hashbrown = "*" thiserror = "*" [profile.release] diff --git a/examples/read.rs b/examples/read.rs index 9b41199..6e6f637 100644 --- a/examples/read.rs +++ b/examples/read.rs @@ -8,7 +8,5 @@ fn main() -> Result<(), un_pak::Error> { Err(e) => println!("{e}"), } } - Err(un_pak::Error::PakInvalid( - "no version can parse".to_string(), - )) + Err(un_pak::Error::Other("no version can parse".to_string())) } diff --git a/src/error.rs b/src/error.rs index 6772c50..23a2543 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,13 +1,17 @@ #[derive(thiserror::Error, Debug)] pub enum Error { - #[error("error parsing pak: {0}")] - PakInvalid(String), + #[error("expected magic of {} but found {0}", super::MAGIC)] + MagicMismatch(u32), + #[error("parsed with {0} but pak was {0}")] + VersionMismatch(super::Version, super::Version), #[error("error reading file: {0}")] IoError(#[from] std::io::Error), - #[error("error converting to enum: {0}")] + #[error("error converting enum: {0}")] StrumError(#[from] strum::ParseError), #[error("error converting to utf8: {0}")] Utf8Error(#[from] std::string::FromUtf8Error), #[error("error converting to utf16: {0}")] Utf16Error(#[from] std::string::FromUtf16Error), + #[error("{0}")] + Other(String), } diff --git a/src/footer.rs b/src/footer.rs index f7f60cb..d74f484 100644 --- a/src/footer.rs +++ b/src/footer.rs @@ -52,17 +52,10 @@ impl Footer { }), }; if super::MAGIC != footer.magic { - return Err(super::Error::PakInvalid(format!( - "incorrect magic - expected {} but got {}", - super::MAGIC, - footer.magic - ))); + return Err(super::Error::MagicMismatch(footer.magic)); } if version != &footer.version { - return Err(super::Error::PakInvalid(format!( - "incorrect version - parsed with {} but is {}", - version, footer.version - ))); + return Err(super::Error::VersionMismatch(*version, footer.version)); } Ok(footer) } diff --git a/src/lib.rs b/src/lib.rs index 36928d3..b49f359 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,8 @@ pub const MAGIC: u32 = 0x5A6F12E1; #[repr(u32)] #[derive( Default, - Copy, Clone, + Copy, PartialEq, Eq, PartialOrd, @@ -38,14 +38,14 @@ pub enum Version { PathHashIndex, // more compression methods } -// strum shouldn't need to be installed +// strum shouldn't need to be installed by users impl Version { pub fn iter() -> VersionIter { ::iter() } } -#[derive(Default, Copy, Clone, PartialEq, Eq, Debug, strum::Display, strum::EnumString)] +#[derive(Default, Clone, Copy, PartialEq, Eq, Debug, strum::Display, strum::EnumString)] pub enum Compression { #[default] None, diff --git a/src/pak.rs b/src/pak.rs index d6cda17..cf6484b 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -5,7 +5,7 @@ use super::Version; pub struct Pak { pub version: Version, pub footer: super::Footer, - pub entries: hashbrown::HashMap, + pub index: super::Index, } impl Pak { @@ -17,10 +17,11 @@ impl Pak { // parse footer info to get index offset let footer = super::Footer::new(&mut reader, &version)?; reader.seek(io::SeekFrom::Start(footer.offset))?; + let index = super::Index::new(&mut reader, &version)?; Ok(Self { version, footer, - entries: hashbrown::HashMap::new(), + index, }) } }