Skip to content

Commit

Permalink
Auto merge of #3668 - KalitaAlexey:fix-example-metadata, r=alexcrichton
Browse files Browse the repository at this point in the history
Reverted the "kind" field to the previous value.

Added a new field named "crate_types".
Fixes #3654
After this PR `cargo metadata` would output:
```json
{
    "packages": [
        {
            "targets": [
                {
                    "kind": ["lib"],
                    "crate_types": ["lib"]
                },
                {
                    "kind": ["example"],
                    "crate_types": ["staticlib"]
                }
            ]
        }
    ]
}
```

I have added tests.
  • Loading branch information
bors committed Feb 11, 2017
2 parents 1b39da7 + 582bc55 commit 6f1b860
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 19 deletions.
41 changes: 29 additions & 12 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,34 @@ pub enum TargetKind {
CustomBuild,
}

impl Encodable for TargetKind {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
impl TargetKind {
/// Returns a vector of crate types as specified in a manifest with one difference.
/// For ExampleLib it returns "example" instead of crate types
pub fn kinds(&self) -> Vec<&str> {
use self::TargetKind::*;
match *self {
TargetKind::Lib(ref kinds) |
TargetKind::ExampleLib(ref kinds) => {
Lib(ref kinds) => kinds.iter().map(LibKind::crate_type).collect(),
Bin => vec!["bin"],
ExampleBin | ExampleLib(_) => vec!["example"],
Test => vec!["test"],
CustomBuild => vec!["custom-build"],
Bench => vec!["bench"]
}
}

/// Returns a vector of crate types as specified in a manifest
pub fn crate_types(&self) -> Vec<&str> {
use self::TargetKind::*;
match *self {
Lib(ref kinds) | ExampleLib(ref kinds) => {
kinds.iter().map(LibKind::crate_type).collect()
}
TargetKind::Bin => vec!["bin"],
TargetKind::ExampleBin => vec!["example"],
TargetKind::Test => vec!["test"],
TargetKind::CustomBuild => vec!["custom-build"],
TargetKind::Bench => vec!["bench"],
}.encode(s)
Bin => vec!["bin"],
ExampleBin => vec!["example"],
Test => vec!["test"],
CustomBuild => vec!["custom-build"],
Bench => vec!["bench"]
}
}
}

Expand Down Expand Up @@ -196,15 +211,17 @@ pub struct Target {

#[derive(RustcEncodable)]
struct SerializedTarget<'a> {
kind: &'a TargetKind,
kind: Vec<&'a str>,
crate_types: Vec<&'a str>,
name: &'a str,
src_path: &'a str,
}

impl Encodable for Target {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
SerializedTarget {
kind: &self.kind,
kind: self.kind.kinds(),
crate_types: self.kind.crate_types(),
name: &self.name,
src_path: &self.src_path.display().to_string(),
}.encode(s)
Expand Down
42 changes: 36 additions & 6 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,12 @@ fn compiler_json_error_format() {
{
"reason":"compiler-message",
"package_id":"bar 0.5.0 ([..])",
"target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"},
"target":{
"kind":["lib"],
"crate_types":["lib"],
"name":"bar",
"src_path":"[..]lib.rs"
},
"message":"{...}"
}
Expand All @@ -2524,21 +2529,36 @@ fn compiler_json_error_format() {
},
"features": [],
"package_id":"bar 0.5.0 ([..])",
"target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"},
"target":{
"kind":["lib"],
"crate_types":["lib"],
"name":"bar",
"src_path":"[..]lib.rs"
},
"filenames":["[..].rlib"]
}
{
"reason":"compiler-message",
"package_id":"foo 0.5.0 ([..])",
"target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"},
"target":{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..]main.rs"
},
"message":"{...}"
}
{
"reason":"compiler-artifact",
"package_id":"foo 0.5.0 ([..])",
"target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"},
"target":{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..]main.rs"
},
"profile": {
"debug_assertions": true,
"debuginfo": 2,
Expand Down Expand Up @@ -2580,14 +2600,24 @@ fn message_format_json_forward_stderr() {
{
"reason":"compiler-message",
"package_id":"foo 0.5.0 ([..])",
"target":{"kind":["bin"],"name":"foo","src_path":"[..]"},
"target":{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..]"
},
"message":"{...}"
}
{
"reason":"compiler-artifact",
"package_id":"foo 0.5.0 ([..])",
"target":{"kind":["bin"],"name":"foo","src_path":"[..]"},
"target":{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..]"
},
"profile":{
"debug_assertions":true,
"debuginfo":2,
Expand Down
Loading

0 comments on commit 6f1b860

Please sign in to comment.