Refactor PackageId.namespace -> source_id

This commit is contained in:
Yehuda Katz + Carl Lerche 2014-07-08 14:58:56 -07:00 committed by Tim Carey-Smith
parent 3dcb3376fc
commit 59bc9adb29
6 changed files with 47 additions and 32 deletions

View File

@ -1,4 +1,4 @@
#![crate_id="cargo-clean"]
#![crate_name="cargo-clean"]
#![feature(phase)]
extern crate cargo;

View File

@ -10,7 +10,7 @@ use serialize::{
};
use util::{CargoResult, CargoError};
use core::source::Location;
use core::source::SourceId;
trait ToVersion {
fn to_version(self) -> Result<semver::Version, String>;
@ -57,7 +57,7 @@ impl<'a> ToUrl for &'a Url {
pub struct PackageId {
name: String,
version: semver::Version,
namespace: Location,
source_id: SourceId,
}
#[deriving(Clone, Show, PartialEq)]
@ -78,12 +78,12 @@ impl CargoError for PackageIdError {
impl PackageId {
pub fn new<T: ToVersion>(name: &str, version: T,
ns: &Location) -> CargoResult<PackageId> {
sid: &SourceId) -> CargoResult<PackageId> {
let v = try!(version.to_version().map_err(InvalidVersion));
Ok(PackageId {
name: name.to_str(),
version: v,
namespace: ns.clone()
source_id: sid.clone()
})
}
@ -95,8 +95,8 @@ impl PackageId {
&self.version
}
pub fn get_namespace<'a>(&'a self) -> &'a Location {
&self.namespace
pub fn get_source_id<'a>(&'a self) -> &'a SourceId {
&self.source_id
}
}
@ -106,8 +106,8 @@ impl Show for PackageId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
try!(write!(f, "{} v{}", self.name, self.version));
if self.namespace.to_str().as_slice() != central_repo {
try!(write!(f, " ({})", self.namespace));
if self.source_id.to_str().as_slice() != central_repo {
try!(write!(f, " ({})", self.source_id));
}
Ok(())
@ -118,31 +118,29 @@ impl<D: Decoder<Box<CargoError + Send>>>
Decodable<D,Box<CargoError + Send>>
for PackageId
{
fn decode(d: &mut D) -> Result<PackageId, Box<CargoError + Send>> {
let vector: Vec<String> = try!(Decodable::decode(d));
fn decode(d: &mut D) -> CargoResult<PackageId> {
let (name, version, source_id): (String, String, SourceId) = try!(Decodable::decode(d));
PackageId::new(
vector.get(0).as_slice(),
vector.get(1).as_slice(),
&try!(Location::parse(vector.get(2).as_slice())))
PackageId::new(name.as_slice(), version.as_slice(), &source_id)
}
}
impl<E, S: Encoder<E>> Encodable<S,E> for PackageId {
fn encode(&self, e: &mut S) -> Result<(), E> {
(vec!(self.name.clone(), self.version.to_str()),
self.namespace.to_str()).encode(e)
(self.name.clone(), self.version.to_str(), self.source_id.clone()).encode(e)
}
}
#[cfg(test)]
mod tests {
use super::{PackageId, central_repo};
use core::source::Location;
use core::source::{Location, RegistryKind, SourceId};
#[test]
fn invalid_version_handled_nicely() {
let repo = Location::parse(central_repo).unwrap();
let loc = Location::parse(central_repo).unwrap();
let repo = SourceId::new(RegistryKind, loc);
assert!(PackageId::new("foo", "1.0", &repo).is_err());
assert!(PackageId::new("foo", "1", &repo).is_err());
assert!(PackageId::new("foo", "bar", &repo).is_err());

View File

@ -4,7 +4,7 @@ use core::{
Dependency,
PackageId,
Summary,
Registry
Registry,
};
use util::{CargoResult, human, internal};
@ -96,8 +96,9 @@ mod test {
)
)
fn registry_loc() -> Location {
Location::parse("http://www.example.com/").unwrap()
fn registry_loc() -> SourceId {
let remote = Location::parse("http://www.example.com/").unwrap();
SourceId::new(RegistryKind, remote)
}
fn pkg(name: &str) -> Summary {

View File

@ -1,12 +1,13 @@
use std::fmt;
use std::fmt::{Show, Formatter};
use serialize::{Decodable, Decoder, Encodable, Encoder};
use url::Url;
use core::{Summary, Package, PackageId};
use sources::{PathSource, GitSource};
use sources::git;
use util::{Config, CargoResult};
use util::{Config, CargoResult, CargoError};
use util::errors::human;
/// A Source finds and downloads remote packages based on names and
@ -45,7 +46,7 @@ pub trait Source {
fn fingerprint(&self, pkg: &Package) -> CargoResult<String>;
}
#[deriving(Show, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[deriving(Encodable, Decodable, Show, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SourceKind {
/// GitKind(<git reference>) represents a git repository
GitKind(String),
@ -61,7 +62,22 @@ pub enum Location {
Remote(Url),
}
#[deriving(Clone, Eq)]
type Error = Box<CargoError + Send>;
impl<E, D: Decoder<E>> Decodable<D, E> for Location {
fn decode(d: &mut D) -> Result<Location, E> {
let url: String = raw_try!(Decodable::decode(d));
Ok(Location::parse(url.as_slice()).unwrap())
}
}
impl<E, S: Encoder<E>> Encodable<S, E> for Location {
fn encode(&self, e: &mut S) -> Result<(), E> {
self.to_str().encode(e)
}
}
#[deriving(Encodable, Decodable, Clone, Eq)]
pub struct SourceId {
pub kind: SourceKind,
pub location: Location,
@ -97,7 +113,7 @@ impl Show for SourceId {
SourceId { kind: GitKind(ref reference), ref location } => {
try!(write!(f, "{}", location));
if reference.as_slice() != "master" {
try!(write!(f, " (ref={})", reference));
try!(write!(f, "#ref={}", reference));
}
},
SourceId { kind: RegistryKind, .. } => {

View File

@ -39,7 +39,7 @@ pub fn project_layout(root: &Path) -> Layout {
bins.push(root.join("src/main.rs"));
}
fs::readdir(&root.join("src/bin"))
let _ = fs::readdir(&root.join("src/bin"))
.map(|v| v.move_iter())
.map(|i| i.filter(|f| f.extension_str() == Some("rs")))
.map(|mut i| i.collect())
@ -176,8 +176,8 @@ pub enum TomlBuildCommandsList {
}
impl TomlProject {
pub fn to_package_id(&self, namespace: &Location) -> CargoResult<PackageId> {
PackageId::new(self.name.as_slice(), self.version.as_slice(), namespace)
pub fn to_package_id(&self, source_id: &SourceId) -> CargoResult<PackageId> {
PackageId::new(self.name.as_slice(), self.version.as_slice(), source_id)
}
}
@ -301,7 +301,7 @@ impl TomlManifest {
try!(process_dependencies(&mut cx, true, self.dev_dependencies.as_ref()));
}
let pkgid = try!(project.to_package_id(source_id.get_location()));
let pkgid = try!(project.to_package_id(source_id));
let summary = Summary::new(&pkgid, deps.as_slice());
Ok((Manifest::new(
&summary,

View File

@ -149,7 +149,7 @@ test!(cargo_compile_git_dep_branch {
assert_that(project.cargo_process("cargo-build"),
execs()
.with_stdout(format!("{} git repository `file:{}`\n\
{} dep1 v0.5.0 (file:{})\n\
{} dep1 v0.5.0 (file:{}#ref=branchy)\n\
{} foo v0.5.0 (file:{})\n",
UPDATING, git_root.display(),
COMPILING, git_root.display(),
@ -214,7 +214,7 @@ test!(cargo_compile_git_dep_tag {
assert_that(project.cargo_process("cargo-build"),
execs()
.with_stdout(format!("{} git repository `file:{}`\n\
{} dep1 v0.5.0 (file:{})\n\
{} dep1 v0.5.0 (file:{}#ref=v0.1.0)\n\
{} foo v0.5.0 (file:{})\n",
UPDATING, git_root.display(),
COMPILING, git_root.display(),