From b6f15f2e5e4045c2d8f82f2a6cb7a30701d0a0c1 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 21 Jun 2022 13:42:39 +1000 Subject: [PATCH] Refactor: Extract `fetch_crate_cratesio` out into mod Signed-off-by: Jiahao XU --- src/drivers.rs | 70 ++------------------------------------- src/drivers/crates_io.rs | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 src/drivers/crates_io.rs diff --git a/src/drivers.rs b/src/drivers.rs index 9a56a7d3..18752554 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -1,12 +1,6 @@ use std::path::{Path, PathBuf}; -use std::time::Duration; -use cargo_toml::Manifest; -use crates_io_api::AsyncClient; -use log::debug; -use url::Url; - -use crate::{helpers::*, BinstallError, Meta, TarBasedFmt}; +use crate::BinstallError; mod vfs; @@ -16,66 +10,8 @@ use visitor::ManifestVisitor; mod version; use version::find_version; -/// Fetch a crate Cargo.toml by name and version from crates.io -pub async fn fetch_crate_cratesio( - name: &str, - version_req: &str, -) -> Result, BinstallError> { - // Fetch / update index - debug!("Looking up crate information"); - - // Build crates.io api client - let api_client = AsyncClient::new( - "cargo-binstall (https://github.com/ryankurte/cargo-binstall)", - Duration::from_millis(100), - ) - .expect("bug: invalid user agent"); - - // Fetch online crate information - let base_info = - api_client - .get_crate(name.as_ref()) - .await - .map_err(|err| BinstallError::CratesIoApi { - crate_name: name.into(), - err, - })?; - - // Locate matching version - let version_iter = - base_info - .versions - .iter() - .filter_map(|v| if !v.yanked { Some(&v.num) } else { None }); - let version_name = find_version(version_req, version_iter)?; - - // Fetch information for the filtered version - let version = base_info - .versions - .iter() - .find(|v| v.num == version_name.to_string()) - .ok_or_else(|| BinstallError::VersionUnavailable { - crate_name: name.into(), - v: version_name.clone(), - })?; - - debug!("Found information for crate version: '{}'", version.num); - - // Download crate to temporary dir (crates.io or git?) - let crate_url = format!("https://crates.io/{}", version.dl_path); - - debug!("Fetching crate from: {crate_url} and extracting Cargo.toml from it"); - - let manifest_dir_path: PathBuf = format!("{name}-{version_name}").into(); - - download_tar_based_and_visit( - Url::parse(&crate_url)?, - TarBasedFmt::Tgz, - ManifestVisitor::new(manifest_dir_path), - ) - .await? - .load_manifest() -} +mod crates_io; +pub use crates_io::fetch_crate_cratesio; /// Fetch a crate by name and version from github /// TODO: implement this diff --git a/src/drivers/crates_io.rs b/src/drivers/crates_io.rs new file mode 100644 index 00000000..94cdccce --- /dev/null +++ b/src/drivers/crates_io.rs @@ -0,0 +1,71 @@ +use std::path::PathBuf; +use std::time::Duration; + +use cargo_toml::Manifest; +use crates_io_api::AsyncClient; +use log::debug; +use url::Url; + +use super::{find_version, ManifestVisitor}; +use crate::{helpers::*, BinstallError, Meta, TarBasedFmt}; + +/// Fetch a crate Cargo.toml by name and version from crates.io +pub async fn fetch_crate_cratesio( + name: &str, + version_req: &str, +) -> Result, BinstallError> { + // Fetch / update index + debug!("Looking up crate information"); + + // Build crates.io api client + let api_client = AsyncClient::new( + "cargo-binstall (https://github.com/ryankurte/cargo-binstall)", + Duration::from_millis(100), + ) + .expect("bug: invalid user agent"); + + // Fetch online crate information + let base_info = + api_client + .get_crate(name.as_ref()) + .await + .map_err(|err| BinstallError::CratesIoApi { + crate_name: name.into(), + err, + })?; + + // Locate matching version + let version_iter = + base_info + .versions + .iter() + .filter_map(|v| if !v.yanked { Some(&v.num) } else { None }); + let version_name = find_version(version_req, version_iter)?; + + // Fetch information for the filtered version + let version = base_info + .versions + .iter() + .find(|v| v.num == version_name.to_string()) + .ok_or_else(|| BinstallError::VersionUnavailable { + crate_name: name.into(), + v: version_name.clone(), + })?; + + debug!("Found information for crate version: '{}'", version.num); + + // Download crate to temporary dir (crates.io or git?) + let crate_url = format!("https://crates.io/{}", version.dl_path); + + debug!("Fetching crate from: {crate_url} and extracting Cargo.toml from it"); + + let manifest_dir_path: PathBuf = format!("{name}-{version_name}").into(); + + download_tar_based_and_visit( + Url::parse(&crate_url)?, + TarBasedFmt::Tgz, + ManifestVisitor::new(manifest_dir_path), + ) + .await? + .load_manifest() +}