rgbds_obj/
util.rs

1use std::io::{self, Read};
2
3/// Utility function to read a "LONG" (according to rgbds(5)) item
4pub fn read_u32le(mut input: impl Read) -> Result<u32, io::Error> {
5    let mut bytes = [0; 4];
6    input.read_exact(&mut bytes)?;
7    Ok(u32::from_le_bytes(bytes))
8}
9
10/// Utility function to read a "BYTE" (according to rgbds(5)) item
11pub fn read_u8(mut input: impl Read) -> Result<u8, io::Error> {
12    let mut byte = [0; 1];
13    input.read_exact(&mut byte)?;
14    Ok(byte[0])
15}
16
17/// Utility function to read a NUL-terminated string
18pub fn read_str(mut input: impl Read) -> Result<Vec<u8>, io::Error> {
19    let mut byte = [0];
20    let mut bytes = Vec::new();
21
22    loop {
23        input.read_exact(&mut byte)?;
24        if byte[0] == 0 {
25            return Ok(bytes);
26        }
27        bytes.push(byte[0]);
28    }
29}
30
31pub fn opt_u32(raw: u32) -> Option<u32> {
32    if raw == u32::MAX {
33        None
34    } else {
35        Some(raw)
36    }
37}