From 970a2211706dbb588cbacd44671549f5dbe5a62b Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 13 Jul 2020 01:30:22 +0100 Subject: [PATCH] prog_send! helper macro --- src/work_async.rs | 21 ++++++++++++--------- src/work_async/progress.rs | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/work_async.rs b/src/work_async.rs index f6aaf4b..191d959 100644 --- a/src/work_async.rs +++ b/src/work_async.rs @@ -14,7 +14,6 @@ mod tasklist; #[macro_use] mod progress; -//TODO: Create a progress task module for atomically printing infos and stuff //TODO: Create a module for temp files, pass the temp file to `perform` and do the regex fixing after `perform` /// Download a loli async @@ -24,7 +23,8 @@ pub async fn perform(url: impl AsRef, path: impl AsRef, mut progress: let path = path.as_ref(); let task = url.to_owned(); //TODO: Real task name - progress.push_task(&task).await; + + prog_send!(link progress.push_task(&task)); let mut resp = reqwest::get(url).await?; let len = resp.content_length(); @@ -34,19 +34,22 @@ pub async fn perform(url: impl AsRef, path: impl AsRef, mut progress: .create(true) .truncate(true) .write(true) + .read(true) .open(path).await?; - progress.println(format!("req ok {:?}, file ok ({:?}), starting download", len, path)).await; + prog_send!(progress.println(format!("req ok {:?}, file ok ({:?}), starting download", len, path))); let mut bytes = resp.bytes_stream(); while let Some(buffer) = bytes.next().await { file.write(buffer?.as_ref()).await?; - progress.bump().await; + prog_send!(progress.bump()); } - progress.println(format!("done for {}", url)).await; - progress.bump().await; + //TODO: Decode `file`. + + prog_send!(progress.println(format!("done for {}", url))); + prog_send!(progress.bump()); - progress.pop_task(task).await; + prog_send!(progress.pop_task(task)); Ok(()) } @@ -83,7 +86,7 @@ pub async fn work(conf: config::Config) -> Result<(), Box })); } - prog_writer.println("Waiting for children...").await; + prog_send!(link prog_writer.println("Waiting for children...")); for child in children.into_iter() { match child.await { @@ -95,7 +98,7 @@ pub async fn work(conf: config::Config) -> Result<(), Box } prog_send!(try link prog_writer.kill()); - prog.await.expect("fatal"); + prog.await.expect("mpsc fatal"); Ok(()) } diff --git a/src/work_async/progress.rs b/src/work_async/progress.rs index b54d403..b49c4ea 100644 --- a/src/work_async/progress.rs +++ b/src/work_async/progress.rs @@ -37,6 +37,8 @@ enum CommandInternal PopTask(String), } +// TODO: Change from `Spinner` to `Bar`. Have push max, push current, etc. + struct Command { internal: CommandInternal, @@ -80,7 +82,7 @@ impl Command macro_rules! prog_send { (try link unwind $expression:expr) => { { - $expression.await.expect("fatal").wait().await.expect("fatal") + $expression.await.expect("mpsc fatal").wait().await.expect("mpsc fatal") } }; (try link $expression:expr) => { @@ -88,9 +90,9 @@ macro_rules! prog_send { $expression.await?.wait().await? } }; - (try unind $expression:expr) => { + (try unwind $expression:expr) => { { - let _ = $expression.await.expect("fatal"); + let _ = $expression.await.expect("mpsc fatal"); } }; (try $expression:expr) => { @@ -98,6 +100,13 @@ macro_rules! prog_send { let _ =$expression.await?; } }; + (link $expression:expr) => { + { + if let Ok(ok) = $expression.await { + let _ = ok.wait().await; + } + } + }; ($expression:expr) => { { let _ = $expression.await;