From 37bec60fa3a1bc1827b4bfb90e46e53cd17908ac Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Mon, 26 Jun 2017 22:44:13 +0200 Subject: [PATCH 1/2] Implement semver ranges for install --vers --- src/cargo/ops/cargo_install.rs | 57 +++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 5421ebbcee3..bb2da606297 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -8,7 +8,7 @@ use std::io::SeekFrom; use std::path::{Path, PathBuf}; use std::sync::Arc; -use semver::Version; +use semver::{Version, VersionReq}; use tempdir::TempDir; use toml; @@ -276,20 +276,49 @@ fn select_pkg<'a, T>(mut source: T, Some(name) => { let vers = match vers { Some(v) => { - match v.parse::() { - Ok(v) => Some(format!("={}", v)), - Err(_) => { - let msg = format!("the `--vers` provided, `{}`, is \ - not a valid semver version\n\n\ - historically Cargo treated this \ - as a semver version requirement \ - accidentally\nand will continue \ - to do so, but this behavior \ - will be removed eventually", v); - config.shell().warn(&msg)?; - Some(v.to_string()) - } + + // If the version begins with character <, >, =, ^, ~ parse it as a + // version range, otherwise parse it as a specific version + let first = v.chars() + .nth(0) + .ok_or("no version provided for the `--vers` flag")?; + + match first { + '<' | '>' | '=' | '^' | '~' => match v.parse::() { + Ok(v) => Some(v.to_string()), + Err(_) => { + let msg = format!("the `--vers` provided, `{}`, is \ + not a valid semver version requirement\n\n + Please have a look at \ + http://doc.crates.io/specifying-dependencies.html \ + for the correct format", v); + return Err(msg.into()); + } + }, + _ => match v.parse::() { + Ok(v) => Some(format!("={}", v)), + Err(_) => { + let mut msg = format!("the `--vers` provided, `{}`, is \ + not a valid semver version\n\n\ + historically Cargo treated this \ + as a semver version requirement \ + accidentally\nand will continue \ + to do so, but this behavior \ + will be removed eventually", v); + + // If it is not a valid version but it is a valid version + // requirement, add a note to the warning + if v.parse::().is_ok() { + msg.push_str(&format!("\nif you want to specify semver range, \ + add an explicit qualifier, like ^{}", v)); + } + config.shell().warn(&msg)?; + Some(v.to_string()) + }, + }, } + + } None => None, }; From d9afb2d6931e43483a3f88d017d79e708476923c Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 27 Jun 2017 12:23:44 +0200 Subject: [PATCH 2/2] Remove extra blank lines and commas on last match arm for consistency --- src/cargo/ops/cargo_install.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index bb2da606297..9e0b22b5815 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -314,11 +314,9 @@ fn select_pkg<'a, T>(mut source: T, } config.shell().warn(&msg)?; Some(v.to_string()) - }, - }, + } + } } - - } None => None, };