Skip to content

Commit

Permalink
Review from refi64 (5)
Browse files Browse the repository at this point in the history
Instead of passing the Copy struct to the copy functions now they take
the content as arguments. We don't need the unwrapping functions then.

Signed-off-by: Rafael Garcia Ruiz <rafael.garcia@collabora.com>
  • Loading branch information
Razaloc committed Dec 13, 2022
1 parent 042afa4 commit 6f11483
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions bmap-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ enum Image {
Url(Url),
}

impl Image {
fn path(self) -> Result<PathBuf> {
if let Image::Path(c) = self {
Ok(c)
} else {
bail!("Not a path")
}
}

fn url(self) -> Result<Url> {
if let Image::Url(d) = self {
Ok(d)
} else {
bail!("Not a url")
}
}
}

#[derive(Debug)]
struct Copy {
image: Image,
Expand Down Expand Up @@ -187,15 +169,14 @@ fn setup_output<T: AsRawFd>(output: &T, bmap: &Bmap, metadata: std::fs::Metadata

async fn copy(c: Copy) -> Result<()> {
match c.image {
Image::Path(_) => copy_local_input(c),
Image::Url(_) => copy_remote_input(c).await,
Image::Path(path) => copy_local_input(path, c.dest),
Image::Url(url) => copy_remote_input(url, c.dest).await,
}
}

fn copy_local_input(c: Copy) -> Result<()> {
let path = c.image.path()?;
ensure!(path.exists(), "Image file doesn't exist");
let bmap = find_bmap(&path).ok_or_else(|| anyhow!("Couldn't find bmap file"))?;
fn copy_local_input(source: PathBuf, destination: PathBuf) -> Result<()> {
ensure!(source.exists(), "Image file doesn't exist");
let bmap = find_bmap(&source).ok_or_else(|| anyhow!("Couldn't find bmap file"))?;
println!("Found bmap file: {}", bmap.display());

let mut b = File::open(&bmap).context("Failed to open bmap file")?;
Expand All @@ -206,11 +187,11 @@ fn copy_local_input(c: Copy) -> Result<()> {
let output = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(c.dest)?;
.open(destination)?;

setup_output(&output, &bmap, output.metadata()?)?;

let mut input = setup_local_input(&path)?;
let mut input = setup_local_input(&source)?;
let pb = setup_progress_bar(&bmap);
bmap::copy(&mut input, &mut pb.wrap_write(&output), &bmap)?;
pb.finish_and_clear();
Expand All @@ -221,9 +202,8 @@ fn copy_local_input(c: Copy) -> Result<()> {
Ok(())
}

async fn copy_remote_input(c: Copy) -> Result<()> {
let url = c.image.url()?;
let bmap_url = find_remote_bmap(url.clone())?;
async fn copy_remote_input(source: Url, destination: PathBuf) -> Result<()> {
let bmap_url = find_remote_bmap(source.clone())?;

let xml = reqwest::get(bmap_url.clone()).await?.text().await?;
println!("Found bmap file: {}", bmap_url);
Expand All @@ -232,12 +212,12 @@ async fn copy_remote_input(c: Copy) -> Result<()> {
let mut output = tokio::fs::OpenOptions::new()
.write(true)
.create(true)
.open(c.dest)
.open(destination)
.await?;

setup_output(&output, &bmap, output.metadata().await?)?;

let res = setup_remote_input(url).await?;
let res = setup_remote_input(source).await?;
let stream = res
.bytes_stream()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
Expand Down

0 comments on commit 6f11483

Please sign in to comment.