Add writing support for booleans and BoolProperty
This commit is contained in:
parent
beaa0e784f
commit
ff87348e42
2 changed files with 51 additions and 7 deletions
|
@ -1,34 +1,70 @@
|
||||||
use crate::common::read_bool_from;
|
use crate::common::{read_bool_from, write_bool_as};
|
||||||
use binrw::binrw;
|
use binrw::binrw;
|
||||||
|
|
||||||
#[binrw]
|
#[binrw]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BoolProperty {
|
pub struct BoolProperty {
|
||||||
#[br(pad_before = 8, pad_after = 1)]
|
#[brw(pad_before = 8, pad_after = 1)]
|
||||||
#[br(map = read_bool_from::<u8>)]
|
#[br(map = read_bool_from::<u8>)]
|
||||||
#[bw(ignore)]
|
#[bw(map = write_bool_as::<u8>)]
|
||||||
pub value: bool,
|
pub value: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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 r#false() {
|
fn read_false() {
|
||||||
let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
let mut cursor = Cursor::new(data);
|
let mut cursor = Cursor::new(data);
|
||||||
let decoded = BoolProperty::read_le(&mut cursor).unwrap();
|
let decoded = BoolProperty::read_le(&mut cursor).unwrap();
|
||||||
assert!(!decoded.value);
|
assert!(!decoded.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn r#true() {
|
fn write_false() {
|
||||||
|
let expected_data: [u8; 10] = [
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||||
|
];
|
||||||
|
let property = BoolProperty {
|
||||||
|
value: false
|
||||||
|
};
|
||||||
|
|
||||||
|
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]
|
||||||
|
fn read_true() {
|
||||||
let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00];
|
let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00];
|
||||||
let mut cursor = Cursor::new(data);
|
let mut cursor = Cursor::new(data);
|
||||||
let decoded = BoolProperty::read_le(&mut cursor).unwrap();
|
let decoded = BoolProperty::read_le(&mut cursor).unwrap();
|
||||||
assert!(decoded.value);
|
assert!(decoded.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn write_true() {
|
||||||
|
let expected_data: [u8; 10] = [
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
|
||||||
|
];
|
||||||
|
let property = BoolProperty {
|
||||||
|
value: true
|
||||||
|
};
|
||||||
|
|
||||||
|
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[..]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,14 @@ pub(crate) fn read_bool_from<T: From<u8> + PartialEq>(x: T) -> bool {
|
||||||
x == T::from(1u8)
|
x == T::from(1u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn write_bool_as<T: From<u8>>(x: &bool) -> T {
|
||||||
|
if *x {
|
||||||
|
T::from(1u8)
|
||||||
|
} else {
|
||||||
|
T::from(0u8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[binrw::parser(reader, endian)]
|
#[binrw::parser(reader, endian)]
|
||||||
pub(crate) fn read_string_with_length() -> BinResult<String> {
|
pub(crate) fn read_string_with_length() -> BinResult<String> {
|
||||||
let length = u32::read_le(reader)? as usize;
|
let length = u32::read_le(reader)? as usize;
|
||||||
|
|
Loading…
Add table
Reference in a new issue