use std::io::Read;
use crate::{Adapter, ByteChunker, RcErr, SimpleAdapter};
pub struct CustomChunker<R, A> {
chunker: ByteChunker<R>,
adapter: A,
}
impl<R, A> CustomChunker<R, A> {
pub fn into_innards(self) -> (ByteChunker<R>, A) {
(self.chunker, self.adapter)
}
pub fn get_adapter(&self) -> &A { &self.adapter }
pub fn get_adapter_mut(&mut self) -> &mut A { &mut self.adapter }
}
impl<R, A> From<(ByteChunker<R>, A)> for CustomChunker<R, A> {
fn from((chunker, adapter): (ByteChunker<R>, A)) -> Self {
Self { chunker, adapter }
}
}
impl<R, A> Iterator for CustomChunker<R, A>
where
R: Read,
A: Adapter,
{
type Item = A::Item;
fn next(&mut self) -> Option<A::Item> {
let opt = self.chunker.next();
self.adapter.adapt(opt)
}
}
pub struct SimpleCustomChunker<R, A> {
chunker: ByteChunker<R>,
adapter: A,
}
impl<R, A> SimpleCustomChunker<R, A> {
pub fn into_innards(self) -> (ByteChunker<R>, A) {
(self.chunker, self.adapter)
}
pub fn get_adapter(&self) -> &A { &self.adapter }
pub fn get_adapter_mut(&mut self) -> &mut A { &mut self.adapter }
}
impl<R, A> From<(ByteChunker<R>, A)> for SimpleCustomChunker<R, A> {
fn from((chunker, adapter): (ByteChunker<R>, A)) -> Self {
Self { chunker, adapter }
}
}
impl<R, A> Iterator for SimpleCustomChunker<R, A>
where
R: Read,
A: SimpleAdapter,
{
type Item = Result<A::Item, RcErr>;
fn next(&mut self) -> Option<Self::Item> {
match self.chunker.next()? {
Ok(v) => Some(Ok(self.adapter.adapt(v))),
Err(e) => Some(Err(e)),
}
}
}