Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement semver ranges for install --vers #4229

Merged
merged 2 commits into from
Jun 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -276,18 +276,45 @@ fn select_pkg<'a, T>(mut source: T,
Some(name) => {
let vers = match vers {
Some(v) => {
match v.parse::<Version>() {
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::<VersionReq>() {
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::<Version>() {
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::<VersionReq>().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())
}
}
}
}
Expand Down