move Opt to DecodeOpt trait because not so many objects need complex decoding

This commit is contained in:
mykola2312 2023-09-03 01:07:34 +03:00
parent 1620098672
commit 93f75c583f
8 changed files with 17 additions and 19 deletions

View file

@ -3,6 +3,12 @@ use anyhow::Result;
use std::str;
pub trait Decoder: Sized {
fn decode(raw: &Raw, offset: usize, size: usize) -> Result<Self>;
fn encode(&self) -> Result<Raw>;
fn get_enc_size(&self) -> usize;
}
pub trait DecoderOpt: Sized {
type Opt<'o>;
fn decode(raw: &Raw, offset: usize, size: usize, opt: Option<Self::Opt<'_>>) -> Result<Self>;
fn encode(&self) -> Result<Raw>;
@ -10,8 +16,7 @@ pub trait Decoder: Sized {
}
impl Decoder for String {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, size: usize) -> Result<Self> {
let str = &raw.mem[offset..];
match str.iter().position(|&c| c == 0) {
Some(pos) => Ok(str::from_utf8(&str[..pos])?.to_string()),

View file

@ -76,8 +76,7 @@ impl ESHValue {
}
impl Decoder for ESHValue {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset);
let data_type = rd.read_u32()?;
let data_size = rd.read_u32()?;
@ -249,8 +248,7 @@ pub struct ESH {
}
impl Decoder for ESH {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset);
let tag: Tag = rd.read(0)?;

View file

@ -24,8 +24,7 @@ pub struct FString {
}
impl Decoder for FString {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> {
let mut rdr = Cursor::new(&raw.mem[offset..]);
let flen = rdr.read_u32::<LittleEndian>()? as usize;
let len = flen & !(1 << 31);

View file

@ -15,8 +15,7 @@ pub struct SGD {
}
impl Decoder for SGD {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset);
let tag: Tag = rd.read(0)?;
let unk1 = rd.read_bytes(0x48)?;

View file

@ -11,8 +11,7 @@ pub struct SSG {
}
impl Decoder for SSG {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, _: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, _: usize) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset);
let tag: Tag = rd.read(0)?;
let unk1 = rd.read_bytes(0x14)?;

View file

@ -1,4 +1,4 @@
use super::decoder::Decoder;
use super::decoder::{Decoder, DecoderOpt};
use super::raw::Raw;
use anyhow::anyhow;
use anyhow::Result;
@ -46,14 +46,14 @@ impl<'a> ReadStream<'a> {
// read_opt - decode with optional paramters. required for complex structure
// with different origins (save / entfile) like entities
pub fn read_opt<'o, T: Decoder>(&mut self, size: usize, opt: T::Opt<'o>) -> Result<T> {
pub fn read_opt<'o, T: DecoderOpt>(&mut self, size: usize, opt: T::Opt<'o>) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, Some(opt))?;
self.skip(val.get_enc_size());
Ok(val)
}
pub fn read<T: Decoder>(&mut self, size: usize) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, None)?;
let val = T::decode(&self.raw, self.offset(), size)?;
self.skip(val.get_enc_size());
Ok(val)
}

View file

@ -10,8 +10,7 @@ pub struct Tag {
}
impl Decoder for Tag {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, size: usize) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset);
let name: String = rd.read(0)?;
let version: String = rd.read(0)?;

View file

@ -67,8 +67,7 @@ impl World {
}
impl Decoder for World {
type Opt<'o> = ();
fn decode(raw: &Raw, offset: usize, size: usize, _: Option<()>) -> Result<Self> {
fn decode(raw: &Raw, offset: usize, size: usize) -> Result<Self> {
let mut enc = ReadStream::new(raw, offset);
let tag: Tag = enc.read(Self::WORLD_TAG_LEN)?;