Add writing support for FloatProperty

This commit is contained in:
Joshua Goins 2025-02-23 16:39:59 -05:00
parent ee2de9c9ae
commit 80b288051c

View file

@ -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<u8> = Vec::new();
{
let mut cursor = Cursor::new(&mut buffer);
property.write_le(&mut cursor).unwrap();
}
assert_eq!(expected_data, &buffer[..]);
}
}