Skip to content

Commit 3775b3f

Browse files
committed
Auto merge of #2219 - matklad:encodable-audit, r=alexcrichton
@alexcrichton another preparation PR for #2196 I've removed obscure `metadata` field from `Target`. It is a breaking change (for read-manifest), but the field seemed cryptic, useless and untested :) `Target` has a bunch of boolean fields: ``` tested: bool, benched: bool, doc: bool, doctest: bool, harness: bool, // whether to use the test harness (--test) for_host: bool, ``` I guess they should not be included in serialized representation? I will push commits for other `Encodable`s here.
2 parents 2cdeb07 + 77e7273 commit 3775b3f

File tree

4 files changed

+86
-69
lines changed

4 files changed

+86
-69
lines changed

src/cargo/core/dependency.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@ pub struct Dependency {
3333

3434

3535
#[derive(RustcEncodable)]
36-
struct SerializedDependency {
37-
name: String,
38-
req: String
36+
struct SerializedDependency<'a> {
37+
name: &'a str,
38+
source: &'a SourceId,
39+
req: String,
40+
kind: Kind,
41+
42+
optional: bool,
43+
uses_default_features: bool,
44+
features: &'a [String],
45+
target: &'a Option<&'a str>,
3946
}
4047

4148
impl Encodable for Dependency {
4249
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
4350
SerializedDependency {
44-
name: self.name().to_string(),
45-
req: self.version_req().to_string()
51+
name: self.name(),
52+
source: &self.source_id(),
53+
req: self.version_req().to_string(),
54+
kind: self.kind(),
55+
optional: self.is_optional(),
56+
uses_default_features: self.uses_default_features(),
57+
features: self.features(),
58+
target: &self.only_for_platform(),
4659
}.encode(s)
4760
}
4861
}
@@ -54,6 +67,16 @@ pub enum Kind {
5467
Build,
5568
}
5669

70+
impl Encodable for Kind {
71+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
72+
match *self {
73+
Kind::Normal => None,
74+
Kind::Development => Some("dev"),
75+
Kind::Build => Some("build"),
76+
}.encode(s)
77+
}
78+
}
79+
5780
impl DependencyInner {
5881
/// Attempt to create a `Dependency` from an entry in the manifest.
5982
pub fn parse(name: &str,
@@ -235,4 +258,3 @@ impl Dependency {
235258
self.inner.matches_id(id)
236259
}
237260
}
238-

src/cargo/core/manifest.rs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,6 @@ pub struct ManifestMetadata {
4343
pub documentation: Option<String>, // url
4444
}
4545

46-
#[derive(RustcEncodable)]
47-
struct SerializedManifest<'a> {
48-
name: String,
49-
version: String,
50-
dependencies: &'a [Dependency],
51-
targets: Vec<Target>,
52-
}
53-
54-
impl Encodable for Manifest {
55-
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
56-
SerializedManifest {
57-
name: self.summary.name().to_string(),
58-
version: self.summary.version().to_string(),
59-
dependencies: self.summary.dependencies(),
60-
targets: self.targets.clone(),
61-
}.encode(s)
62-
}
63-
}
64-
6546
#[derive(Debug, Clone, PartialEq, Eq, Hash, RustcEncodable, Copy)]
6647
pub enum LibKind {
6748
Lib,
@@ -93,7 +74,7 @@ impl LibKind {
9374
}
9475
}
9576

