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)] #![feature(phase)]
extern crate cargo; extern crate cargo;

View File

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

View File

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

View File

@ -1,12 +1,13 @@
use std::fmt; use std::fmt;
use std::fmt::{Show, Formatter}; use std::fmt::{Show, Formatter};
use serialize::{Decodable, Decoder, Encodable, Encoder};
use url::Url; use url::Url;
use core::{Summary, Package, PackageId}; use core::{Summary, Package, PackageId};
use sources::{PathSource, GitSource}; use sources::{PathSource, GitSource};
use sources::git; use sources::git;
use util::{Config, CargoResult}; use util::{Config, CargoResult, CargoError};
use util::errors::human; use util::errors::human;
/// A Source finds and downloads remote packages based on names and /// 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>; 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 { pub enum SourceKind {
/// GitKind(<git reference>) represents a git repository /// GitKind(<git reference>) represents a git repository
GitKind(String), GitKind(String),
@ -61,7 +62,22 @@ pub enum Location {
Remote(Url), 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 struct SourceId {
pub kind: SourceKind, pub kind: SourceKind,
pub location: Location, pub location: Location,
@ -97,7 +113,7 @@ impl Show for SourceId {
SourceId { kind: GitKind(ref reference), ref location } => { SourceId { kind: GitKind(ref reference), ref location } => {
try!(write!(f, "{}", location)); try!(write!(f, "{}", location));
if reference.as_slice() != "master" { if reference.as_slice() != "master" {
try!(write!(f, " (ref={})", reference)); try!(write!(f, "#ref={}", reference));
} }
}, },
SourceId { kind: RegistryKind, .. } => { SourceId { kind: RegistryKind, .. } => {

View File

@ -39,7 +39,7 @@ pub fn project_layout(root: &Path) -> Layout {
bins.push(root.join("src/main.rs")); 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(|v| v.move_iter())
.map(|i| i.filter(|f| f.extension_str() == Some("rs"))) .map(|i| i.filter(|f| f.extension_str() == Some("rs")))
.map(|mut i| i.collect()) .map(|mut i| i.collect())
@ -176,8 +176,8 @@ pub enum TomlBuildCommandsList {
} }
impl TomlProject { impl TomlProject {
pub fn to_package_id(&self, namespace: &Location) -> CargoResult<PackageId> { pub fn to_package_id(&self, source_id: &SourceId) -> CargoResult<PackageId> {
PackageId::new(self.name.as_slice(), self.version.as_slice(), namespace) 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())); 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()); let summary = Summary::new(&pkgid, deps.as_slice());
Ok((Manifest::new( Ok((Manifest::new(
&summary, &summary,

View File

@ -149,7 +149,7 @@ test!(cargo_compile_git_dep_branch {
assert_that(project.cargo_process("cargo-build"), assert_that(project.cargo_process("cargo-build"),
execs() execs()
.with_stdout(format!("{} git repository `file:{}`\n\ .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", {} foo v0.5.0 (file:{})\n",
UPDATING, git_root.display(), UPDATING, git_root.display(),
COMPILING, git_root.display(), COMPILING, git_root.display(),
@ -214,7 +214,7 @@ test!(cargo_compile_git_dep_tag {
assert_that(project.cargo_process("cargo-build"), assert_that(project.cargo_process("cargo-build"),
execs() execs()
.with_stdout(format!("{} git repository `file:{}`\n\ .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", {} foo v0.5.0 (file:{})\n",
UPDATING, git_root.display(), UPDATING, git_root.display(),
COMPILING, git_root.display(), COMPILING, git_root.display(),