From 065f62a6255c6119c01eed8de63e000c9e9a94f1 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sun, 7 Aug 2022 18:42:59 +1000 Subject: [PATCH] Use `VersionReq` for `Options::version_req` and update usage of `CrateName` in `binstall::install` Signed-off-by: Jiahao XU --- src/binstall.rs | 4 ++-- src/binstall/install.rs | 4 ++-- src/binstall/resolve.rs | 29 +++++++++++++---------------- src/main.rs | 8 ++++---- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/binstall.rs b/src/binstall.rs index 73c474c3..20b582b7 100644 --- a/src/binstall.rs +++ b/src/binstall.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use compact_str::CompactString; +use semver::VersionReq; use crate::{metafiles::binstall_v1::MetaData, DesiredTargets, PkgOverride}; @@ -14,7 +14,7 @@ pub struct Options { pub no_symlinks: bool, pub dry_run: bool, pub force: bool, - pub version: Option, + pub version_req: Option, pub manifest_path: Option, pub cli_overrides: PkgOverride, pub desired_targets: DesiredTargets, diff --git a/src/binstall/install.rs b/src/binstall/install.rs index 21015333..0bf5c0e7 100644 --- a/src/binstall/install.rs +++ b/src/binstall/install.rs @@ -19,7 +19,7 @@ pub async fn install( fetcher, package, name, - version, + version_req, bin_path, bin_files, } => { @@ -31,7 +31,7 @@ pub async fn install( .map(|option| { option.map(|bins| MetaData { name, - version_req: version, + version_req, current_version, source: Source::cratesio_registry(), target, diff --git a/src/binstall/resolve.rs b/src/binstall/resolve.rs index 6290deef..d17747b4 100644 --- a/src/binstall/resolve.rs +++ b/src/binstall/resolve.rs @@ -4,10 +4,11 @@ use std::{ }; use cargo_toml::{Package, Product}; -use compact_str::{format_compact, CompactString}; +use compact_str::{CompactString, ToCompactString}; use log::{debug, error, info, warn}; use miette::{miette, Result}; use reqwest::Client; +use semver::VersionReq; use super::Options; use crate::{ @@ -21,7 +22,7 @@ pub enum Resolution { fetcher: Arc, package: Package, name: CompactString, - version: CompactString, + version_req: CompactString, bin_path: PathBuf, bin_files: Vec, }, @@ -83,30 +84,26 @@ pub async fn resolve( ) -> Result { info!("Installing package: '{}'", crate_name); - let mut version: CompactString = match (&crate_name.version, &opts.version) { + let version_req: VersionReq = match (&crate_name.version_req, &opts.version_req) { (Some(version), None) => version.clone(), (None, Some(version)) => version.clone(), (Some(_), Some(_)) => Err(BinstallError::SuperfluousVersionOption)?, - (None, None) => "*".into(), + (None, None) => VersionReq::STAR, }; - // Treat 0.1.2 as =0.1.2 - if version - .chars() - .next() - .map(|ch| ch.is_ascii_digit()) - .unwrap_or(false) - { - version = format_compact!("={version}"); - } - // Fetch crate via crates.io, git, or use a local manifest path // TODO: work out which of these to do based on `opts.name` // TODO: support git-based fetches (whole repo name rather than just crate name) let manifest = match opts.manifest_path.clone() { Some(manifest_path) => load_manifest_path(manifest_path)?, None => { - fetch_crate_cratesio(&client, &crates_io_api_client, &crate_name.name, &version).await? + fetch_crate_cratesio( + &client, + &crates_io_api_client, + &crate_name.name, + &version_req, + ) + .await? } }; @@ -175,7 +172,7 @@ pub async fn resolve( fetcher, package, name: crate_name.name, - version, + version_req: version_req.to_compact_string(), bin_path, bin_files, } diff --git a/src/main.rs b/src/main.rs index da87fc49..2aa7e58f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,9 @@ use std::{ }; use clap::{builder::PossibleValue, AppSettings, Parser}; -use compact_str::CompactString; use log::{debug, error, info, warn, LevelFilter}; use miette::{miette, Result, WrapErr}; +use semver::VersionReq; use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode}; use tokio::{runtime::Runtime, task::block_in_place}; @@ -46,8 +46,8 @@ struct Options { /// /// Cannot be used when multiple packages are installed at once, use the attached version /// syntax in that case. - #[clap(help_heading = "Package selection", long = "version")] - version_req: Option, + #[clap(help_heading = "Package selection", long = "version", parse(try_from_str = parse_version))] + version_req: Option, /// Override binary target set. /// @@ -371,7 +371,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { no_symlinks: opts.no_symlinks, dry_run: opts.dry_run, force: opts.force, - version: opts.version_req.take(), + version_req: opts.version_req.take(), manifest_path: opts.manifest_path.take(), cli_overrides, desired_targets,