💥 Improve the docs and error handling

This commit is contained in:
eigenein committed 2023-05-08 23:39:52 +02:00
1 parent 838a49ee68
commit c986abca5d
10 files changed
+58 -19

No files matched your search

+14 -1
View File
@@ -6,6 +6,7 @@ use serde_with::serde_as;
use crate::models::data::payload::Payload;
use crate::result::Result;
use crate::Error;
/// «Packet» is a single traffic message in a replay,
/// packets tell us what is going on during the battle.
@@ -28,14 +29,26 @@ pub struct Packet {
}
impl Packet {
/// Parse next packet from the reader.
///
/// # Returns
///
/// Parsed packet, or [`None`], when no packets are left.
pub fn from_reader(reader: &mut impl Read) -> Result<Option<Self>> {
let Ok(length) = reader.read_u32::<LittleEndian>() else { return Ok(None) };
let type_ = reader.read_u32::<LittleEndian>()?;
let clock_secs = reader.read_f32::<LittleEndian>()?;
let raw_payload = Self::read_raw_payload(reader, length as usize)?;
let payload = Payload::new(type_, &raw_payload).map_err(|source| {
Error::PacketPayloadParsingError {
source: Box::new(source),
type_,
clock_secs,
}
})?;
let this = Self {
clock_secs,
payload: Payload::new(type_, &raw_payload)?,
payload,
#[cfg(feature = "raw-payload")]
raw_payload,