Add writing support for IntProperty

This commit is contained in:
Joshua Goins 2025-02-23 16:38:57 -05:00
parent ff87348e42
commit ee2de9c9ae

View file

@ -3,29 +3,46 @@ use binrw::binrw;
#[binrw] #[binrw]
#[derive(Debug)] #[derive(Debug)]
pub struct IntProperty { pub struct IntProperty {
/// In bytes #[bw(calc = 4)]
pub integer_size: u32, pub size_in_bytes: u32,
#[br(pad_before = 5)] #[brw(pad_before = 5)]
pub value: u32, pub value: u32,
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use binrw::BinRead; use binrw::{BinRead, BinWrite};
use std::io::Cursor; use std::io::Cursor;
#[test] #[test]
fn zero() { fn read_zero() {
let data = [ let data = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]; ];
let mut cursor = Cursor::new(data); let mut cursor = Cursor::new(data);
let decoded = IntProperty::read_le(&mut cursor).unwrap(); let decoded = IntProperty::read_le(&mut cursor).unwrap();
assert_eq!(decoded.integer_size, 4);
assert_eq!(decoded.value, 0); assert_eq!(decoded.value, 0);
} }
#[test]
fn write_zero() {
let expected_data: [u8; 13] = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let property = IntProperty {
value: 0
};
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[..]);
}
#[test] #[test]
fn integer() { fn integer() {
let data = [ let data = [
@ -33,7 +50,24 @@ mod tests {
]; ];
let mut cursor = Cursor::new(data); let mut cursor = Cursor::new(data);
let decoded = IntProperty::read_le(&mut cursor).unwrap(); let decoded = IntProperty::read_le(&mut cursor).unwrap();
assert_eq!(decoded.integer_size, 4);
assert_eq!(decoded.value, 4); assert_eq!(decoded.value, 4);
} }
#[test]
fn write_integer() {
let expected_data: [u8; 13] = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
];
let property = IntProperty {
value: 4
};
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[..]);
}
} }