use crate::record_types::{Chunk, IndexData};
use crate::{record::Record, Cursor, Error, Result};
#[derive(Debug, Clone)]
pub enum ChunkRecord<'a> {
Chunk(Chunk<'a>),
IndexData(IndexData<'a>),
}
pub struct ChunkRecordsIterator<'a> {
pub(crate) cursor: Cursor<'a>,
pub(crate) offset: u64,
}
impl<'a> ChunkRecordsIterator<'a> {
pub fn seek(&mut self, pos: u64) -> Result<()> {
if pos < self.offset {
return Err(Error::OutOfBounds);
}
Ok(self.cursor.seek(pos - self.offset)?)
}
}
impl<'a> Iterator for ChunkRecordsIterator<'a> {
type Item = Result<ChunkRecord<'a>>;
fn next(&mut self) -> Option<Self::Item> {
if self.cursor.left() == 0 {
return None;
}
let res = match Record::next_record(&mut self.cursor) {
Ok(Record::Chunk(v)) => Ok(ChunkRecord::Chunk(v)),
Ok(Record::IndexData(v)) => Ok(ChunkRecord::IndexData(v)),
Ok(v) => Err(Error::UnexpectedChunkSectionRecord(v.get_type())),
Err(e) => Err(e),
};
Some(res)
}
}