use crate::{
Result,
bytes::{Cursor, Reader},
message::Question,
};
pub struct Questions<'a> {
cursor: Cursor<'a>,
err: bool,
qd_count: u16,
qd_read: u16,
}
impl<'a> Questions<'a> {
pub(crate) fn new(cursor: Cursor<'a>, qd_count: u16) -> Self {
Self {
cursor,
err: false,
qd_count,
qd_read: 0,
}
}
fn read(&mut self) -> Option<Result<Question>> {
if self.err || self.qd_read == self.qd_count {
return None;
}
let res: Result<Question> = self.cursor.read();
if res.is_ok() {
self.qd_read += 1;
} else {
self.err = true;
}
Some(res)
}
}
impl Iterator for Questions<'_> {
type Item = Result<Question>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.read()
}
}