Skip to content

Commit

Permalink
Auto merge of #6476 - dwijnand:salvaged-semver-work, r=Eh2406
Browse files Browse the repository at this point in the history
Salvaged semver work

None
  • Loading branch information
bors committed Dec 22, 2018
2 parents 8e547a7 + 1606ab3 commit e593efc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
39 changes: 16 additions & 23 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ impl<'de> de::Deserialize<'de> for PackageId {
let string = String::deserialize(d)?;
let mut s = string.splitn(3, ' ');
let name = s.next().unwrap();
let name = InternedString::new(name);
let version = match s.next() {
Some(s) => s,
None => return Err(de::Error::custom("invalid serialized PackageId")),
};
let version = semver::Version::parse(version).map_err(de::Error::custom)?;
let version = version.to_semver().map_err(de::Error::custom)?;
let url = match s.next() {
Some(s) => s,
None => return Err(de::Error::custom("invalid serialized PackageId")),
Expand All @@ -88,11 +89,7 @@ impl<'de> de::Deserialize<'de> for PackageId {
};
let source_id = SourceId::from_url(url).map_err(de::Error::custom)?;

Ok(PackageId::wrap(PackageIdInner {
name: InternedString::new(name),
version,
source_id,
}))
Ok(PackageId::pure(name, version, source_id))
}
}

Expand All @@ -118,15 +115,15 @@ impl<'a> Hash for PackageId {
impl PackageId {
pub fn new<T: ToSemver>(name: &str, version: T, sid: SourceId) -> CargoResult<PackageId> {
let v = version.to_semver()?;

Ok(PackageId::wrap(PackageIdInner {
name: InternedString::new(name),
version: v,
source_id: sid,
}))
Ok(PackageId::pure(InternedString::new(name), v, sid))
}

fn wrap(inner: PackageIdInner) -> PackageId {
pub fn pure(name: InternedString, version: semver::Version, source_id: SourceId) -> PackageId {
let inner = PackageIdInner {
name,
version,
source_id,
};
let mut cache = PACKAGE_ID_CACHE.lock().unwrap();
let inner = cache.get(&inner).cloned().unwrap_or_else(|| {
let inner = Box::leak(Box::new(inner));
Expand All @@ -147,19 +144,15 @@ impl PackageId {
}

pub fn with_precise(self, precise: Option<String>) -> PackageId {
PackageId::wrap(PackageIdInner {
name: self.inner.name,
version: self.inner.version.clone(),
source_id: self.inner.source_id.with_precise(precise),
})
PackageId::pure(
self.inner.name,
self.inner.version.clone(),
self.inner.source_id.with_precise(precise),
)
}

pub fn with_source_id(self, source: SourceId) -> PackageId {
PackageId::wrap(PackageIdInner {
name: self.inner.name,
version: self.inner.version.clone(),
source_id: source,
})
PackageId::pure(self.inner.name, self.inner.version.clone(), source)
}

pub fn stable_hash(self, workspace: &Path) -> PackageIdStableHash<'_> {
Expand Down
14 changes: 7 additions & 7 deletions src/cargo/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl PackageIdSpec {
let mut parts = spec.splitn(2, ':');
let name = parts.next().unwrap();
let version = match parts.next() {
Some(version) => Some(Version::parse(version)?),
Some(version) => Some(version.to_semver()?),
None => None,
};
validate_package_name(name, "pkgid", "")?;
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<'de> de::Deserialize<'de> for PackageIdSpec {
mod tests {
use super::PackageIdSpec;
use crate::core::{PackageId, SourceId};
use semver::Version;
use crate::util::ToSemver;
use url::Url;

#[test]
Expand All @@ -289,15 +289,15 @@ mod tests {
"http://crates.io/foo#1.2.3",
PackageIdSpec {
name: "foo".to_string(),
version: Some(Version::parse("1.2.3").unwrap()),
version: Some("1.2.3".to_semver().unwrap()),
url: Some(Url::parse("http://crates.io/foo").unwrap()),
},
);
ok(
"http://crates.io/foo#bar:1.2.3",
PackageIdSpec {
name: "bar".to_string(),
version: Some(Version::parse("1.2.3").unwrap()),
version: Some("1.2.3".to_semver().unwrap()),
url: Some(Url::parse("http://crates.io/foo").unwrap()),
},
);
Expand All @@ -313,7 +313,7 @@ mod tests {
"crates.io/foo#1.2.3",
PackageIdSpec {
name: "foo".to_string(),
version: Some(Version::parse("1.2.3").unwrap()),
version: Some("1.2.3".to_semver().unwrap()),
url: Some(Url::parse("cargo://crates.io/foo").unwrap()),
},
);
Expand All @@ -329,7 +329,7 @@ mod tests {
"crates.io/foo#bar:1.2.3",
PackageIdSpec {
name: "bar".to_string(),
version: Some(Version::parse("1.2.3").unwrap()),
version: Some("1.2.3".to_semver().unwrap()),
url: Some(Url::parse("cargo://crates.io/foo").unwrap()),
},
);
Expand All @@ -345,7 +345,7 @@ mod tests {
"foo:1.2.3",
PackageIdSpec {
name: "foo".to_string(),
version: Some(Version::parse("1.2.3").unwrap()),
version: Some("1.2.3".to_semver().unwrap()),
url: None,
},
);
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{env, fs};

use semver::{Version, VersionReq};
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use tempfile::Builder as TempFileBuilder;

Expand All @@ -19,7 +19,7 @@ use crate::ops::{self, CompileFilter};
use crate::sources::{GitSource, PathSource, SourceConfigMap};
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::paths;
use crate::util::{internal, Config};
use crate::util::{internal, Config, ToSemver};
use crate::util::{FileLock, Filesystem};

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -456,7 +456,7 @@ where
v
),
},
_ => match v.parse::<Version>() {
_ => match v.to_semver() {
Ok(v) => Some(format!("={}", v)),
Err(_) => {
let mut msg = format!(
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::core::dependency::Dependency;
use crate::core::{PackageId, SourceId, Summary};
use crate::sources::registry::RegistryData;
use crate::sources::registry::{RegistryPackage, INDEX_LOCK};
use crate::util::{internal, CargoResult, Config, Filesystem};
use crate::util::{internal, CargoResult, Config, Filesystem, ToSemver};

/// Crates.io treats hyphen and underscores as interchangeable
/// but, the index and old cargo do not. So the index must store uncanonicalized version
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<'cfg> RegistryIndex<'cfg> {
let mut vers = p[name.len() + 1..].splitn(2, "->");
if dep
.version_req()
.matches(&Version::parse(vers.next().unwrap()).unwrap())
.matches(&vers.next().unwrap().to_semver().unwrap())
{
vers.next().unwrap() == s.version().to_string()
} else {
Expand Down

0 comments on commit e593efc

Please sign in to comment.