use std::io::Read;

use crate::byte_record::ByteRecord;
use crate::fw_error::Result;
use crate::fw_reader::FwReader;

/// Iterator yielding [`ByteRecord<'static>`] for each record in the source.
#[derive(Debug)]
pub struct ByteRecordIterator<R> {
    reader: FwReader<R>,
}

impl<R> ByteRecordIterator<R> {
    /// Creates a new iterator from the given reader.
    pub(crate) fn new(reader: FwReader<R>) -> Self {
        Self { reader }
    }
}

impl<R: Read> Iterator for ByteRecordIterator<R> {
    type Item = Result<ByteRecord<'static>>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.reader.read_owned_byte_record() {
            Ok(Some(record)) => Some(Ok(record)),
            Ok(None) => None,
            Err(e) => Some(Err(e)),
        }
    }
}
