From cffd5b24dc6644c3727bb6cd82d8f89e80c9c42f Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 19 Dec 2018 23:49:36 +0000 Subject: [PATCH 1/2] Use ToSemver more --- src/cargo/core/package_id.rs | 2 +- src/cargo/core/package_id_spec.rs | 14 +++++++------- src/cargo/ops/cargo_install.rs | 6 +++--- src/cargo/sources/registry/index.rs | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index 784a3491573..9182cb20d54 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -76,7 +76,7 @@ impl<'de> de::Deserialize<'de> for PackageId { 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")), diff --git a/src/cargo/core/package_id_spec.rs b/src/cargo/core/package_id_spec.rs index a2cb6171b80..5c8421b832c 100644 --- a/src/cargo/core/package_id_spec.rs +++ b/src/cargo/core/package_id_spec.rs @@ -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", "")?; @@ -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] @@ -289,7 +289,7 @@ 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()), }, ); @@ -297,7 +297,7 @@ mod tests { "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()), }, ); @@ -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()), }, ); @@ -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()), }, ); @@ -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, }, ); diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index dd1523a1ee0..b193131a927 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -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; @@ -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)] @@ -456,7 +456,7 @@ where v ), }, - _ => match v.parse::() { + _ => match v.to_semver() { Ok(v) => Some(format!("={}", v)), Err(_) => { let mut msg = format!( diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index 353115535c4..b165c4c65d1 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -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 @@ -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 { From 1606ab3a79384c330f3d0f52184ec0fd4055a0fc Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 20 Dec 2018 10:36:43 +0000 Subject: [PATCH 2/2] Replace PackageId::wrap with PackageId::pure --- src/cargo/core/package_id.rs | 37 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index 9182cb20d54..c25a668e4e9 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -72,6 +72,7 @@ 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")), @@ -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)) } } @@ -118,15 +115,15 @@ impl<'a> Hash for PackageId { impl PackageId { pub fn new(name: &str, version: T, sid: SourceId) -> CargoResult { 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)); @@ -147,19 +144,15 @@ impl PackageId { } pub fn with_precise(self, precise: Option) -> 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<'_> {