use std::io::Read;

use crate::fw_error::Result;
use crate::fw_reader::FwReader;
use crate::string_record::StringRecord;

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

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

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

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