2014-06-03 13:25:16 -05:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-12-09 15:56:53 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
2013-12-28 11:16:48 -06:00
|
|
|
/// CrateIds identify crates and include the crate name and optionally a path
|
|
|
|
/// and version. In the full form, they look like relative URLs. Example:
|
2014-06-16 18:07:34 -05:00
|
|
|
/// `github.com/rust-lang/rust#std:1.0` would be a package ID with a path of
|
|
|
|
/// `github.com/rust-lang/rust` and a crate name of `std` with a version of
|
2013-12-14 20:58:07 -06:00
|
|
|
/// `1.0`. If no crate name is given after the hash, the name is inferred to
|
|
|
|
/// be the last component of the path. If no version is given, it is inferred
|
|
|
|
/// to be `0.0`.
|
2014-02-22 19:29:42 -06:00
|
|
|
|
|
|
|
use std::from_str::FromStr;
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq)]
|
2013-12-28 11:16:48 -06:00
|
|
|
pub struct CrateId {
|
2013-12-14 20:58:07 -06:00
|
|
|
/// A path which represents the codes origin. By convention this is the
|
|
|
|
/// URL, without `http://` or `https://` prefix, to the crate's repository
|
2014-05-22 18:57:53 -05:00
|
|
|
pub path: String,
|
2013-12-14 20:58:07 -06:00
|
|
|
/// The name of the crate.
|
2014-05-22 18:57:53 -05:00
|
|
|
pub name: String,
|
2013-12-14 20:58:07 -06:00
|
|
|
/// The version of the crate.
|
2014-05-22 18:57:53 -05:00
|
|
|
pub version: Option<String>,
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
impl fmt::Show for CrateId {
|
2014-05-28 11:24:28 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
try!(write!(f, "{}", self.path));
|
|
|
|
let version = match self.version {
|
|
|
|
None => "0.0",
|
|
|
|
Some(ref version) => version.as_slice(),
|
|
|
|
};
|
|
|
|
if self.path == self.name ||
|
|
|
|
self.path
|
|
|
|
.as_slice()
|
|
|
|
.ends_with(format!("/{}", self.name).as_slice()) {
|
|
|
|
write!(f, "#{}", version)
|
|
|
|
} else {
|
|
|
|
write!(f, "#{}:{}", self.name, version)
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
2013-12-28 11:16:48 -06:00
|
|
|
impl FromStr for CrateId {
|
|
|
|
fn from_str(s: &str) -> Option<CrateId> {
|
2014-08-07 14:56:26 -05:00
|
|
|
let pieces: Vec<&str> = s.splitn(1, '#').collect();
|
2014-05-25 05:10:11 -05:00
|
|
|
let path = pieces.get(0).to_string();
|
2013-12-09 15:56:53 -06:00
|
|
|
|
2014-05-19 19:23:26 -05:00
|
|
|
if path.as_slice().starts_with("/") || path.as_slice().ends_with("/") ||
|
|
|
|
path.as_slice().starts_with(".") || path.is_empty() {
|
2013-12-09 15:56:53 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2014-05-19 19:23:26 -05:00
|
|
|
let path_pieces: Vec<&str> = path.as_slice()
|
2014-08-07 14:56:26 -05:00
|
|
|
.rsplitn(1, '/')
|
2014-05-19 19:23:26 -05:00
|
|
|
.collect();
|
2014-02-28 14:54:01 -06:00
|
|
|
let inferred_name = *path_pieces.get(0);
|
2013-12-14 20:58:07 -06:00
|
|
|
|
|
|
|
let (name, version) = if pieces.len() == 1 {
|
2014-05-25 05:17:19 -05:00
|
|
|
(inferred_name.to_string(), None)
|
2013-12-14 20:58:07 -06:00
|
|
|
} else {
|
2014-02-28 14:54:01 -06:00
|
|
|
let hash_pieces: Vec<&str> = pieces.get(1)
|
2014-08-07 14:56:26 -05:00
|
|
|
.splitn(1, ':')
|
2014-02-28 14:54:01 -06:00
|
|
|
.collect();
|
2013-12-14 20:58:07 -06:00
|
|
|
let (hash_name, hash_version) = if hash_pieces.len() == 1 {
|
2014-02-28 14:54:01 -06:00
|
|
|
("", *hash_pieces.get(0))
|
2013-12-14 20:58:07 -06:00
|
|
|
} else {
|
2014-02-28 14:54:01 -06:00
|
|
|
(*hash_pieces.get(0), *hash_pieces.get(1))
|
2013-12-14 20:58:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let name = if !hash_name.is_empty() {
|
2014-05-25 05:17:19 -05:00
|
|
|
hash_name.to_string()
|
2013-12-14 20:58:07 -06:00
|
|
|
} else {
|
2014-05-25 05:17:19 -05:00
|
|
|
inferred_name.to_string()
|
2013-12-14 20:58:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let version = if !hash_version.is_empty() {
|
2014-01-10 09:22:06 -06:00
|
|
|
if hash_version == "0.0" {
|
|
|
|
None
|
|
|
|
} else {
|
2014-05-25 05:17:19 -05:00
|
|
|
Some(hash_version.to_string())
|
2014-01-10 09:22:06 -06:00
|
|
|
}
|
2013-12-14 20:58:07 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
(name, version)
|
2013-12-09 15:56:53 -06:00
|
|
|
};
|
|
|
|
|
2013-12-28 11:16:48 -06:00
|
|
|
Some(CrateId {
|
2014-05-25 05:17:19 -05:00
|
|
|
path: path.to_string(),
|
2013-12-14 20:58:07 -06:00
|
|
|
name: name,
|
2013-12-09 15:56:53 -06:00
|
|
|
version: version,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 11:16:48 -06:00
|
|
|
impl CrateId {
|
2013-12-09 15:56:53 -06:00
|
|
|
pub fn version_or_default<'a>(&'a self) -> &'a str {
|
|
|
|
match self.version {
|
|
|
|
None => "0.0",
|
|
|
|
Some(ref version) => version.as_slice(),
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 09:22:06 -06:00
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn short_name_with_version(&self) -> String {
|
2014-06-26 01:15:14 -05:00
|
|
|
format!("{}-{}", self.name, self.version_or_default())
|
2014-01-10 09:22:06 -06:00
|
|
|
}
|
2014-02-24 20:13:51 -06:00
|
|
|
|
|
|
|
pub fn matches(&self, other: &CrateId) -> bool {
|
|
|
|
// FIXME: why does this not match on `path`?
|
|
|
|
if self.name != other.name { return false }
|
|
|
|
match (&self.version, &other.version) {
|
|
|
|
(&Some(ref v1), &Some(ref v2)) => v1 == v2,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bare_name() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("foo").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "foo".to_string());
|
2013-12-28 11:16:48 -06:00
|
|
|
assert_eq!(crateid.version, None);
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.path, "foo".to_string());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bare_name_single_char() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("f").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "f".to_string());
|
2013-12-28 11:16:48 -06:00
|
|
|
assert_eq!(crateid.version, None);
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.path, "f".to_string());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-12-28 11:16:48 -06:00
|
|
|
fn empty_crateid() {
|
|
|
|
let crateid: Option<CrateId> = from_str("");
|
|
|
|
assert!(crateid.is_none());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_path() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("example.com/foo/bar").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "bar".to_string());
|
2013-12-28 11:16:48 -06:00
|
|
|
assert_eq!(crateid.version, None);
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.path, "example.com/foo/bar".to_string());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_version() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("foo#1.0").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "foo".to_string());
|
|
|
|
assert_eq!(crateid.version, Some("1.0".to_string()));
|
|
|
|
assert_eq!(crateid.path, "foo".to_string());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn absolute_path() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: Option<CrateId> = from_str("/foo/bar");
|
|
|
|
assert!(crateid.is_none());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
2013-12-14 20:58:07 -06:00
|
|
|
#[test]
|
|
|
|
fn path_ends_with_slash() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: Option<CrateId> = from_str("foo/bar/");
|
|
|
|
assert!(crateid.is_none());
|
2013-12-14 20:58:07 -06:00
|
|
|
}
|
|
|
|
|
2013-12-09 15:56:53 -06:00
|
|
|
#[test]
|
|
|
|
fn path_and_version() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("example.com/foo/bar#1.0").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "bar".to_string());
|
|
|
|
assert_eq!(crateid.version, Some("1.0".to_string()));
|
|
|
|
assert_eq!(crateid.path, "example.com/foo/bar".to_string());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_chars() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("a/b#1").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "b".to_string());
|
|
|
|
assert_eq!(crateid.version, Some("1".to_string()));
|
|
|
|
assert_eq!(crateid.path, "a/b".to_string());
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_version() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("foo#").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "foo".to_string());
|
2013-12-28 11:16:48 -06:00
|
|
|
assert_eq!(crateid.version, None);
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.path, "foo".to_string());
|
2013-12-14 20:58:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_and_name() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("foo/rust-bar#bar:1.0").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "bar".to_string());
|
|
|
|
assert_eq!(crateid.version, Some("1.0".to_string()));
|
|
|
|
assert_eq!(crateid.path, "foo/rust-bar".to_string());
|
2013-12-14 20:58:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_name() {
|
2013-12-28 11:16:48 -06:00
|
|
|
let crateid: CrateId = from_str("foo/bar#:1.0").expect("valid crateid");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(crateid.name, "bar".to_string());
|
|
|
|
assert_eq!(crateid.version, Some("1.0".to_string()));
|
|
|
|
assert_eq!(crateid.path, "foo/bar".to_string());
|
2013-12-14 20:58:07 -06:00
|
|
|
}
|