From 8f8e54220ceff1ff785f71bdffc6c1b17336c300 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 4 Jul 2023 01:19:07 -0400 Subject: [PATCH] Implement iterator for a stream of nodes --- src/lib.rs | 3 +++ src/stream.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/stream.rs diff --git a/src/lib.rs b/src/lib.rs index d053d01..2b48b0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, }; diff --git a/src/stream.rs b/src/stream.rs new file mode 100644 index 0000000..7345ed3 --- /dev/null +++ b/src/stream.rs @@ -0,0 +1,29 @@ +use { + crate::{Error, Node}, + std::{ + io::{ErrorKind, Read}, + iter::Iterator, + }, +}; + +#[derive(Debug)] +pub struct Stream { + reader: T, +} + +impl From for Stream { + fn from(value: T) -> Self { + Self { reader: value } + } +} + +impl Iterator for Stream { + type Item = Result; + + fn next(&mut self) -> Option { + match Node::read(&mut self.reader) { + Err(Error::Io(e)) if e.kind() == ErrorKind::UnexpectedEof => None, + x => Some(x), + } + } +}