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 77d760c
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 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,13 +169,12 @@ 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(c.dest, path),
Image::Url(url) => copy_remote_input(c.dest, url).await,
}
}

fn copy_local_input(c: Copy) -> Result<()> {
let path = c.image.path()?;
fn copy_local_input(dest: PathBuf, path: PathBuf) -> Result<()> {
ensure!(path.exists(), "Image file doesn't exist");
let bmap = find_bmap(&path).ok_or_else(|| anyhow!("Couldn't find bmap file"))?;
println!("Found bmap file: {}", bmap.display());
Expand All @@ -206,7 +187,7 @@ fn copy_local_input(c: Copy) -> Result<()> {
let output = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(c.dest)?;
.open(dest)?;

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

Expand All @@ -221,8 +202,7 @@ fn copy_local_input(c: Copy) -> Result<()> {
Ok(())
}

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

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

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

0 comments on commit 77d760c

Please sign in to comment.