From 774be9ecb01d6360bd2f71a1ba32146c2aa95a44 Mon Sep 17 00:00:00 2001 From: spuds <71292624+bananaturtlesandwich@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:52:40 +0000 Subject: [PATCH] error refactor --- examples/list.rs | 10 ++++++++++ examples/read.rs | 8 -------- src/entry.rs | 45 ++++++++++++++++++++++++++------------------- src/error.rs | 20 +++++++++++--------- src/ext.rs | 2 +- src/footer.rs | 4 ++-- src/pak.rs | 2 +- 7 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 examples/list.rs delete mode 100644 examples/read.rs diff --git a/examples/list.rs b/examples/list.rs new file mode 100644 index 0000000..dfbb30b --- /dev/null +++ b/examples/list.rs @@ -0,0 +1,10 @@ +fn main() -> Result<(), un_pak::Error> { + let pak = un_pak::Pak::new( + std::io::BufReader::new(std::io::Cursor::new(include_bytes!("rando_p.pak"))), + un_pak::Version::CompressionEncryption, + )?; + for file in pak.entries.keys() { + println!("{file}") + } + Ok(()) +} diff --git a/examples/read.rs b/examples/read.rs deleted file mode 100644 index f542895..0000000 --- a/examples/read.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn main() -> Result<(), un_pak::Error> { - let pak = un_pak::Pak::new( - un_pak::Version::CompressionEncryption, - std::io::Cursor::new(include_bytes!("rando_p.pak")), - )?; - print!("{:#?}", pak); - Ok(()) -} diff --git a/src/entry.rs b/src/entry.rs index 13b1a28..52895f2 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,7 +1,24 @@ use byteorder::{ReadBytesExt, LE}; +use std::io; use super::{Compression, ReadExt, Version}; +#[derive(Debug)] +pub struct Block { + pub offset: u64, + /// size of the compressed block + pub size: u64, +} + +impl Block { + pub fn new(reader: &mut R) -> Result { + Ok(Self { + offset: reader.read_u64::()?, + size: reader.read_u64::()?, + }) + } +} + #[derive(Debug)] pub struct Entry { pub offset: u64, @@ -16,10 +33,7 @@ pub struct Entry { } impl Entry { - pub fn new( - reader: &mut R, - version: super::Version, - ) -> Result { + pub fn new(reader: &mut R, version: super::Version) -> Result { let offset = reader.read_u64::()?; let compressed = reader.read_u64::()?; let uncompressed = reader.read_u64::()?; @@ -50,21 +64,14 @@ impl Entry { }, }) } -} -#[derive(Debug)] -pub struct Block { - /// start offset relative to the start of the entry header - pub offset: u64, - /// size of the compressed block - pub size: u64, -} - -impl Block { - pub fn new(reader: &mut R) -> Result { - Ok(Self { - offset: reader.read_u64::()?, - size: reader.read_u64::()?, - }) + pub fn read( + self, + reader: &mut R, + version: super::Version, + ) -> Result, super::Error> { + let buf = io::BufWriter::new(Vec::new()); + todo!("read the stuff"); + Ok(buf.into_inner()?) } } diff --git a/src/error.rs b/src/error.rs index 13b60db..aa43810 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,19 +1,21 @@ #[derive(thiserror::Error, Debug)] pub enum Error { - #[error("found magic of {0:#x} instead of {:#x}", super::MAGIC)] - WrongMagic(u32), - #[error("used version {0} but pak is version {1}")] - WrongVersion(super::Version, super::Version), #[error("io error: {0}")] - IoError(#[from] std::io::Error), + Io(#[from] std::io::Error), #[error("enum conversion: {0}")] - StrumConv(#[from] strum::ParseError), + Strum(#[from] strum::ParseError), #[error("utf8 conversion: {0}")] - Utf8Conv(#[from] std::string::FromUtf8Error), + Utf8(#[from] std::string::FromUtf8Error), #[error("utf16 conversion: {0}")] - Utf16Conv(#[from] std::string::FromUtf16Error), + Utf16(#[from] std::string::FromUtf16Error), + #[error("bufwriter dereference: {0}")] + IntoInner(#[from] std::io::IntoInnerError>>), + #[error("found magic of {0:#x} instead of {:#x}", super::MAGIC)] + Magic(u32), + #[error("used version {0} but pak is version {1}")] + Version(super::Version, super::Version), #[error("got {0}, which is not a boolean")] - BoolConv(u8), + Bool(u8), #[error("{0}")] Other(String), } diff --git a/src/ext.rs b/src/ext.rs index 09f72bf..90e8835 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -16,7 +16,7 @@ impl ReadExt for R { match self.read_u8()? { 1 => Ok(true), 0 => Ok(false), - err => Err(super::Error::BoolConv(err)), + err => Err(super::Error::Bool(err)), } } diff --git a/src/footer.rs b/src/footer.rs index 0f56416..8f48e97 100644 --- a/src/footer.rs +++ b/src/footer.rs @@ -54,10 +54,10 @@ impl Footer { }, }; if super::MAGIC != footer.magic { - return Err(super::Error::WrongMagic(footer.magic)); + return Err(super::Error::Magic(footer.magic)); } if version != footer.version { - return Err(super::Error::WrongVersion(version, footer.version)); + return Err(super::Error::Version(version, footer.version)); } Ok(footer) } diff --git a/src/pak.rs b/src/pak.rs index 898a4fa..68e26cd 100644 --- a/src/pak.rs +++ b/src/pak.rs @@ -15,8 +15,8 @@ pub struct Pak { impl Pak { pub fn new( - version: super::Version, mut reader: R, + version: super::Version, ) -> Result { reader.seek(io::SeekFrom::End(-version.size()))?; let footer = super::Footer::new(&mut reader, version)?;