use core::{task::{Context, Poll}};
use futures::{AsyncRead, AsyncBufRead};
use crate::backend::{Format, FormatDecode};
pub enum DecodeStatus<Dta, Dec> {
Ready (Dta),
Pending(Dec),
}
impl<Dta, Dec> DecodeStatus<Dta, Dec> {
pub fn bimap<Fdta, Gdec, F: FnOnce(Dta) -> Fdta, G: FnOnce(Dec) -> Gdec>(self, f: F, g: G) -> DecodeStatus<Fdta, Gdec> {
match self {
Self::Ready (dta) => DecodeStatus::Ready (f(dta)),
Self::Pending(dec) => DecodeStatus::Pending(g(dec)),
}
}
}
pub trait Decode: Sized {
type Format: FormatDecode;
type Data;
fn init() -> Self;
fn start_decode<R>(format: &Self::Format, reader: &mut R, cx: &mut Context<'_>) -> Result<DecodeStatus<Self::Data, Self>, <<Self as Decode>::Format as Format>::Error>
where
R: AsyncRead + AsyncBufRead + Unpin,
{
let mut decode = Self::init();
match decode.poll_decode(format, reader, cx) {
Poll::Ready(d) => d.map(DecodeStatus::Ready),
Poll::Pending => Ok(DecodeStatus::Pending(decode)),
}
}
fn poll_decode<R>(&mut self, format: &Self::Format, reader: &mut R, cx: &mut Context<'_>) -> Poll<Result<Self::Data, <<Self as Decode>::Format as Format>::Error>>
where
R: AsyncRead + AsyncBufRead + Unpin,
;
}