From 80b288051c9d0080ca035a0c55f1f89dc4ebc841 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 23 Feb 2025 16:39:59 -0500 Subject: [PATCH] Add writing support for FloatProperty --- src/float_property.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/float_property.rs b/src/float_property.rs index 7a4f6fc..fcc0977 100644 --- a/src/float_property.rs +++ b/src/float_property.rs @@ -3,26 +3,43 @@ use binrw::binrw; #[binrw] #[derive(Debug)] pub struct FloatProperty { - /// Typically 4 for f32 - pub byte_size: u32, - #[br(pad_before = 5)] + #[bw(calc = 4)] + pub size_in_bytes: u32, + #[brw(pad_before = 5)] pub value: f32, } #[cfg(test)] mod tests { use super::*; - use binrw::BinRead; + use binrw::{BinRead, BinWrite}; use std::io::Cursor; #[test] - fn value() { + fn read_value() { let data = [ 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x76, 0x9c, 0x45, ]; let mut cursor = Cursor::new(data); let decoded = FloatProperty::read_le(&mut cursor).unwrap(); - assert_eq!(decoded.byte_size, 4); assert_eq!(decoded.value, 5006.8184); } + + #[test] + fn write_value() { + let expected_data: [u8; 13] = [ + 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x76, 0x9c, 0x45, + ]; + let property = FloatProperty { + value: 5006.8184 + }; + + let mut buffer: Vec = Vec::new(); + { + let mut cursor = Cursor::new(&mut buffer); + property.write_le(&mut cursor).unwrap(); + } + + assert_eq!(expected_data, &buffer[..]); + } }