Skip to content

Commit

Permalink
Implement TryFromVersionPlatform<&TrivialClass<LinkHeaderType, BodyTy…
Browse files Browse the repository at this point in the history
…pe>> for Object
  • Loading branch information
widberg committed Sep 30, 2023
1 parent d90e6d0 commit 2b7a1b3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
91 changes: 55 additions & 36 deletions bff/src/class/trivial_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,88 @@ use binrw::{BinRead, BinWrite};
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::name::Name;
use crate::object::Object;
use crate::platforms::Platform;
use crate::traits::TryFromVersionPlatform;
use crate::versions::Version;

trait TyEq {}

impl<T: Sized> TyEq for (T, T) {}

#[derive(Debug, Serialize, Deserialize)]
pub struct TrivialClass<LinkHeaderType, BodyType>
where
for<'a> LinkHeaderType: BinRead + BinWrite + Serialize + 'a,
for<'a> <LinkHeaderType as BinRead>::Args<'a>: Default,
for<'a> <LinkHeaderType as BinWrite>::Args<'a>: Default,

for<'a> BodyType: BinRead<Args<'a> = (&'a LinkHeaderType,)> + BinWrite + Serialize + 'a,
{
link_header: LinkHeaderType,
body: BodyType,
pub struct TrivialClass<LinkHeaderType, BodyType> {
pub name: Name,
pub class_name: Name,
pub link_header: LinkHeaderType,
pub body: BodyType,
}

impl<LinkHeaderType, BodyType> TrivialClass<LinkHeaderType, BodyType>
impl<LinkHeaderType, BodyType> TryFromVersionPlatform<&Object>
for TrivialClass<LinkHeaderType, BodyType>
where
for<'a> LinkHeaderType: BinRead + BinWrite + Serialize + 'a,
for<'a> LinkHeaderType: BinRead + 'a,
for<'a> <LinkHeaderType as BinRead>::Args<'a>: Default,
for<'a> <LinkHeaderType as BinWrite>::Args<'a>: Default,

for<'a> BodyType: BinRead<Args<'a> = (&'a LinkHeaderType,)> + BinWrite + Serialize + 'a,
for<'a> BodyType: BinRead<Args<'a> = (&'a LinkHeaderType,)> + 'a,
{
pub fn link_header(&self) -> &LinkHeaderType {
&self.link_header
}
type Error = Error;

pub fn body(&self) -> &BodyType {
&self.body
fn try_from_version_platform(
object: &Object,
_version: Version,
platform: Platform,
) -> Result<Self, Self::Error> {
let mut link_header_cursor = Cursor::new(object.link_header());
let mut body_cursor = Cursor::new(object.body());
let link_header = LinkHeaderType::read_options(
&mut link_header_cursor,
platform.into(),
<LinkHeaderType as binrw::BinRead>::Args::default(),
)?;
let body = BodyType::read_options(&mut body_cursor, platform.into(), (&link_header,))?;
Ok(Self {
name: object.name,
class_name: object.class_name,
link_header,
body,
})
}
}

impl<LinkHeaderType, BodyType> TryFromVersionPlatform<&Object>
for TrivialClass<LinkHeaderType, BodyType>
impl<LinkHeaderType, BodyType> TryFromVersionPlatform<&TrivialClass<LinkHeaderType, BodyType>>
for Object
where
for<'a> LinkHeaderType: BinRead + BinWrite + Serialize + 'a,
for<'a> <LinkHeaderType as BinRead>::Args<'a>: Default,
for<'a> LinkHeaderType: BinWrite + 'a,
for<'a> <LinkHeaderType as BinWrite>::Args<'a>: Default,

for<'a> BodyType: BinRead<Args<'a> = (&'a LinkHeaderType,)> + BinWrite + Serialize + 'a,
for<'a> BodyType: BinWrite + 'a,
for<'a> <BodyType as BinWrite>::Args<'a>: Default,
{
type Error = Error;

fn try_from_version_platform(
object: &Object,
class: &TrivialClass<LinkHeaderType, BodyType>,
_version: Version,
platform: Platform,
) -> Result<Self, Self::Error> {
let mut header_cursor = Cursor::new(object.link_header());
let mut body_cursor = Cursor::new(object.body());
let link_header = LinkHeaderType::read_options(
&mut header_cursor,
let mut link_header_cursor = Cursor::new(Vec::new());
let mut body_cursor = Cursor::new(Vec::new());
LinkHeaderType::write_options(
&class.link_header,
&mut link_header_cursor,
platform.into(),
<LinkHeaderType as binrw::BinRead>::Args::default(),
<LinkHeaderType as BinWrite>::Args::default(),
)?;
let body = BodyType::read_options(&mut body_cursor, platform.into(), (&link_header,))?;
Ok(Self { link_header, body })
BodyType::write_options(
&class.body,
&mut body_cursor,
platform.into(),
<BodyType as BinWrite>::Args::default(),
)?;
Ok(Self {
compress: false,
name: class.name,
class_name: class.class_name,
link_header: link_header_cursor.into_inner(),
body: body_cursor.into_inner(),
})
}
}
8 changes: 4 additions & 4 deletions bff/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ pub struct Object {
compressed_size: u32,
#[br(calc = compressed_size != 0)]
#[bw(ignore)]
compress: bool,
class_name: Name,
name: Name,
pub compress: bool,
pub class_name: Name,
pub name: Name,
#[br(count = link_header_size)]
#[serde(skip_serializing)]
link_header: Vec<u8>,
pub link_header: Vec<u8>,
#[br(parse_with = body_parser, args(decompressed_size, compressed_size))]
#[serde(skip_serializing)]
pub body: Vec<u8>,
Expand Down

0 comments on commit 2b7a1b3

Please sign in to comment.