add async feature #1
@ -1,8 +1,3 @@
|
|||||||
#[cfg(all(feature = "convert", feature = "convert_async"))]
|
|
||||||
compile_error!(
|
|
||||||
"feature \"convert\" and feature \"convert_async\" cannot be enabled at the same time"
|
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
pub mod async_db;
|
pub mod async_db;
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
|
@ -69,8 +69,8 @@ pub struct Question {
|
|||||||
pub batch_info: BatchInfo,
|
pub batch_info: BatchInfo,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "convert")]
|
#[cfg(any(feature = "convert", feature = "convert_async"))]
|
||||||
pub mod convert {
|
pub mod convert_common {
|
||||||
use super::{BatchInfo, Question};
|
use super::{BatchInfo, Question};
|
||||||
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
use crate::source::{SourceQuestion, SourceQuestionsBatch};
|
||||||
|
|
||||||
@ -116,6 +116,12 @@ pub mod convert {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "convert")]
|
||||||
|
pub mod convert {
|
||||||
|
use super::Question;
|
||||||
|
use crate::source::SourceQuestionsBatch;
|
||||||
|
|
||||||
pub trait QuestionsConverter {
|
pub trait QuestionsConverter {
|
||||||
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a>;
|
fn convert<'a>(&'a mut self) -> Box<dyn Iterator<Item = Question> + 'a>;
|
||||||
@ -145,58 +151,10 @@ pub use convert::QuestionsConverter;
|
|||||||
pub mod convert_async {
|
pub mod convert_async {
|
||||||
use futures::stream;
|
use futures::stream;
|
||||||
use futures_core::stream::Stream;
|
use futures_core::stream::Stream;
|
||||||
use futures_core::Future;
|
use futures_util::StreamExt;
|
||||||
use futures_util::{pin_mut, StreamExt};
|
|
||||||
|
|
||||||
use std::future;
|
use super::Question;
|
||||||
use std::pin::Pin;
|
use crate::source::SourceQuestionsBatch;
|
||||||
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>
|
pub struct QuestionsConverterAsync<T>
|
||||||
where
|
where
|
||||||
@ -222,18 +180,22 @@ pub mod convert_async {
|
|||||||
+ std::marker::Unpin,
|
+ std::marker::Unpin,
|
||||||
{
|
{
|
||||||
pub fn convert(self) -> impl Stream<Item = Question> {
|
pub fn convert(self) -> impl Stream<Item = Question> {
|
||||||
self.inner.filter_map(|(name,r)| async move {
|
self.inner
|
||||||
if r.is_err() {
|
.filter_map(|(name, res)| async move {
|
||||||
None
|
if let Ok(item) = res {
|
||||||
|
Some((name, item))
|
||||||
} else {
|
} else {
|
||||||
Some((name, r.unwrap()))
|
None
|
||||||
}
|
}
|
||||||
}).flat_map(|(filename, batch)| stream::iter({
|
})
|
||||||
|
.flat_map(|(filename, batch)| {
|
||||||
|
stream::iter({
|
||||||
let mut batch = batch;
|
let mut batch = batch;
|
||||||
batch.filename = filename;
|
batch.filename = filename;
|
||||||
let questions: Vec<Question> = batch.into();
|
let questions: Vec<Question> = batch.into();
|
||||||
questions
|
questions
|
||||||
}))
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user