use crate::{
decoder::{DecodeStatus, Decoder},
encoder::Encoder,
DecodeError,
};
#[derive(Debug, Clone, Copy)]
pub struct EncodeIter<I> {
iter: I,
encoder: Encoder,
}
impl<I> EncodeIter<I> {
pub(crate) fn new(iter: I) -> Self {
Self {
iter,
encoder: Encoder::default(),
}
}
pub fn inner(&self) -> &I {
&self.iter
}
pub fn inner_mut(&mut self) -> &mut I {
&mut self.iter
}
pub fn into_inner(self) -> I {
self.iter
}
}
impl<I> Iterator for EncodeIter<I>
where
I: Iterator<Item = u8>,
{
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if let Some(encoded) = self.encoder.next() {
return Some(encoded);
}
let byte = self.iter.next()?;
let encoded = self.encoder.push(byte);
Some(encoded)
}
}
#[derive(Debug, Clone, Copy)]
pub struct DecodeIter<I> {
iter: I,
decoder: Decoder,
}
impl<I> DecodeIter<I> {
pub(crate) fn new(iter: I) -> Self {
Self {
iter,
decoder: Decoder::default(),
}
}
pub fn inner(&self) -> &I {
&self.iter
}
pub fn inner_mut(&mut self) -> &mut I {
&mut self.iter
}
pub fn into_inner(self) -> I {
self.iter
}
}
impl<I> Iterator for DecodeIter<I>
where
I: Iterator<Item = u8>,
{
type Item = Result<u8, DecodeError>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let next_byte = self.iter.next();
match self.decoder.push(next_byte) {
DecodeStatus::NeedMore => {}
DecodeStatus::Emit(result) => {
return result;
}
}
}
}
}