add async feature #1
@ -1,12 +1,12 @@
|
|||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use futures::{Future, pin_mut};
|
use futures::{pin_mut, Future};
|
||||||
use rand::distributions::Uniform;
|
use rand::distributions::Uniform;
|
||||||
use rand::seq::IteratorRandom;
|
use rand::seq::IteratorRandom;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
use async_zip::tokio::read::seek::ZipFileReader;
|
use async_zip::tokio::read::seek::ZipFileReader;
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::{self, StreamExt};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
||||||
|
|
||||||
@ -53,10 +53,10 @@ async fn zip_reader_task(tx: UnboundedSender<Question>) {
|
|||||||
.await
|
.await
|
||||||
.expect("open zip file reader");
|
.expect("open zip file reader");
|
||||||
let mut source_questions = archive.source_questions();
|
let mut source_questions = archive.source_questions();
|
||||||
let mut source_questions = source_questions.stream();
|
let source_questions = source_questions.stream();
|
||||||
pin_mut!(source_questions);
|
pin_mut!(source_questions);
|
||||||
|
|
||||||
let mut converter = QuestionsConverterAsync::from(source_questions).convert();
|
let converter = QuestionsConverterAsync::from(source_questions).convert();
|
||||||
pin_mut!(converter);
|
pin_mut!(converter);
|
||||||
|
|
||||||
let mut num: u32 = 0;
|
let mut num: u32 = 0;
|
||||||
@ -85,23 +85,26 @@ async fn read_from_zip(file_num: usize, mut num: usize) -> Option<Question> {
|
|||||||
.await
|
.await
|
||||||
.expect("open zip file reader");
|
.expect("open zip file reader");
|
||||||
|
|
||||||
let files_count = archive.file().entries().len();
|
let mut source = archive.source_questions();
|
||||||
|
let files_count = source.len();
|
||||||
let file_index = if file_num == 0 {
|
let file_index = if file_num == 0 {
|
||||||
let files = Uniform::new(0, files_count);
|
let files = Uniform::new(0, files_count);
|
||||||
rng.sample(files)
|
rng.sample(files)
|
||||||
} else {
|
} else {
|
||||||
file_num - 1
|
file_num - 1
|
||||||
};
|
};
|
||||||
let mut source_questions = archive.source_questions();
|
|
||||||
let mut source_questions = source_questions.stream();
|
let src = source.get(file_index).await;
|
||||||
pin_mut!(source_questions);
|
let src = stream::once(async { src.expect("get source file") });
|
||||||
let src = source_questions.skip(file_index).take(1);
|
pin_mut!(src);
|
||||||
let converter = QuestionsConverterAsync::from(src);
|
let converter = QuestionsConverterAsync::from(src);
|
||||||
let questions: Vec<_> = converter.convert().collect().await;
|
let questions: Vec<_> = converter.convert().collect().await;
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
num = (1..=questions.len()).choose(&mut rng).unwrap();
|
num = (1..=questions.len()).choose(&mut rng).unwrap();
|
||||||
}
|
}
|
||||||
Some(questions[num - 1].clone())
|
let mut question = questions.get(num - 1).expect("get question").clone();
|
||||||
|
question.num = num as u32;
|
||||||
|
Some(question)
|
||||||
}
|
}
|
||||||
|
|
||||||
// measure and return time elapsed in `fut` in seconds
|
// measure and return time elapsed in `fut` in seconds
|
||||||
@ -162,7 +165,7 @@ async fn read_from_db(id: u32) -> Option<Question> {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{:#?}", e); // DEBUG
|
println!("{:#?}", e); // DEBUG
|
||||||
None
|
None
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async fn write_db() {
|
async fn write_db() {
|
||||||
|
Loading…
Reference in New Issue
Block a user