use crate::metadata::{Metadata, MetadataRef, MetadataRefs, MetadataTag};
use crate::Result;
use crate::{Chd, Hunk};
use lending_iterator::prelude::*;
use std::io::{Read, Seek};
#[::nougat::gat(Item)]
pub use lending_iterator::lending_iterator::LendingIterator;
pub struct Hunks<'a, F: Read + Seek> {
inner: &'a mut Chd<F>,
last_hunk: u32,
current_hunk: u32,
}
impl<'a, F: Read + Seek> Hunks<'a, F> {
pub(crate) fn new(inner: &'a mut Chd<F>) -> Self {
let last_hunk = inner.header().hunk_count();
Hunks {
inner,
last_hunk,
current_hunk: 0,
}
}
}
#[::nougat::gat]
impl<'a, F: Read + Seek> LendingIterator for Hunks<'a, F> {
type Item<'next>
where
Self: 'next,
= Hunk<'next, F>;
fn next(&'_ mut self) -> Option<Hunk<'_, F>> {
if self.current_hunk == self.last_hunk {
return None;
}
let curr = self.current_hunk;
self.current_hunk += 1;
self.inner.hunk(curr).ok()
}
}
pub struct MetadataEntries<'a, F: Read + Seek + 'a> {
inner: MetadataRefs<'a, F>,
}
impl<'a, F: Read + Seek + 'a> MetadataEntries<'a, F> {
pub(crate) fn new(inner: MetadataRefs<'a, F>) -> Self {
MetadataEntries { inner }
}
}
impl<'a, F: Read + Seek + 'a> MetadataEntry<'a, F> {
pub fn read(&mut self) -> Result<Metadata> {
self.meta_ref.read(self.file)
}
}
pub struct MetadataEntry<'a, F: Read + Seek + 'a> {
meta_ref: MetadataRef,
file: &'a mut F,
}
impl<'a, F: Read + Seek + 'a> MetadataTag for MetadataEntry<'a, F> {
fn metatag(&self) -> u32 {
self.meta_ref.metatag()
}
}
#[::nougat::gat]
impl<'a, F: Read + Seek> LendingIterator for MetadataEntries<'a, F> {
type Item<'next>
where
Self: 'next,
= MetadataEntry<'next, F>;
fn next(&'_ mut self) -> Option<Item<'_, Self>> {
let next = self.inner.next();
if let Some(next) = next {
Some(MetadataEntry {
meta_ref: next,
file: self.inner.file,
})
} else {
None
}
}
}