Skip to content

Commit

Permalink
Decode directly into a semver::Version
Browse files Browse the repository at this point in the history
This generates errors for malformed semver versions during the decoding process
rather than later in the convertion to a package id. This also cuts down on the
large number of derived traits to only what's necessary.

Closes #54
  • Loading branch information
alexcrichton committed Aug 26, 2014
1 parent a40d1fd commit 2c3fa2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
44 changes: 30 additions & 14 deletions src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serialize::Decodable;
use std::collections::HashMap;
use std::fmt;
use std::io::fs;
use std::slice;
use std::str;
use toml;
use semver;
use serialize::{Decodable, Decoder};

use core::{SourceId, GitKind};
use core::manifest::{LibKind, Lib, Dylib, Profile};
Expand Down Expand Up @@ -168,14 +169,14 @@ type TomlBenchTarget = TomlTarget;
* TODO: Make all struct fields private
*/

#[deriving(Encodable,Decodable,PartialEq,Clone,Show)]
#[deriving(Decodable)]
pub enum TomlDependency {
SimpleDep(String),
DetailedDep(DetailedTomlDependency)
}


#[deriving(Encodable,Decodable,PartialEq,Clone,Show)]
#[deriving(Decodable)]
pub struct DetailedTomlDependency {
version: Option<String>,
path: Option<String>,
Expand All @@ -185,7 +186,7 @@ pub struct DetailedTomlDependency {
rev: Option<String>
}

#[deriving(Encodable,Decodable,PartialEq,Clone)]
#[deriving(Decodable)]
pub struct TomlManifest {
package: Option<Box<TomlProject>>,
project: Option<Box<TomlProject>>,
Expand All @@ -198,7 +199,7 @@ pub struct TomlManifest {
dev_dependencies: Option<HashMap<String, TomlDependency>>,
}

#[deriving(Encodable,Decodable,PartialEq,Clone)]
#[deriving(Decodable)]
pub enum ManyOrOne<T> {
Many(Vec<T>),
One(T),
Expand All @@ -213,25 +214,40 @@ impl<T> ManyOrOne<T> {
}
}

#[deriving(Decodable,Encodable,PartialEq,Clone,Show)]
#[deriving(Decodable)]
pub struct TomlProject {
pub name: String,
// FIXME #54: should be a Version to be able to be Decodable'd directly.
pub version: String,
name: String,
version: TomlVersion,
pub authors: Vec<String>,
build: Option<TomlBuildCommandsList>,
exclude: Option<Vec<String>>,
}

#[deriving(Encodable,Decodable,PartialEq,Clone,Show)]
#[deriving(Decodable)]
pub enum TomlBuildCommandsList {
SingleBuildCommand(String),
MultipleBuildCommands(Vec<String>)
}

pub struct TomlVersion {
version: semver::Version,
}

impl<E, D: Decoder<E>> Decodable<D, E> for TomlVersion {
fn decode(d: &mut D) -> Result<TomlVersion, E> {
let s = raw_try!(d.read_str());
match semver::parse(s.as_slice()) {
Some(s) => Ok(TomlVersion { version: s }),
None => Err(d.error(format!("cannot parse '{}' as a semver",
s).as_slice())),
}
}
}

impl TomlProject {
pub fn to_package_id(&self, source_id: &SourceId) -> CargoResult<PackageId> {
PackageId::new(self.name.as_slice(), self.version.as_slice(), source_id)
PackageId::new(self.name.as_slice(), self.version.version.clone(),
source_id)
}
}

Expand Down Expand Up @@ -395,7 +411,7 @@ impl TomlManifest {
&metadata);

if targets.is_empty() {
debug!("manifest has no build targets; project={}", self.project);
debug!("manifest has no build targets");
}

let mut deps = Vec::new();
Expand Down Expand Up @@ -490,7 +506,7 @@ fn process_dependencies<'a>(cx: &mut Context<'a>, dev: bool,
Ok(())
}

#[deriving(Decodable,Encodable,PartialEq,Clone,Show)]
#[deriving(Decodable, Show, Clone)]
struct TomlTarget {
name: String,
crate_type: Option<Vec<String>>,
Expand All @@ -502,7 +518,7 @@ struct TomlTarget {
plugin: Option<bool>,
}

#[deriving(Decodable,Encodable,PartialEq,Clone)]
#[deriving(Decodable, Clone)]
enum TomlPath {
TomlString(String),
TomlPath(Path),
Expand Down
3 changes: 2 additions & 1 deletion tests/test_cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ test!(cargo_compile_with_invalid_version {
execs()
.with_status(101)
.with_stderr("Cargo.toml is not a valid manifest\n\n\
invalid version: cannot parse '1.0' as a semver\n"))
cannot parse '1.0' as a semver for the key \
`project.version`\n"))

})

Expand Down

9 comments on commit 2c3fa2d

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@2c3fa2d

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/cargo/issue-54 = 2c3fa2d into auto-cargo

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/cargo/issue-54 = 2c3fa2d merged ok, testing candidate = c6bcf215

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@2c3fa2d

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/cargo/issue-54 = 2c3fa2d into auto-cargo

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/cargo/issue-54 = 2c3fa2d merged ok, testing candidate = caca3af

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 2c3fa2d Aug 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto-cargo = caca3af

Please sign in to comment.