Skip to content

Commit

Permalink
Reduce duplicated code
Browse files Browse the repository at this point in the history
Take shared code between copy_local_input and copy_remote_input into
helper functions.

Signed-off-by: Rafael Garcia Ruiz <rafael.garcia@collabora.com>
  • Loading branch information
Razaloc committed Nov 18, 2022
1 parent b9e2d8b commit 8691dc5
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions bmap-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ async fn setup_remote_input(url: Url) -> Result<Response> {
reqwest::get(url).await.map_err(anyhow::Error::new)
}

fn setup_progress_bar(bmap: &Bmap) -> Result<ProgressBar> {
let pb = ProgressBar::new(bmap.total_mapped_size());
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
Ok(pb)
}

fn setup_output<T: AsRawFd>(output: &T, bmap: &Bmap, metadata: std::fs::Metadata) -> Result<()> {
if metadata.is_file() {
ftruncate(output.as_raw_fd(), bmap.image_size() as i64)
.context("Failed to truncate file")?;
}
Ok(())
}

async fn copy(c: Copy) -> Result<()> {
match c.image {
Image::Path(_) => copy_local_input(c),
Expand All @@ -188,17 +205,10 @@ fn copy_local_input(c: Copy) -> Result<()> {
.create(true)
.open(c.dest)?;

if output.metadata()?.is_file() {
ftruncate(output.as_raw_fd(), bmap.image_size() as i64)
.context("Failed to truncate file")?;
}
setup_output(&output, &bmap, output.metadata()?)?;

let mut input = setup_local_input(&path)?;
let pb = ProgressBar::new(bmap.total_mapped_size());
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
let pb = setup_progress_bar(&bmap)?;
bmap::copy(&mut input, &mut pb.wrap_write(&output), &bmap)?;
pb.finish_and_clear();

Expand All @@ -222,10 +232,7 @@ async fn copy_remote_input(c: Copy) -> Result<()> {
.open(c.dest)
.await?;

if output.metadata().await?.is_file() {
ftruncate(output.as_raw_fd(), bmap.image_size() as i64)
.context("Failed to truncate file")?;
}
setup_output(&output, &bmap, output.metadata().await?)?;

let res = setup_remote_input(url).await?;
let stream = res
Expand All @@ -234,11 +241,7 @@ async fn copy_remote_input(c: Copy) -> Result<()> {
.into_async_read();
let reader = async_compression::futures::bufread::GzipDecoder::new(stream);
let mut input = AsyncDiscarder::new(reader);
let pb = ProgressBar::new(bmap.total_mapped_size());
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
let pb = setup_progress_bar(&bmap)?;
bmap::copy_async(
&mut input,
&mut pb.wrap_async_write(&mut output).compat(),
Expand Down

0 comments on commit 8691dc5

Please sign in to comment.