2023-12-29 23:52:05 -05:00
|
|
|
use {
|
|
|
|
crate::{listing::Kind, Error, Listing, MAGIC},
|
|
|
|
std::{
|
|
|
|
io::{ErrorKind, Read, Seek},
|
|
|
|
iter::Iterator,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ListingStream<R: Read + Send + Seek> {
|
|
|
|
pub length: u32,
|
|
|
|
reader: R,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Read + Send + Seek> Iterator for ListingStream<R> {
|
|
|
|
type Item = Result<Listing, Error>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match Listing::read(&mut self.reader) {
|
|
|
|
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => None,
|
|
|
|
Ok(f) => match f.kind {
|
|
|
|
Kind::Eof => None,
|
|
|
|
_ => Some(Ok(f)),
|
|
|
|
},
|
|
|
|
x => Some(x),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: Read + Send + Seek> ListingStream<R> {
|
|
|
|
pub fn new(mut reader: R) -> Result<Self, Error> {
|
|
|
|
let mut buf = [0; 11];
|
|
|
|
reader.read_exact(&mut buf)?;
|
|
|
|
let length = u32::from_le_bytes(buf[7..].try_into()?);
|
|
|
|
if buf[0..7] == MAGIC {
|
|
|
|
Ok(Self { length, reader })
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidMagic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list(&mut self) -> Result<Vec<Listing>, Error> {
|
|
|
|
let mut list = vec![];
|
|
|
|
for listing in self {
|
|
|
|
let listing = listing?;
|
|
|
|
list.push(listing);
|
|
|
|
}
|
2024-01-01 23:32:40 -05:00
|
|
|
list.sort_unstable();
|
2023-12-29 23:52:05 -05:00
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
}
|