From 79d92c57745d2ffadfaa82af5f5c75dbc48cff8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20P=C4=99dzich?= Date: Mon, 17 Feb 2025 19:04:15 +0100 Subject: [PATCH] refactor: add binrw assertions for session-related structs --- src/packets/session.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/packets/session.rs b/src/packets/session.rs index 6a0f8ad..305f675 100644 --- a/src/packets/session.rs +++ b/src/packets/session.rs @@ -8,9 +8,17 @@ use serde::{Deserialize, Serialize}; #[derive( BinRead, PartialEq, PartialOrd, Copy, Clone, Debug, Serialize, Deserialize, )] -#[br(little, import(_packet_format: u16))] +#[br( + little, + import(_packet_format: u16), + assert( + (0.0..1.0).contains(&zone_start), + "Marshal zone has an invalid zone start value: {}", + zone_start + ) +)] pub struct MarshalZone { - /// Fraction (0..1) of way through the lap the marshal zone starts. + /// Fraction (in range `(0.0..1.0)`) of way through the lap the marshal zone starts. pub zone_start: f32, /// Flag that's currently being waved in the marshal zone. pub zone_flag: MarshalZoneFlag, @@ -20,7 +28,15 @@ pub struct MarshalZone { #[derive( BinRead, PartialEq, PartialOrd, Copy, Clone, Debug, Serialize, Deserialize, )] -#[br(little, import(_packet_format: u16))] +#[br( + little, + import(_packet_format: u16), + assert( + rain_percentage <= 100, + "Weather forecast sample has an invalid rain percentage value: {}", + rain_percentage + ) +)] pub struct WeatherForecastSample { /// Session's type. pub session_type: SessionType,