error refactor

This commit is contained in:
spuds 2023-01-11 13:52:40 +00:00
parent 100c4a189f
commit 774be9ecb0
No known key found for this signature in database
GPG key ID: 0B6CA6068E827C8F
7 changed files with 51 additions and 40 deletions

10
examples/list.rs Normal file
View file

@ -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(())
}

View file

@ -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(())
}

View file

@ -1,7 +1,24 @@
use byteorder::{ReadBytesExt, LE}; use byteorder::{ReadBytesExt, LE};
use std::io;
use super::{Compression, ReadExt, Version}; 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<R: io::Read>(reader: &mut R) -> Result<Self, super::Error> {
Ok(Self {
offset: reader.read_u64::<LE>()?,
size: reader.read_u64::<LE>()?,
})
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Entry { pub struct Entry {
pub offset: u64, pub offset: u64,
@ -16,10 +33,7 @@ pub struct Entry {
} }
impl Entry { impl Entry {
pub fn new<R: std::io::Read>( pub fn new<R: io::Read>(reader: &mut R, version: super::Version) -> Result<Self, super::Error> {
reader: &mut R,
version: super::Version,
) -> Result<Self, super::Error> {
let offset = reader.read_u64::<LE>()?; let offset = reader.read_u64::<LE>()?;
let compressed = reader.read_u64::<LE>()?; let compressed = reader.read_u64::<LE>()?;
let uncompressed = reader.read_u64::<LE>()?; let uncompressed = reader.read_u64::<LE>()?;
@ -50,21 +64,14 @@ impl Entry {
}, },
}) })
} }
}
#[derive(Debug)] pub fn read<R: io::Read + io::Seek>(
pub struct Block { self,
/// start offset relative to the start of the entry header reader: &mut R,
pub offset: u64, version: super::Version,
/// size of the compressed block ) -> Result<Vec<u8>, super::Error> {
pub size: u64, let buf = io::BufWriter::new(Vec::new());
} todo!("read the stuff");
Ok(buf.into_inner()?)
impl Block {
pub fn new<R: std::io::Read>(reader: &mut R) -> Result<Self, super::Error> {
Ok(Self {
offset: reader.read_u64::<LE>()?,
size: reader.read_u64::<LE>()?,
})
} }
} }

View file

@ -1,19 +1,21 @@
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { 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}")] #[error("io error: {0}")]
IoError(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error("enum conversion: {0}")] #[error("enum conversion: {0}")]
StrumConv(#[from] strum::ParseError), Strum(#[from] strum::ParseError),
#[error("utf8 conversion: {0}")] #[error("utf8 conversion: {0}")]
Utf8Conv(#[from] std::string::FromUtf8Error), Utf8(#[from] std::string::FromUtf8Error),
#[error("utf16 conversion: {0}")] #[error("utf16 conversion: {0}")]
Utf16Conv(#[from] std::string::FromUtf16Error), Utf16(#[from] std::string::FromUtf16Error),
#[error("bufwriter dereference: {0}")]
IntoInner(#[from] std::io::IntoInnerError<std::io::BufWriter<Vec<u8>>>),
#[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")] #[error("got {0}, which is not a boolean")]
BoolConv(u8), Bool(u8),
#[error("{0}")] #[error("{0}")]
Other(String), Other(String),
} }

View file

@ -16,7 +16,7 @@ impl<R: std::io::Read> ReadExt for R {
match self.read_u8()? { match self.read_u8()? {
1 => Ok(true), 1 => Ok(true),
0 => Ok(false), 0 => Ok(false),
err => Err(super::Error::BoolConv(err)), err => Err(super::Error::Bool(err)),
} }
} }

View file

@ -54,10 +54,10 @@ impl Footer {
}, },
}; };
if super::MAGIC != footer.magic { if super::MAGIC != footer.magic {
return Err(super::Error::WrongMagic(footer.magic)); return Err(super::Error::Magic(footer.magic));
} }
if version != footer.version { if version != footer.version {
return Err(super::Error::WrongVersion(version, footer.version)); return Err(super::Error::Version(version, footer.version));
} }
Ok(footer) Ok(footer)
} }

View file

@ -15,8 +15,8 @@ pub struct Pak {
impl Pak { impl Pak {
pub fn new<R: io::Read + io::Seek>( pub fn new<R: io::Read + io::Seek>(
version: super::Version,
mut reader: R, mut reader: R,
version: super::Version,
) -> Result<Self, super::Error> { ) -> Result<Self, super::Error> {
reader.seek(io::SeekFrom::End(-version.size()))?; reader.seek(io::SeekFrom::End(-version.size()))?;
let footer = super::Footer::new(&mut reader, version)?; let footer = super::Footer::new(&mut reader, version)?;