96-
#[derive(Debug, Clone, Hash, PartialEq, RustcEncodable, Eq)]
77+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
9778
pub enum TargetKind {
9879
Lib(Vec<LibKind>),
9980
Bin,
@@ -103,6 +84,21 @@ pub enum TargetKind {
10384
CustomBuild,
10485
}
10586

87+
impl Encodable for TargetKind {
88+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
89+
match *self {
90+
TargetKind::Lib(ref kinds) => {
91+
kinds.iter().map(|k| k.crate_type()).collect()
92+
}
93+
TargetKind::Bin => vec!["bin"],
94+
TargetKind::Example => vec!["example"],
95+
TargetKind::Test => vec!["test"],
96+
TargetKind::CustomBuild => vec!["custom-build"],
97+
TargetKind::Bench => vec!["bench"],
98+
}.encode(s)
99+
}
100+
}
101+
106102
#[derive(RustcEncodable, RustcDecodable, Clone, PartialEq, Eq, Debug, Hash)]
107103
pub struct Profile {
108104
pub opt_level: u32,
@@ -145,31 +141,18 @@ pub struct Target {
145141
}
146142

147143
#[derive(RustcEncodable)]
148-
pub struct SerializedTarget {
149-
kind: Vec<&'static str>,
150-
name: String,
151-
src_path: String,
152-
metadata: Option<Metadata>
144+
struct SerializedTarget<'a> {
145+
kind: &'a TargetKind,
146+
name: &'a str,
147+
src_path: &'a str,
153148
}
154149

155150
impl Encodable for Target {
156151
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
157-
let kind = match self.kind {
158-
TargetKind::Lib(ref kinds) => {
159-
kinds.iter().map(|k| k.crate_type()).collect()
160-
}
161-
TargetKind::Bin => vec!["bin"],
162-
TargetKind::Example => vec!["example"],
163-
TargetKind::Test => vec!["test"],
164-
TargetKind::CustomBuild => vec!["custom-build"],
165-
TargetKind::Bench => vec!["bench"],
166-
};
167-
168152
SerializedTarget {
169-
kind: kind,
170-
name: self.name.clone(),
171-
src_path: self.src_path.display().to_string(),
172-
metadata: self.metadata.clone()
153+
kind: &self.kind,
154+
name: &self.name,
155+
src_path: &self.src_path.display().to_string(),
173156
}.encode(s)
174157
}
175158
}

src/cargo/core/package.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::fmt::{self, Formatter};
23
use std::hash;
34
use std::slice;
@@ -24,25 +25,30 @@ pub struct Package {
2425

2526
#[derive(RustcEncodable)]
2627
struct SerializedPackage<'a> {
27-
name: String,
28-
version: String,
28+
name: &'a str,
29+
version: &'a str,
30+
id: &'a PackageId,
31+
source: &'a SourceId,
2932
dependencies: &'a [Dependency],
30-
targets: Vec<Target>,
31-
manifest_path: String,
33+
targets: &'a [Target],
34+
features: &'a HashMap<String, Vec<String>>,
35+
manifest_path: &'a str,
3236
}
3337

3438
impl Encodable for Package {
3539
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
36-
let manifest = self.manifest();
37-
let summary = manifest.summary();
40+
let summary = self.manifest.summary();
3841
let package_id = summary.package_id();
3942

4043
SerializedPackage {
41-
name: package_id.name().to_string(),
42-
version: package_id.version().to_string(),
44+
name: &package_id.name(),
45+
version: &package_id.version().to_string(),
46+
id: package_id,
47+
source: summary.source_id(),
4348
dependencies: summary.dependencies(),
44-
targets: manifest.targets().to_vec(),
45-
manifest_path: self.manifest_path.display().to_string()
49+
targets: &self.manifest.targets(),
50+
features: summary.features(),
51+
manifest_path: &self.manifest_path.display().to_string(),
4652
}.encode(s)
4753
}
4854
}

tests/test_cargo_read_manifest.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@ use hamcrest::{assert_that};
33

44
fn setup() {}
55

6+
fn remove_all_whitespace(s: &str) -> String {
7+
s.split_whitespace().collect()
8+
}
9+
610
fn read_manifest_output() -> String {
7-
"\
8-
{\
9-
\"name\":\"foo\",\
10-
\"version\":\"0.5.0\",\
11-
\"dependencies\":[],\
12-
\"targets\":[{\
13-
\"kind\":[\"bin\"],\
14-
\"name\":\"foo\",\
15-
\"src_path\":\"src[..]foo.rs\",\
16-
\"metadata\":null\
17-
}],\
18-
\"manifest_path\":\"[..]Cargo.toml\"\
19-
}".into()
11+
remove_all_whitespace(r#"
12+
{
13+
"name":"foo",
14+
"version":"0.5.0",
15+
"id":"foo[..]0.5.0[..](path+file://[..]/foo)",
16+
"source":null,
17+
"dependencies":[],
18+
"targets":[{
19+
"kind":["bin"],
20+
"name":"foo",
21+
"src_path":"src[..]foo.rs"
22+
}],
23+
"features":{},
24+
"manifest_path":"[..]Cargo.toml"
25+
}"#)
2026
}
2127

2228
test!(cargo_read_manifest_path_to_cargo_toml_relative {

0 commit comments

Comments
 (0)