use crate::raw::error::SdmpError;
pub struct Entry {
pub path: String,
pub file_size: u32,
}
impl Entry {
pub fn write(&self, output: &mut Vec<u8>) {
let path_bytes = self.path.as_bytes();
let path_len = path_bytes.len() as u16;
output.extend_from_slice(&path_len.to_le_bytes());
output.extend_from_slice(path_bytes);
output.extend_from_slice(&self.file_size.to_le_bytes());
}
pub fn read(bytes: &[u8]) -> Result<(Self, usize), SdmpError> {
let mut i = 0;
if bytes.len() < 2 {
return Err(SdmpError::UnexpectedEof);
}
let path_len = u16::from_le_bytes([bytes[i], bytes[i + 1]]) as usize;
i += 2;
if bytes.len() < i + path_len {
return Err(SdmpError::UnexpectedEof);
}
let path = String::from_utf8(bytes[i..i + path_len].to_vec())
.map_err(|_| SdmpError::InvalidUtf8)?;
i += path_len;
if bytes.len() < i + 4 {
return Err(SdmpError::UnexpectedEof);
}
let file_size = u32::from_le_bytes([bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]);
i += 4;
Ok((Self { path, file_size }, i))
}
}