1pub struct LpChunkIter<'a> {
2 pub data: &'a [u8],
3 pub pos: usize,
4 pub index: usize,
5}
6
7impl<'a> LpChunkIter<'a> {
8 pub fn new(data: &'a [u8]) -> Self {
9 Self {
10 data,
11 pos: 0,
12 index: 0,
13 }
14 }
15}
16
17impl<'a> Iterator for LpChunkIter<'a> {
18 type Item = Result<(usize, &'a [u8]), &'static str>;
19
20 fn next(&mut self) -> Option<Self::Item> {
21 if self.pos + 4 > self.data.len() {
22 if self.pos == self.data.len() {
23 return None;
24 }
25 return Some(Err("Incomplete length prefix"));
26 }
27
28 let chunk_len =
29 u32::from_le_bytes(self.data[self.pos..self.pos + 4].try_into().unwrap()) as usize;
30 self.pos += 4;
31
32 if self.pos + chunk_len > self.data.len() {
33 return Some(Err("Incomplete chunk data"));
34 }
35
36 let chunk = &self.data[self.pos..self.pos + chunk_len];
37 self.pos += chunk_len;
38 let current_index = self.index;
39 self.index += 1;
40 Some(Ok((current_index, chunk)))
41 }
42}