+ async source
(reader and converter)
This commit is contained in:
+120
-3
@@ -69,7 +69,7 @@ pub struct Question {
|
||||
pub batch_info: BatchInfo,
|
||||
}
|
||||
|
||||
#[cfg(feature = "source")]
|
||||
#[cfg(feature = "convert")]
|
||||
pub mod convert {
|
||||
use super::{BatchInfo, Question};
|
||||
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
||||
@@ -138,6 +138,123 @@ pub mod convert {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "source")]
|
||||
#[cfg(feature = "convert")]
|
||||
pub use convert::QuestionsConverter;
|
||||
|
||||
#[cfg(feature = "convert_async")]
|
||||
pub mod convert_async {
|
||||
use async_stream::stream;
|
||||
use futures_core::stream::Stream;
|
||||
use futures_core::Future;
|
||||
use futures_util::{pin_mut, StreamExt};
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use super::{BatchInfo, Question};
|
||||
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
||||
|
||||
macro_rules! make {
|
||||
($Target:ident; by {$($field:ident),+}; from $src:expr) => {$Target {$(
|
||||
$field: $src.$field
|
||||
),+}};
|
||||
($Target:ident; with defaults and by {$($field:ident),+}; from $src:expr) => {$Target {$(
|
||||
$field: $src.$field
|
||||
),+ ,..$Target::default()}}
|
||||
}
|
||||
|
||||
impl From<SourceQuestion> for Question {
|
||||
fn from(src: SourceQuestion) -> Self {
|
||||
make! {Self; with defaults and by {
|
||||
num, id, description, answer, author, comment, comment1, tour, url,
|
||||
date, processed_by, redacted_by, copyright, theme, kind, source, rating
|
||||
}; from src}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SourceQuestionsBatch> for BatchInfo {
|
||||
fn from(src: SourceQuestionsBatch) -> Self {
|
||||
make! {Self; by {
|
||||
filename, description, author, comment, url, date,
|
||||
processed_by, redacted_by, copyright, theme, kind, source, rating
|
||||
}; from src}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SourceQuestionsBatch> for Vec<Question> {
|
||||
fn from(src: SourceQuestionsBatch) -> Self {
|
||||
let mut result: Vec<Question> = src
|
||||
.questions
|
||||
.iter()
|
||||
.map(|item| item.clone().into())
|
||||
.collect();
|
||||
let batch_info = BatchInfo::from(src);
|
||||
result.iter_mut().for_each(|question| {
|
||||
question.batch_info = batch_info.clone();
|
||||
});
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub struct QuestionsConverterAsync<T>
|
||||
where
|
||||
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
||||
+ std::marker::Unpin,
|
||||
{
|
||||
inner: T,
|
||||
}
|
||||
|
||||
impl<T> From<T> for QuestionsConverterAsync<T>
|
||||
where
|
||||
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
||||
+ std::marker::Unpin,
|
||||
{
|
||||
fn from(inner: T) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> QuestionsConverterAsync<T>
|
||||
where
|
||||
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
||||
+ std::marker::Unpin,
|
||||
{
|
||||
fn convert(&mut self) -> impl Stream<Item = Question> + '_ {
|
||||
stream! {
|
||||
while let Some((filename, Ok(batch))) = self.inner.next().await {
|
||||
let mut batch = batch;
|
||||
batch.filename = filename;
|
||||
let questions: Vec<Question> = batch.into();
|
||||
for question in questions {
|
||||
yield question
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Stream for QuestionsConverterAsync<T>
|
||||
where
|
||||
T: Stream<Item = (String, Result<SourceQuestionsBatch, serde_json::Error>)>
|
||||
+ std::marker::Unpin,
|
||||
{
|
||||
type Item = Question;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let convert = self.convert();
|
||||
pin_mut!(convert);
|
||||
match Pin::new(&mut convert.next()).poll(cx) {
|
||||
Poll::Ready(Some(item)) => Poll::Ready(Some(item)),
|
||||
Poll::Ready(None) => Poll::Ready(None),
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "convert_async")]
|
||||
pub use convert_async::QuestionsConverterAsync;
|
||||
|
||||
Reference in New Issue
Block a user