Skip to content

Commit

Permalink
refactor: VOICEVOX#928 用のコード移動 (VOICEVOX#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip authored Jan 19, 2025
1 parent 25ba079 commit b20f8b6
Showing 1 changed file with 64 additions and 66 deletions.
130 changes: 64 additions & 66 deletions crates/downloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ static OPEN_JTALK_DIC_URL: LazyLock<Url> = LazyLock::new(|| {
.unwrap()
});

static PROGRESS_STYLE0: LazyLock<ProgressStyle> =
LazyLock::new(|| ProgressStyle::with_template("{prefix}").unwrap());
static PROGRESS_STYLE1: LazyLock<ProgressStyle> = LazyLock::new(|| {
ProgressStyle::with_template(
"{prefix:55} {bytes:>11} {bytes_per_sec:>13} {elapsed_precise} {bar} {percent:>3}%",
)
.unwrap()
});
static PROGRESS_STYLE2: LazyLock<ProgressStyle> =
LazyLock::new(|| ProgressStyle::with_template("{prefix:55} {spinner} {msg}").unwrap());

#[derive(clap::Parser)]
struct Args {
/// ダウンロード対象を限定する
Expand Down Expand Up @@ -587,15 +598,12 @@ fn add_progress_bar(
prefix: impl Into<Cow<'static, str>>,
) -> ProgressBar {
let pb = progresses.add(ProgressBar::new(len));
pb.set_style(PROGRESS_STYLE.clone());
pb.set_style(PROGRESS_STYLE0.clone());
pb.enable_steady_tick(INTERVAL);
pb.set_prefix(prefix);
return pb;

const INTERVAL: Duration = Duration::from_millis(100);

static PROGRESS_STYLE: LazyLock<ProgressStyle> =
LazyLock::new(|| ProgressStyle::with_template("{prefix}").unwrap());
}

async fn download_and_extract(
Expand All @@ -613,68 +621,6 @@ async fn download_and_extract(
let files = &read_archive(archive, archive_kind, pb.clone()).await?;
return extract(files, stripping, output, pb).await;

static PROGRESS_STYLE1: LazyLock<ProgressStyle> = LazyLock::new(|| {
ProgressStyle::with_template(
"{prefix:55} {bytes:>11} {bytes_per_sec:>13} {elapsed_precise} {bar} {percent:>3}%",
)
.unwrap()
});

static PROGRESS_STYLE2: LazyLock<ProgressStyle> =
LazyLock::new(|| ProgressStyle::with_template("{prefix:55} {spinner} {msg}").unwrap());

async fn with_style(
pb: ProgressBar,
style: &'static ProgressStyle,
) -> Result<ProgressBar, JoinError> {
tokio::task::spawn_blocking(move || {
pb.set_style(style.clone());
pb
})
.await
}

async fn download(
mut bytes_stream: impl Stream<Item = anyhow::Result<Bytes>> + Unpin,
content_length: Option<u64>,
pb: ProgressBar,
) -> anyhow::Result<Vec<u8>> {
if let Some(content_length) = content_length {
pb.set_length(content_length);
}

with_progress(pb, |pos_tx| async move {
let mut downloaded = Vec::with_capacity(content_length.unwrap_or(0) as _);
while let Some(chunk) = bytes_stream.next().await.transpose()? {
downloaded.extend_from_slice(&chunk);
pos_tx.send(downloaded.len() as _)?;
}
Ok(downloaded)
})
.await
}

async fn with_progress<Fun, Fut, Out>(pb: ProgressBar, f: Fun) -> anyhow::Result<Out>
where
Fun: FnOnce(tokio::sync::mpsc::UnboundedSender<u64>) -> Fut,
Fut: Future<Output = anyhow::Result<Out>>,
{
let (pos_tx, mut pos_rx) = tokio::sync::mpsc::unbounded_channel();

let (result1, result2) = futures_util::future::join(
tokio::task::spawn_blocking(move || {
while let Some(pos) = pos_rx.blocking_recv() {
pb.set_position(pos);
}
}),
f(pos_tx),
)
.await;

result1?;
result2
}

async fn read_archive(
archive: Vec<u8>,
archive_kind: ArchiveKind,
Expand Down Expand Up @@ -764,6 +710,58 @@ async fn download_and_extract(
}
}

async fn with_style(
pb: ProgressBar,
style: &'static ProgressStyle,
) -> Result<ProgressBar, JoinError> {
tokio::task::spawn_blocking(move || {
pb.set_style(style.clone());
pb
})
.await
}

async fn download(
mut bytes_stream: impl Stream<Item = anyhow::Result<Bytes>> + Unpin,
content_length: Option<u64>,
pb: ProgressBar,
) -> anyhow::Result<Vec<u8>> {
if let Some(content_length) = content_length {
pb.set_length(content_length);
}

return with_progress(pb, |pos_tx| async move {
let mut downloaded = Vec::with_capacity(content_length.unwrap_or(0) as _);
while let Some(chunk) = bytes_stream.next().await.transpose()? {
downloaded.extend_from_slice(&chunk);
pos_tx.send(downloaded.len() as _)?;
}
Ok(downloaded)
})
.await;

async fn with_progress<Fun, Fut, Out>(pb: ProgressBar, f: Fun) -> anyhow::Result<Out>
where
Fun: FnOnce(tokio::sync::mpsc::UnboundedSender<u64>) -> Fut,
Fut: Future<Output = anyhow::Result<Out>>,
{
let (pos_tx, mut pos_rx) = tokio::sync::mpsc::unbounded_channel();

let (result1, result2) = futures_util::future::join(
tokio::task::spawn_blocking(move || {
while let Some(pos) = pos_rx.blocking_recv() {
pb.set_position(pos);
}
}),
f(pos_tx),
)
.await;

result1?;
result2
}
}

struct GhAsset {
octocrab: Arc<Octocrab>,
repo: RepoName,
Expand Down

0 comments on commit b20f8b6

Please sign in to comment.