Add writing support for FloatProperty
This commit is contained in:
parent
ee2de9c9ae
commit
80b288051c
1 changed files with 23 additions and 6 deletions
|
@ -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[..]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue