30 lines
575 B
Rust
30 lines
575 B
Rust
|
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),
|
||
|
}
|
||
|
}
|
||
|
}
|