refactor DecoderOpt to DecoderCtx because it is not optional anymore, but it is a context (which makes more sense)

This commit is contained in:
mykola2312 2023-09-03 09:04:39 +03:00
parent 122ee7bfc0
commit 226b8dcfb9
4 changed files with 16 additions and 16 deletions

View file

@ -8,9 +8,9 @@ pub trait Decoder: Sized {
fn get_enc_size(&self) -> usize;
}
pub trait DecoderOpt<Opt>: Sized {
fn decode(raw: &Raw, offset: usize, size: usize, opt: Opt) -> Result<Self>;
fn encode(&self, opt: Opt) -> Result<Raw>;
pub trait DecoderCtx<Ctx>: Sized {
fn decode(raw: &Raw, offset: usize, size: usize, ctx: Ctx) -> Result<Self>;
fn encode(&self, ctx: Ctx) -> Result<Raw>;
fn get_enc_size(&self) -> usize;
}

View file

@ -1,4 +1,4 @@
use super::decoder::DecoderOpt;
use super::decoder::DecoderCtx;
use super::entitylist::{EntityEncoding, EntityList};
use super::esh::ESH;
use super::raw::Raw;
@ -14,13 +14,13 @@ pub struct Entity {
enc_size: usize,
}
impl DecoderOpt<&mut EntityList> for Entity {
fn decode(raw: &Raw, offset: usize, _: usize, opt: &mut EntityList) -> Result<Self> {
impl DecoderCtx<&mut EntityList> for Entity {
fn decode(raw: &Raw, offset: usize, _: usize, ctx: &mut EntityList) -> Result<Self> {
let mut rd = ReadStream::new(raw, offset);
Ok(match opt.get_entity_encoding() {
Ok(match ctx.get_entity_encoding() {
EntityEncoding::File => {
let flags = NO_FLAGS;
let type_idx = opt.add_or_get_type(rd.read(0)?);
let type_idx = ctx.add_or_get_type(rd.read(0)?);
let esh: ESH = rd.read(0)?;
let enc_size = rd.offset() - offset;
Entity { flags, type_idx, esh, enc_size }
@ -35,12 +35,12 @@ impl DecoderOpt<&mut EntityList> for Entity {
})
}
fn encode(&self, opt: &mut EntityList) -> Result<Raw> {
fn encode(&self, ctx: &mut EntityList) -> Result<Raw> {
let mut wd = WriteStream::new(self.get_enc_size());
match opt.get_entity_encoding() {
match ctx.get_entity_encoding() {
EntityEncoding::File => {
wd.write(opt.get_entity_tag())?;
wd.write(opt.get_type_name(self.type_idx))?;
wd.write(ctx.get_entity_tag())?;
wd.write(ctx.get_type_name(self.type_idx))?;
wd.write(&self.esh)?;
},
EntityEncoding::World => {

View file

@ -1,4 +1,4 @@
use super::decoder::DecoderOpt;
use super::decoder::DecoderCtx;
use super::entity::Entity;
use super::esh::ESH;
use super::fstring::FString;

View file

@ -1,4 +1,4 @@
use super::decoder::{Decoder, DecoderOpt};
use super::decoder::{Decoder, DecoderCtx};
use super::raw::Raw;
use anyhow::anyhow;
use anyhow::Result;
@ -46,8 +46,8 @@ 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<T: DecoderOpt<Opt>, Opt>(&mut self, size: usize, opt: Opt) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, opt)?;
pub fn read_opt<T: DecoderCtx<Ctx>, Ctx>(&mut self, size: usize, ctx: Ctx) -> Result<T> {
let val = T::decode(&self.raw, self.offset(), size, ctx)?;
self.skip(val.get_enc_size());
Ok(val)
}