Skip to content

Commit

Permalink
Merge pull request #71 from Holzhaus/pdb-serialization-pt2
Browse files Browse the repository at this point in the history
PDB Serialization (Pt. 2)
  • Loading branch information
Holzhaus authored Aug 13, 2023
2 parents 04dbcb6 + 0e522e8 commit 832910d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ thiserror = "1.0"
[build-dependencies]
glob = "0.3"

[dev-dependencies]
pretty-hex = "0.3"
pretty_assertions = "1"

[features]
default = ["cli"]
cli = ["dep:clap"]
Expand Down
26 changes: 13 additions & 13 deletions src/pdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,61 +426,61 @@ impl RowGroup {
/// Identifies a track.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct TrackId(pub u32);

/// Identifies an artwork item.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct ArtworkId(pub u32);

/// Identifies an album.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct AlbumId(pub u32);

/// Identifies an artist.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct ArtistId(pub u32);

/// Identifies a genre.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct GenreId(pub u32);

/// Identifies a key.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct KeyId(pub u32);

/// Identifies a label.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct LabelId(pub u32);

/// Identifies a playlist tree node.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct PlaylistTreeNodeId(pub u32);

/// Identifies a history playlist.
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[br(little)]
#[brw(little)]
pub struct HistoryPlaylistId(pub u32);

/// Contains the album name, along with an ID of the corresponding artist.
#[binread]
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[br(little)]
#[brw(little)]
pub struct Album {
/// Position of start of this row (needed of offset calculations).
///
Expand Down Expand Up @@ -932,9 +932,9 @@ impl BinWrite for Track {
}

/// A table row contains the actual data.
#[binread]
#[binrw]
#[derive(Debug, PartialEq, Eq, Clone)]
#[br(little)]
#[brw(little)]
#[br(import(page_type: PageType))]
// The large enum size is unfortunate, but since users of this library will probably use iterators
// to consume the results on demand, we can live with this. The alternative of using a `Box` would
Expand Down
53 changes: 43 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,61 @@ pub(crate) mod testing {
use binrw::{
meta::{ReadEndian, WriteEndian},
prelude::*,
Endian, ReadOptions, WriteOptions,
};
pub fn test_roundtrip<T>(bin: &[u8], obj: T)
where
<T as binrw::BinRead>::Args: Default,
<T as binrw::BinWrite>::Args: Default,
use pretty_assertions::assert_eq;
use pretty_hex::pretty_hex;

macro_rules! assert_eq_hex {
($cond:expr, $expected:expr) => {
assert_eq!(pretty_hex($cond), pretty_hex($expected));
};
}

pub fn test_roundtrip_with_args<T>(
bin: &[u8],
obj: T,
read_args: <T as binrw::BinRead>::Args,
write_args: <T as binrw::BinWrite>::Args,
) where
T: BinRead + BinWrite + PartialEq + core::fmt::Debug + ReadEndian + WriteEndian,
{
let write_opts = WriteOptions::new(Endian::NATIVE);
let read_opts = ReadOptions::new(Endian::NATIVE);
// T->binary
let mut writer = binrw::io::Cursor::new(Vec::with_capacity(bin.len()));
obj.write(&mut writer).unwrap();
assert_eq!(bin, writer.get_ref());
obj.write_options(&mut writer, &write_opts, write_args.clone())
.unwrap();
assert_eq!(bin.len(), writer.get_ref().len());
assert_eq_hex!(&bin, &writer.get_ref());
// T->binary->T
writer.set_position(0);
let parsed = T::read(&mut writer).unwrap();
let parsed = T::read_options(&mut writer, &read_opts, read_args.clone()).unwrap();
assert_eq!(obj, parsed);
// binary->T
let mut cursor = binrw::io::Cursor::new(bin);
let parsed = T::read(&mut cursor).unwrap();
let parsed = T::read_options(&mut cursor, &read_opts, read_args).unwrap();
assert_eq!(obj, parsed);
// binary->T->binary
writer.set_position(0);
parsed.write(&mut writer).unwrap();
assert_eq!(bin, writer.get_ref());
parsed
.write_options(&mut writer, &write_opts, write_args)
.unwrap();
assert_eq!(bin.len(), writer.get_ref().len());
assert_eq_hex!(&bin, &writer.get_ref());
}

pub fn test_roundtrip<T>(bin: &[u8], obj: T)
where
<T as binrw::BinRead>::Args: Default,
<T as binrw::BinWrite>::Args: Default,
T: BinRead + BinWrite + PartialEq + core::fmt::Debug + ReadEndian + WriteEndian,
{
test_roundtrip_with_args(
bin,
obj,
<T as binrw::BinRead>::Args::default(),
<T as binrw::BinWrite>::Args::default(),
);
}
}

0 comments on commit 832910d

Please sign in to comment.