Opt: extract BattleResultsDat::from_reader

This commit is contained in:
Pavel Perestoronin committed 2022-12-03 17:59:04 +01:00
1 parent 679b0ddadf
commit 6fb70da06f
2 files changed
+15 -4

No files matched your search

+11
View File
@@ -1,6 +1,17 @@
use std::io::Read;
use serde::Deserialize;
use serde_bytes::ByteBuf;
use crate::{Error, Result};
/// Represents un-pickled `battle_results.dat`.
#[derive(Debug, Deserialize)]
pub struct BattleResultsDat(pub(crate) u64, pub(crate) ByteBuf);
impl BattleResultsDat {
/// Try and parse the pickled structure from the reader which contains `battle_results.dat`.
pub fn from_reader(reader: impl Read) -> Result<Self> {
serde_pickle::from_reader(reader, Default::default()).map_err(Error::UnpickleFailed)
}
}
+4 -4
View File
@@ -7,24 +7,24 @@ use crate::error::Error;
use crate::models::{BattleResults, BattleResultsDat};
use crate::result::Result;
/// Opened (but not yet parsed) replay.
pub struct Replay<R>(pub(crate) ZipArchive<R>);
impl<R: Read + Seek> Replay<R> {
/// Opens a replay from the reader.
/// Opens a replay from the reader which contains a `*.wotbreplay`.
pub fn open(reader: R) -> Result<Self> {
ZipArchive::new(reader)
.map_err(Error::OpenArchiveFailed)
.map(Self)
}
/// Reads and parses the battle results from the replay.
pub fn read_battle_results(&mut self) -> Result<BattleResults> {
let pickled_battle_results = self
.0
.by_name("battle_results.dat")
.map_err(Error::OpenBattleResultsFailed)?;
let battle_results_dat: BattleResultsDat =
serde_pickle::from_reader(pickled_battle_results, Default::default())
.map_err(Error::UnpickleFailed)?;
let battle_results_dat = BattleResultsDat::from_reader(pickled_battle_results)?;
BattleResults::decode(battle_results_dat.1.as_ref()).map_err(Error::DecodeFailed)
}
}