diff --git a/app_async/src/main.rs b/app_async/src/main.rs index 4d9fb22..a3caa1d 100644 --- a/app_async/src/main.rs +++ b/app_async/src/main.rs @@ -1,12 +1,12 @@ extern crate serde_json; use clap::{Parser, Subcommand}; -use futures::{Future, pin_mut}; +use futures::{pin_mut, Future}; use rand::distributions::Uniform; use rand::seq::IteratorRandom; use rand::{thread_rng, Rng}; use async_zip::tokio::read::seek::ZipFileReader; -use futures::stream::StreamExt; +use futures::stream::{self, StreamExt}; use std::time::Instant; use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; @@ -53,10 +53,10 @@ async fn zip_reader_task(tx: UnboundedSender) { .await .expect("open zip file reader"); let mut source_questions = archive.source_questions(); - let mut source_questions = source_questions.stream(); + let source_questions = source_questions.stream(); pin_mut!(source_questions); - let mut converter = QuestionsConverterAsync::from(source_questions).convert(); + let converter = QuestionsConverterAsync::from(source_questions).convert(); pin_mut!(converter); let mut num: u32 = 0; @@ -85,23 +85,26 @@ async fn read_from_zip(file_num: usize, mut num: usize) -> Option { .await .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 files = Uniform::new(0, files_count); rng.sample(files) } else { file_num - 1 }; - let mut source_questions = archive.source_questions(); - let mut source_questions = source_questions.stream(); - pin_mut!(source_questions); - let src = source_questions.skip(file_index).take(1); + + let src = source.get(file_index).await; + let src = stream::once(async { src.expect("get source file") }); + pin_mut!(src); let converter = QuestionsConverterAsync::from(src); let questions: Vec<_> = converter.convert().collect().await; if num == 0 { 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 @@ -162,7 +165,7 @@ async fn read_from_db(id: u32) -> Option { Err(e) => { println!("{:#?}", e); // DEBUG None - }, + } } } async fn write_db() {