1use std::io::{Read, Write};
8
9use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
10
11use super::XP3Error;
12
13pub mod file;
14
15#[derive(Debug, Clone)]
16pub struct XP3Index {
18
19 pub identifier: u32,
20 pub data: Vec<u8>
21
22}
23
24impl XP3Index {
25
26 pub fn from_bytes(stream: &mut impl Read) -> Result<(u64, Self), XP3Error> {
29 let identifier: u32 = stream.read_u32::<LittleEndian>()?;
30 let size: u64 = stream.read_u64::<LittleEndian>()?;
31 let mut data = Vec::<u8>::with_capacity(size as usize);
32
33 stream.take(size).read_to_end(&mut data)?;
34
35 Ok((12 + size, Self { identifier, data }))
36 }
37
38 pub fn write_bytes(&mut self, stream: &mut impl Write) -> Result<u64, XP3Error> {
40 stream.write_u32::<LittleEndian>(self.identifier)?;
41 let len = self.data.len() as u64;
42 stream.write_u64::<LittleEndian>(len)?;
43 stream.write_all(&mut self.data)?;
44
45 Ok(12 + len)
46 }
47
48}