use crate::{
message::{mail::Mail, raw_body::RawBody},
ParserError, ParserResult,
};
#[async_trait::async_trait]
pub trait MailParser: Default {
fn parse_sync(&mut self, raw: Vec<Vec<u8>>) -> ParserResult<either::Either<RawBody, Mail>>;
async fn parse<'a>(
&'a mut self,
mut stream: impl tokio_stream::Stream<Item = Result<Vec<u8>, ParserError>> + Unpin + Send + 'a,
) -> ParserResult<either::Either<RawBody, Mail>> {
let mut buffer = Vec::with_capacity(20_000_000);
while let Some(i) = tokio_stream::StreamExt::try_next(&mut stream).await? {
buffer.push(i);
}
self.parse_sync(buffer)
}
fn convert(mut self, input: &RawBody) -> ParserResult<Option<Mail>> {
let raw = input.to_string();
self.parse_sync(raw.lines().map(|l| l.as_bytes().to_vec()).collect())
.map(|either| match either {
either::Left(_) => None,
either::Right(mail) => Some(mail),
})
}
}