Skip to content

Commit

Permalink
feat: implement entry header parsing (#5)
Browse files Browse the repository at this point in the history
feat: add #![warn(clippy::pedantic)]

feat: add `#[derive(Debug, PartialEq)]` to types for easier testing.

---------

Co-authored-by: nenikitov <nenikitov@mail.ru>
  • Loading branch information
UserIsntAvailable and nenikitov committed Nov 12, 2023
1 parent 7f9a1f8 commit 18fefd2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unused)] // FIX(Unavailable): just testing :)
#![warn(unused_imports)]
#![warn(clippy::pedantic)]

mod packfile;
66 changes: 65 additions & 1 deletion lib/src/packfile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@
mod nom;

#[allow(clippy::wildcard_imports)]
use nom::*;

#[derive(Debug, PartialEq)]
enum EntryKind {}

#[derive(Debug, PartialEq)]
pub struct EntryData {
bytes: Vec<u8>,
kind: EntryKind,
}

#[derive(Debug, PartialEq)]
pub struct PackFile {
copyright: String,
entries: Vec<EntryData>,
}

#[derive(Debug, PartialEq)]
struct EntryHeader {
offset: u32,
size: u32,
}

impl PackFile {
const HEADER: &'static str = "PMAN";
const COPYRIGHT_LENGTH: usize = 56;

fn new(bytes: &[u8]) -> Result<()> {
pub fn new(bytes: &[u8]) -> Result<Self> {
todo!()
}

Expand All @@ -36,6 +46,24 @@ impl PackFile {

Ok((input, (copyright, total_entries)))
}

fn entries(input: &[u8], total_entries: u32) -> Result<Vec<EntryHeader>> {
fn entry(input: &[u8]) -> Result<EntryHeader> {
// TODO(nenikitov): add check for `asset_kind == 0`
let (input, asset_kind) = number::le_u32(input)?;

let (input, offset) = number::le_u32(input)?;

let (input, size) = number::le_u32(input)?;

// TODO(nenikitov): add check for `reserved == 0`
let (input, reserved) = number::le_u32(input)?;

Ok((input, EntryHeader { offset, size }))
}

multi::count(entry, total_entries as usize)(input)
}
}

#[cfg(test)]
Expand All @@ -51,4 +79,40 @@ mod tests {

Ok(())
}

#[test]
fn packfile_entries_works() -> eyre::Result<()> {
#[rustfmt::skip]
let (_, entries) = PackFile::entries(
&[
// File 1
0x00, 0x00, 0x00, 0x00,
0x20, 0x0A, 0x00, 0x00,
0x00, 0x65, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// File 2
0x00, 0x00, 0x00, 0x00,
0x20, 0x6F, 0x00, 0x00,
0x00, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
],
2
)?;

assert_eq!(
entries,
[
EntryHeader {
offset: 0x0A20,
size: 0x6500,
},
EntryHeader {
offset: 0x6F20,
size: 0x8000,
},
]
);

Ok(())
}
}
4 changes: 4 additions & 0 deletions lib/src/packfile/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ re_export!(bytes);
re_export!(character);
re_export!(number);

pub mod multi {
pub use nom::multi::*;
}

pub type Result<'a, T> = nom::IResult<&'a [u8], T>;

0 comments on commit 18fefd2

Please sign in to comment.