From b6bfd40c3a682fec1c1ece3f51d492e34513ffe0 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 9 Jun 2022 14:12:44 +1000 Subject: [PATCH] Use `download_and_extract` in `fetch_crate_cratesio` Signed-off-by: Jiahao XU --- src/drivers.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/drivers.rs b/src/drivers.rs index 49b615c1..3ddf654d 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -5,6 +5,7 @@ use std::time::Duration; use crates_io_api::AsyncClient; use log::debug; use semver::{Version, VersionReq}; +use url::Url; use crate::{helpers::*, BinstallError, PkgFmt}; @@ -50,7 +51,7 @@ fn find_version<'a, V: Iterator>( .ok_or(BinstallError::VersionMismatch { req: version_req }) } -/// Fetch a crate by name and version from crates.io +/// Fetch a crate Cargo.toml by name and version from crates.io pub async fn fetch_crate_cratesio( name: &str, version_req: &str, @@ -98,16 +99,17 @@ pub async fn fetch_crate_cratesio( // Download crate to temporary dir (crates.io or git?) let crate_url = format!("https://crates.io/{}", version.dl_path); - let tgz_path = temp_dir.join(format!("{name}.tgz")); - debug!("Fetching crate from: {crate_url}"); + debug!("Fetching crate from: {crate_url} and extracting Cargo.toml from it"); - // Download crate - download(&crate_url, &tgz_path).await?; + download_and_extract( + Url::parse(&crate_url)?, + PkgFmt::Tgz, + &temp_dir, + Some([Path::new("Cargo.toml").into()]), + ) + .await?; - // Decompress downloaded tgz - debug!("Decompressing crate archive"); - extract(&tgz_path, PkgFmt::Tgz, &temp_dir)?; let crate_path = temp_dir.join(format!("{name}-{version_name}")); // Return crate directory