Implement iterator for a stream of nodes

This commit is contained in:
Nathan Fisher 2023-07-04 01:19:07 -04:00
parent 82ede31688
commit 8f8e54220c
2 changed files with 32 additions and 0 deletions

View File

@ -4,6 +4,9 @@ mod file;
mod filetype;
mod node;
mod special;
mod stream;
pub use {
checksum::Checksum, error::Error, file::File, filetype::FileType, node::Node, special::Special,
stream::Stream,
};

29
src/stream.rs Normal file
View File

@ -0,0 +1,29 @@
use {
crate::{Error, Node},
std::{
io::{ErrorKind, Read},
iter::Iterator,
},
};
#[derive(Debug)]
pub struct Stream<T: Read> {
reader: T,
}
impl<T: Read> From<T> for Stream<T> {
fn from(value: T) -> Self {
Self { reader: value }
}
}
impl<T: Read> Iterator for Stream<T> {
type Item = Result<Node, Error>;
fn next(&mut self) -> Option<Self::Item> {
match Node::read(&mut self.reader) {
Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => None,
x => Some(x),
}
}
}