evtc_parse/
util.rs

1use crate::ParseError;
2use byteorder::LittleEndian;
3use std::io;
4
5/// Endianness.
6///
7/// EVTC logs will be written on Windows and Windows uses little endian.
8pub type Endian = LittleEndian;
9
10/// Truncates the string to remove trailing null character.
11pub fn truncate_null(mut string: String) -> String {
12    if let Some(end) = string.find('\0') {
13        string.truncate(end);
14    }
15    string
16}
17
18/// Reads a fixed amount of bytes from the input into a buffer.
19pub fn read_buffer<const SIZE: usize>(input: &mut impl io::Read) -> io::Result<[u8; SIZE]> {
20    let mut buffer = [0; SIZE];
21    input.read_exact(&mut buffer)?;
22    Ok(buffer)
23}
24
25/// Reads a UTF-8 string from a char buffer.
26pub fn read_string_buffer<const SIZE: usize>(
27    input: &mut impl io::Read,
28) -> Result<String, ParseError> {
29    let buffer = read_buffer::<SIZE>(input)?;
30    Ok(String::from_utf8(buffer.to_vec())?)
31}
32
33/// Reads a UTF-8 string from a char buffer.
34pub fn write_string_buffer<const SIZE: usize>(
35    output: &mut impl io::Write,
36    string: impl AsRef<str>,
37) -> Result<(), io::Error> {
38    let bytes = string.as_ref().as_bytes();
39    let mut buffer = [0; SIZE];
40    buffer[..bytes.len()].copy_from_slice(bytes);
41    output.write_all(&buffer)?;
42    Ok(())
43}