use std::pin::Pin;
use async_trait::async_trait;
use futures::Stream;
use crate::documents::{Blob, Document};
use crate::error::Result;
pub type DocumentStream = Pin<Box<dyn Stream<Item = Result<Document>> + Send>>;
pub type BlobStream = Pin<Box<dyn Stream<Item = Result<Blob>> + Send>>;
#[async_trait]
pub trait BaseLoader: Send + Sync {
async fn lazy_load(&self) -> Result<DocumentStream>;
async fn load(&self) -> Result<Vec<Document>> {
use futures::StreamExt;
let mut stream = self.lazy_load().await?;
let mut docs = Vec::new();
while let Some(doc_result) = stream.next().await {
docs.push(doc_result?);
}
Ok(docs)
}
}
#[async_trait]
pub trait BaseBlobParser: Send + Sync {
async fn lazy_parse(&self, blob: &Blob) -> Result<DocumentStream>;
async fn parse(&self, blob: &Blob) -> Result<Vec<Document>> {
use futures::StreamExt;
let mut stream = self.lazy_parse(blob).await?;
let mut docs = Vec::new();
while let Some(doc_result) = stream.next().await {
docs.push(doc_result?);
}
Ok(docs)
}
}
#[async_trait]
pub trait BlobLoader: Send + Sync {
async fn lazy_load(&self) -> Result<BlobStream>;
async fn load(&self) -> Result<Vec<Blob>> {
use futures::StreamExt;
let mut stream = self.lazy_load().await?;
let mut blobs = Vec::new();
while let Some(blob_result) = stream.next().await {
blobs.push(blob_result?);
}
Ok(blobs)
}
}