Skip to content

Commit caca3af

Browse files
committed
auto merge of #444 : alexcrichton/cargo/issue-54, r=brson
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
2 parents 0eb5bf9 + 2c3fa2d commit caca3af

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/cargo/util/toml.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use serialize::Decodable;
21
use std::collections::HashMap;
32
use std::fmt;
43
use std::io::fs;
54
use std::slice;
65
use std::str;
76
use toml;
7+
use semver;
8+
use serialize::{Decodable, Decoder};
89

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

171-
#[deriving(Encodable,Decodable,PartialEq,Clone,Show)]
172+
#[deriving(Decodable)]
172173
pub enum TomlDependency {
173174
SimpleDep(String),
174175
DetailedDep(DetailedTomlDependency)
175176
}
176177

177178

178-
#[deriving(Encodable,Decodable,PartialEq,Clone,Show)]
179+
#[deriving(Decodable)]
179180
pub struct DetailedTomlDependency {
180181
version: Option<String>,
181182
path: Option<String>,
@@ -185,7 +186,7 @@ pub struct DetailedTomlDependency {
185186
rev: Option<String>
186187
}
187188

188-
#[deriving(Encodable,Decodable,PartialEq,Clone)]
189+
#[deriving(Decodable)]
189190
pub struct TomlManifest {
190191
package: Option<Box<TomlProject>>,
191192
project: Option<Box<TomlProject>>,
@@ -198,7 +199,7 @@ pub struct TomlManifest {
198199
dev_dependencies: Option<HashMap<String, TomlDependency>>,
199200
}
200201

201-
#[deriving(Encodable,Decodable,PartialEq,Clone)]
202+
#[deriving(Decodable)]
202203
pub enum ManyOrOne<T> {
203204
Many(Vec<T>),
204205
One(T),
@@ -213,25 +214,40 @@ impl<T> ManyOrOne<T> {
213214
}
214215
}
215216

216-
#[deriving(Decodable,Encodable,PartialEq,Clone,Show)]
217+
#[deriving(Decodable)]
217218
pub struct TomlProject {
218-
pub name: String,
219-
// FIXME #54: should be a Version to be able to be Decodable'd directly.
220-
pub version: String,
219+
name: String,
220+
version: TomlVersion,
221221
pub authors: Vec<String>,
222222
build: Option<TomlBuildCommandsList>,
223223
exclude: Option<Vec<String>>,
224224
}
225225

226-
#[deriving(Encodable,Decodable,PartialEq,Clone,Show)]
226+
#[deriving(Decodable)]
227227
pub enum TomlBuildCommandsList {
228228
SingleBuildCommand(String),
229229
MultipleBuildCommands(Vec<String>)
230230
}
231231

232+
pub struct TomlVersion {
233+
version: semver::Version,
234+
}
235+
236+
impl<E, D: Decoder<E>> Decodable<D, E> for TomlVersion {
237+
fn decode(d: &mut D) -> Result<TomlVersion, E> {
238+
let s = raw_try!(d.read_str());
239+
match semver::parse(s.as_slice()) {
240+
Some(s) => Ok(TomlVersion { version: s }),
241+
None => Err(d.error(format!("cannot parse '{}' as a semver",
242+
s).as_slice())),
243+
}
244+
}
245+
}
246+
232247
impl TomlProject {
233248
pub fn to_package_id(&self, source_id: &SourceId) -> CargoResult<PackageId> {
234-
PackageId::new(self.name.as_slice(), self.version.as_slice(), source_id)
249+
PackageId::new(self.name.as_slice(), self.version.version.clone(),
250+
source_id)
235251
}
236252
}
237253

@@ -395,7 +411,7 @@ impl TomlManifest {
395411
&metadata);
396412

397413
if targets.is_empty() {
398-
debug!("manifest has no build targets; project={}", self.project);
414+
debug!("manifest has no build targets");
399415
}
400416

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

493-
#[deriving(Decodable,Encodable,PartialEq,Clone,Show)]
509+
#[deriving(Decodable, Show, Clone)]
494510
struct TomlTarget {
495511
name: String,
496512
crate_type: Option<Vec<String>>,
@@ -503,7 +519,7 @@ struct TomlTarget {
503519
harness: Option<bool>,
504520
}
505521

506-
#[deriving(Decodable,Encodable,PartialEq,Clone)]
522+
#[deriving(Decodable, Clone)]
507523
enum TomlPath {
508524
TomlString(String),
509525
TomlPath(Path),

tests/test_cargo_compile.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ test!(cargo_compile_with_invalid_version {
7676
execs()
7777
.with_status(101)
7878
.with_stderr("Cargo.toml is not a valid manifest\n\n\
79-
invalid version: cannot parse '1.0' as a semver\n"))
79+
cannot parse '1.0' as a semver for the key \
80+
`project.version`\n"))
8081

8182
})
8283

0 commit comments

Comments
 (0)