Skip to content

Commit

Permalink
Merge pull request #101 from erictapen/binrw-0.13
Browse files Browse the repository at this point in the history
update binrw to 0.13
  • Loading branch information
Holzhaus authored Nov 30, 2023
2 parents 57e3875 + 78fc631 commit 05a1c76
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 83 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2021"
exclude = [".*"]

[dependencies]
binrw = "0.10"
binrw = "0.13"
modular-bitfield = "0.11"
crc16 = "0.4"
clap = { version = "4.0.10", features = ["derive"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() {
out_dir.join("tests_pdb.rs"),
"data/complete_export/*/PIONEER/rekordbox/export.pdb",
r#"// THIS FILE IS AUTOGENERATED - DO NOT EDIT!
use binrw::{{BinRead, ReadOptions}};
use binrw::{{BinRead}};
use rekordcrate::pdb::Header;
"#,
"./tests/tests_pdb.rs.in"
Expand Down
18 changes: 9 additions & 9 deletions src/anlz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{util::ColorIndex, xor::XorStream};
use binrw::{
binrw,
io::{Read, Seek, Write},
BinRead, BinResult, BinWrite, NullWideString, ReadOptions, WriteOptions,
BinRead, BinResult, BinWrite, Endian, NullWideString,
};
use modular_bitfield::prelude::*;

Expand Down Expand Up @@ -867,15 +867,15 @@ impl SongStructureData {
/// value.
fn read_encrypted<R: Read + Seek>(
reader: &mut R,
options: &ReadOptions,
endian: Endian,
(is_encrypted, len_entries): (bool, u16),
) -> BinResult<Self> {
if is_encrypted {
let key: Vec<u8> = Self::get_key(len_entries).collect();
let mut xor_reader = XorStream::with_key(reader, key);
Self::read_options(&mut xor_reader, options, (len_entries,))
Self::read_options(&mut xor_reader, endian, (len_entries,))
} else {
Self::read_options(reader, options, (len_entries,))
Self::read_options(reader, endian, (len_entries,))
}
}

Expand All @@ -884,15 +884,15 @@ impl SongStructureData {
fn write_encrypted<W: Write + Seek>(
&self,
writer: &mut W,
options: &WriteOptions,
endian: Endian,
(is_encrypted, len_entries): (bool, u16),
) -> BinResult<()> {
if is_encrypted {
let key: Vec<u8> = Self::get_key(len_entries).collect();
let mut xor_writer = XorStream::with_key(writer, key);
self.write_options(&mut xor_writer, options, ())
self.write_options(&mut xor_writer, endian, ())
} else {
self.write_options(writer, options, ())
self.write_options(writer, endian, ())
}
}
}
Expand Down Expand Up @@ -943,15 +943,15 @@ pub struct ANLZ {
impl ANLZ {
fn parse_sections<R: Read + Seek>(
reader: &mut R,
ro: &ReadOptions,
endian: Endian,
args: (u32,),
) -> BinResult<Vec<Section>> {
let (content_size,) = args;
let final_position = reader.stream_position()? + u64::from(content_size);

let mut sections: Vec<Section> = vec![];
while reader.stream_position()? < final_position {
let section = Section::read_options(reader, ro, ())?;
let section = Section::read_options(reader, endian, ())?;
sections.push(section);
}

Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
// SPDX-License-Identifier: MPL-2.0

use binrw::{BinRead, ReadOptions};
use binrw::BinRead;
use clap::{Parser, Subcommand};
use rekordcrate::anlz::ANLZ;
use rekordcrate::pdb::{Header, PageType, Row};
Expand Down Expand Up @@ -85,7 +85,7 @@ fn list_playlists(path: &PathBuf) -> rekordcrate::Result<()> {
header
.read_pages(
&mut reader,
&ReadOptions::new(binrw::Endian::NATIVE),
binrw::Endian::NATIVE,
(&table.first_page, &table.last_page),
)
.unwrap()
Expand All @@ -101,7 +101,6 @@ fn list_playlists(path: &PathBuf) -> rekordcrate::Result<()> {
unreachable!("encountered non-playlist tree row in playlist table");
}
})
.cloned()
.collect::<Vec<PlaylistTreeNode>>()
.into_iter()
})
Expand Down Expand Up @@ -132,7 +131,7 @@ fn dump_pdb(path: &PathBuf) -> rekordcrate::Result<()> {
for page in header
.read_pages(
&mut reader,
&ReadOptions::new(binrw::Endian::NATIVE),
binrw::Endian::NATIVE,
(&table.first_page, &table.last_page),
)
.unwrap()
Expand Down
99 changes: 52 additions & 47 deletions src/pdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ use crate::pdb::string::DeviceSQLString;
use crate::util::ColorIndex;
use binrw::{
binread, binrw,
file_ptr::FilePtrArgs,
io::{Read, Seek, SeekFrom, Write},
BinRead, BinResult, BinWrite, Endian, FilePtr16, FilePtr8, ReadOptions, WriteOptions,
BinRead, BinResult, BinWrite, Endian, FilePtr16, FilePtr8,
};

/// Do not read anything, but the return the current stream position of `reader`.
fn current_offset<R: Read + Seek>(reader: &mut R, _: &ReadOptions, _: ()) -> BinResult<u64> {
fn current_offset<R: Read + Seek>(reader: &mut R, _: Endian, _: ()) -> BinResult<u64> {
reader.stream_position().map_err(binrw::Error::Io)
}

Expand Down Expand Up @@ -161,7 +162,7 @@ impl Header {
pub fn read_pages<R: Read + Seek>(
&self,
reader: &mut R,
ro: &ReadOptions,
endian: Endian,
args: (&PageIndex, &PageIndex),
) -> BinResult<Vec<Page>> {
let (first_page, last_page) = args;
Expand All @@ -171,7 +172,7 @@ impl Header {
loop {
let page_offset = SeekFrom::Start(page_index.offset(self.page_size));
reader.seek(page_offset).map_err(binrw::Error::Io)?;
let page = Page::read_options(reader, ro, (self.page_size,))?;
let page = Page::read_options(reader, endian, (self.page_size,))?;
let is_last_page = &page.page_index == last_page;
page_index = page.next_page.clone();
pages.push(page);
Expand Down Expand Up @@ -312,7 +313,7 @@ impl Page {
/// Parse the row groups at the end of the page.
fn parse_row_groups<R: Read + Seek>(
reader: &mut R,
ro: &ReadOptions,
endian: Endian,
args: (PageType, u64, u16, u16, PageFlags),
) -> BinResult<Vec<RowGroup>> {
let (page_type, page_heap_offset, num_rows, num_row_groups, page_flags) = args;
Expand All @@ -331,7 +332,7 @@ impl Page {
// Read last row group
let row_group = RowGroup::read_options(
reader,
ro,
endian,
(page_type, page_heap_offset, num_rows_in_last_row_group),
)?;
row_groups.push(row_group);
Expand All @@ -340,7 +341,7 @@ impl Page {
for _ in 1..num_row_groups {
let row_group = RowGroup::read_options(
reader,
ro,
endian,
(page_type, page_heap_offset, RowGroup::MAX_ROW_COUNT),
)?;
row_groups.insert(0, row_group);
Expand Down Expand Up @@ -395,7 +396,7 @@ pub struct RowGroup {
/// An offset which points to a row in the table, whose actual presence is controlled by one of the
/// bits in `row_present_flags`. This instance allows the row itself to be lazily loaded, unless it
/// is not present, in which case there is no content to be loaded.
#[br(offset = page_heap_offset, args { count: num_rows.into(), inner: (page_type,) })]
#[br(args { count: num_rows.into(), inner: FilePtrArgs { inner: (page_type,), offset: page_heap_offset }})]
rows: Vec<FilePtr16<Row>>,
row_presence_flags: u16,
/// Unknown field, probably padding.
Expand All @@ -408,14 +409,18 @@ impl RowGroup {
const MAX_ROW_COUNT: u16 = 16;

/// Return the ordered list of row offsets that are actually present.
pub fn present_rows(&self) -> impl Iterator<Item = &Row> {
pub fn present_rows(&self) -> impl Iterator<Item = Row> + '_ {
self.rows
.iter()
.rev()
.enumerate()
.filter_map(|(i, row_offset)| {
if (self.row_presence_flags & (1 << i)) != 0 {
row_offset.value.as_ref()
// TODO: the explicit clone is probably quite expensive
// but the simplest way to make the borrow checker happy for now.
// This is forced by the changes to FilePtr in binrw 0.12.
// We should investigate how to remove the clone in the future.
Some(row_offset.value.clone())
} else {
None
}
Expand Down Expand Up @@ -835,48 +840,48 @@ impl binrw::meta::WriteEndian for Track {
}

impl BinWrite for Track {
type Args = ();
type Args<'a> = ();

fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
options: &WriteOptions,
_args: Self::Args,
endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<()> {
debug_assert!(options.endian() == Endian::Little);
debug_assert!(endian == Endian::Little);

let base_position = writer.stream_position()?;
self.unknown1.write_options(writer, options, ())?;
self.index_shift.write_options(writer, options, ())?;
self.bitmask.write_options(writer, options, ())?;
self.sample_rate.write_options(writer, options, ())?;
self.composer_id.write_options(writer, options, ())?;
self.file_size.write_options(writer, options, ())?;
self.unknown2.write_options(writer, options, ())?;
self.unknown3.write_options(writer, options, ())?;
self.unknown4.write_options(writer, options, ())?;
self.artwork_id.write_options(writer, options, ())?;
self.key_id.write_options(writer, options, ())?;
self.orig_artist_id.write_options(writer, options, ())?;
self.label_id.write_options(writer, options, ())?;
self.remixer_id.write_options(writer, options, ())?;
self.bitrate.write_options(writer, options, ())?;
self.track_number.write_options(writer, options, ())?;
self.tempo.write_options(writer, options, ())?;
self.genre_id.write_options(writer, options, ())?;
self.album_id.write_options(writer, options, ())?;
self.artist_id.write_options(writer, options, ())?;
self.id.write_options(writer, options, ())?;
self.disc_number.write_options(writer, options, ())?;
self.play_count.write_options(writer, options, ())?;
self.year.write_options(writer, options, ())?;
self.sample_depth.write_options(writer, options, ())?;
self.duration.write_options(writer, options, ())?;
self.unknown5.write_options(writer, options, ())?;
self.color.write_options(writer, options, ())?;
self.rating.write_options(writer, options, ())?;
self.unknown6.write_options(writer, options, ())?;
self.unknown7.write_options(writer, options, ())?;
self.unknown1.write_options(writer, endian, ())?;
self.index_shift.write_options(writer, endian, ())?;
self.bitmask.write_options(writer, endian, ())?;
self.sample_rate.write_options(writer, endian, ())?;
self.composer_id.write_options(writer, endian, ())?;
self.file_size.write_options(writer, endian, ())?;
self.unknown2.write_options(writer, endian, ())?;
self.unknown3.write_options(writer, endian, ())?;
self.unknown4.write_options(writer, endian, ())?;
self.artwork_id.write_options(writer, endian, ())?;
self.key_id.write_options(writer, endian, ())?;
self.orig_artist_id.write_options(writer, endian, ())?;
self.label_id.write_options(writer, endian, ())?;
self.remixer_id.write_options(writer, endian, ())?;
self.bitrate.write_options(writer, endian, ())?;
self.track_number.write_options(writer, endian, ())?;
self.tempo.write_options(writer, endian, ())?;
self.genre_id.write_options(writer, endian, ())?;
self.album_id.write_options(writer, endian, ())?;
self.artist_id.write_options(writer, endian, ())?;
self.id.write_options(writer, endian, ())?;
self.disc_number.write_options(writer, endian, ())?;
self.play_count.write_options(writer, endian, ())?;
self.year.write_options(writer, endian, ())?;
self.sample_depth.write_options(writer, endian, ())?;
self.duration.write_options(writer, endian, ())?;
self.unknown5.write_options(writer, endian, ())?;
self.color.write_options(writer, endian, ())?;
self.rating.write_options(writer, endian, ())?;
self.unknown6.write_options(writer, endian, ())?;
self.unknown7.write_options(writer, endian, ())?;

let start_of_string_section = writer.stream_position()?;
debug_assert_eq!(start_of_string_section - base_position, 0x5e);
Expand Down Expand Up @@ -919,12 +924,12 @@ impl BinWrite for Track {
message: "Wraparound while calculating row offset".to_string(),
})?;
string_offsets[i] = offset;
string.write_options(writer, options, ())?;
string.write_options(writer, endian, ())?;
}

let end_of_row = writer.stream_position()?;
writer.seek(SeekFrom::Start(start_of_string_section))?;
string_offsets.write_options(writer, options, ())?;
string_offsets.write_options(writer, endian, ())?;
writer.seek(SeekFrom::Start(end_of_row))?;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! The `SettingData` structs implement the `Default` trait and allows you to create objects that
//! use the same default values as found in Rekordbox 6.6.1.

use binrw::{binrw, io::Cursor, BinWrite, Endian, NullString, WriteOptions};
use binrw::{binrw, io::Cursor, BinWrite, Endian, NullString};
use parse_display::Display;

#[binrw]
Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Setting {
/// details.
#[br(temp)]
#[bw(calc = no_checksum.then_some(0).unwrap_or_else(|| self.calculate_checksum()))]
checksum: u16,
_checksum: u16,
/// Unknown field (apparently always `0000`).
#[br(temp)]
#[br(assert(unknown == 0))]
Expand Down Expand Up @@ -138,7 +138,7 @@ where
fn calculate_checksum(&self) -> u16 {
let mut data = Vec::<u8>::with_capacity(156);
let mut writer = Cursor::new(&mut data);
self.write_options(&mut writer, &WriteOptions::new(Endian::Little), (true,))
self.write_options(&mut writer, Endian::Little, (true,))
.unwrap();
let start = match self.data {
// In `DJMMYSETTING.DAT`, the checksum is calculated over all previous bytes, including
Expand Down
Loading

0 comments on commit 05a1c76

Please sign in to comment.