use std::iter::FusedIterator;
use crate::spectrum::Chromatogram;
pub trait ChromatogramSource {
fn get_chromatogram_by_id(&mut self, id: &str) -> Option<Chromatogram>;
fn get_chromatogram_by_index(&mut self, index: usize) -> Option<Chromatogram>;
fn iter_chromatograms(&mut self) -> ChromatogramIterator<'_, Self>
where
Self: Sized,
{
ChromatogramIterator::new(self)
}
fn count_chromatograms(&self) -> usize;
}
#[derive(Debug)]
pub struct ChromatogramIterator<'a, R: ChromatogramSource> {
source: &'a mut R,
index: usize,
}
impl<'a, R: ChromatogramSource> ChromatogramIterator<'a, R> {
pub fn new(source: &'a mut R) -> Self {
Self { source, index: 0 }
}
}
impl<R: ChromatogramSource> Iterator for ChromatogramIterator<'_, R> {
type Item = Chromatogram;
fn next(&mut self) -> Option<Self::Item> {
if let Some(chrom) = self.source.get_chromatogram_by_index(self.index) {
self.index += 1;
Some(chrom)
} else {
None
}
}
}
impl<R: ChromatogramSource> FusedIterator for ChromatogramIterator<'_, R> {}