From 226b8dcfb9114d40c67dc3fe384f2e6a9da0bce7 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 3 Sep 2023 09:04:39 +0300 Subject: [PATCH] refactor DecoderOpt to DecoderCtx because it is not optional anymore, but it is a context (which makes more sense) --- src/fot/decoder.rs | 6 +++--- src/fot/entity.rs | 18 +++++++++--------- src/fot/entitylist.rs | 2 +- src/fot/stream.rs | 6 +++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/fot/decoder.rs b/src/fot/decoder.rs index b4ebac2..d996928 100644 --- a/src/fot/decoder.rs +++ b/src/fot/decoder.rs @@ -8,9 +8,9 @@ pub trait Decoder: Sized { fn get_enc_size(&self) -> usize; } -pub trait DecoderOpt: Sized { - fn decode(raw: &Raw, offset: usize, size: usize, opt: Opt) -> Result; - fn encode(&self, opt: Opt) -> Result; +pub trait DecoderCtx: Sized { + fn decode(raw: &Raw, offset: usize, size: usize, ctx: Ctx) -> Result; + fn encode(&self, ctx: Ctx) -> Result; fn get_enc_size(&self) -> usize; } diff --git a/src/fot/entity.rs b/src/fot/entity.rs index 3f007b5..67f4e43 100644 --- a/src/fot/entity.rs +++ b/src/fot/entity.rs @@ -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 { +impl DecoderCtx<&mut EntityList> for Entity { + fn decode(raw: &Raw, offset: usize, _: usize, ctx: &mut EntityList) -> Result { 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 { + fn encode(&self, ctx: &mut EntityList) -> Result { 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 => { diff --git a/src/fot/entitylist.rs b/src/fot/entitylist.rs index 295e076..494e1c5 100644 --- a/src/fot/entitylist.rs +++ b/src/fot/entitylist.rs @@ -1,4 +1,4 @@ -use super::decoder::DecoderOpt; +use super::decoder::DecoderCtx; use super::entity::Entity; use super::esh::ESH; use super::fstring::FString; diff --git a/src/fot/stream.rs b/src/fot/stream.rs index 50dac4b..bfb1d5d 100644 --- a/src/fot/stream.rs +++ b/src/fot/stream.rs @@ -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, Opt>(&mut self, size: usize, opt: Opt) -> Result { - let val = T::decode(&self.raw, self.offset(), size, opt)?; + pub fn read_opt, Ctx>(&mut self, size: usize, ctx: Ctx) -> Result { + let val = T::decode(&self.raw, self.offset(), size, ctx)?; self.skip(val.get_enc_size()); Ok(val) }