2025-02-19 19:34:13 -05:00
|
|
|
use binrw::binrw;
|
|
|
|
|
2025-03-02 17:00:33 -05:00
|
|
|
use super::PropertyBase;
|
|
|
|
|
2025-02-19 19:34:13 -05:00
|
|
|
#[binrw]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct IntProperty {
|
2025-02-23 16:38:57 -05:00
|
|
|
#[bw(calc = 4)]
|
|
|
|
pub size_in_bytes: u32,
|
|
|
|
#[brw(pad_before = 5)]
|
2025-02-19 19:34:13 -05:00
|
|
|
pub value: u32,
|
|
|
|
}
|
|
|
|
|
2025-03-02 17:00:33 -05:00
|
|
|
impl PropertyBase for IntProperty {
|
2025-03-02 14:07:39 -05:00
|
|
|
fn type_name() -> &'static str {
|
2025-03-02 14:13:41 -05:00
|
|
|
"IntProperty"
|
2025-03-02 14:07:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size_in_bytes(&self) -> u32 {
|
|
|
|
4 + 5 + 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-19 19:34:13 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2025-02-23 16:38:57 -05:00
|
|
|
use binrw::{BinRead, BinWrite};
|
2025-02-19 19:34:13 -05:00
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
#[test]
|
2025-02-23 16:38:57 -05:00
|
|
|
fn read_zero() {
|
2025-02-19 19:34:13 -05:00
|
|
|
let data = [
|
|
|
|
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
];
|
|
|
|
let mut cursor = Cursor::new(data);
|
|
|
|
let decoded = IntProperty::read_le(&mut cursor).unwrap();
|
|
|
|
assert_eq!(decoded.value, 0);
|
|
|
|
}
|
|
|
|
|
2025-02-23 16:38:57 -05:00
|
|
|
#[test]
|
|
|
|
fn write_zero() {
|
|
|
|
let expected_data: [u8; 13] = [
|
|
|
|
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
];
|
2025-02-24 00:30:16 -05:00
|
|
|
let property = IntProperty { value: 0 };
|
2025-02-23 16:38:57 -05:00
|
|
|
|
|
|
|
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[..]);
|
|
|
|
}
|
|
|
|
|
2025-02-19 19:34:13 -05:00
|
|
|
#[test]
|
|
|
|
fn integer() {
|
|
|
|
let data = [
|
|
|
|
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
|
|
|
|
];
|
|
|
|
let mut cursor = Cursor::new(data);
|
|
|
|
let decoded = IntProperty::read_le(&mut cursor).unwrap();
|
|
|
|
assert_eq!(decoded.value, 4);
|
|
|
|
}
|
2025-02-23 16:38:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_integer() {
|
|
|
|
let expected_data: [u8; 13] = [
|
|
|
|
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
|
|
|
|
];
|
2025-02-24 00:30:16 -05:00
|
|
|
let property = IntProperty { value: 4 };
|
2025-02-23 16:38:57 -05:00
|
|
|
|
|
|
|
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[..]);
|
|
|
|
}
|
2025-02-19 19:34:13 -05:00
|
|
|
}
|