Skip to content

Commit ef94f46

Browse files
committed
Create a centralized machine_message module
1 parent e4643b5 commit ef94f46

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_serialize::json;
1010
use core::{Package, PackageId, PackageSet, Target, Resolve};
1111
use core::{Profile, Profiles, Workspace};
1212
use core::shell::ColorConfig;
13-
use util::{self, CargoResult, human};
13+
use util::{self, CargoResult, human, machine_message};
1414
use util::{Config, internal, ChainError, profile, join_paths, short_hash};
1515

1616
use self::job::{Job, Work};
@@ -272,32 +272,23 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
272272
state.running(&rustc);
273273
let process_builder = rustc.into_process_builder();
274274
try!(if json_errors {
275-
#[derive(RustcEncodable)]
276-
struct Message<'a> {
277-
reason: &'a str,
278-
package_id: &'a PackageId,
279-
target: &'a Target,
280-
message: json::Json,
281-
}
282275
process_builder.exec_with_streaming(
283276
&mut |line| if !line.is_empty() {
284277
Err(internal(&format!("compiler stdout is not empty: `{}`", line)))
285278
} else {
286279
Ok(())
287280
},
288281
&mut |line| {
289-
let rustc_message = try!(json::Json::from_str(line).map_err(|_| {
282+
let compiler_message = try!(json::Json::from_str(line).map_err(|_| {
290283
internal(&format!("compiler produced invalid json: `{}`", line))
291284
}));
292285

293-
let message = Message {
294-
reason: "rustc-message",
295-
package_id: &package_id,
296-
target: &target,
297-
message: rustc_message,
298-
};
299-
let encoded = json::encode(&message).unwrap();
300-
println!("{}", encoded);
286+
machine_message::FromCompiler::new(
287+
&package_id,
288+
&target,
289+
compiler_message
290+
).emit();
291+
301292
Ok(())
302293
},
303294
).map(|_| ())

src/cargo/util/machine_message.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use rustc_serialize::json;
2+
use core::{PackageId, Target};
3+
4+
#[derive(RustcEncodable)]
5+
pub struct FromCompiler<'a> {
6+
reason: &'static str,
7+
package_id: &'a PackageId,
8+
target: &'a Target,
9+
message: json::Json,
10+
}
11+
12+
impl<'a> FromCompiler<'a> {
13+
pub fn new(package_id: &'a PackageId,
14+
target: &'a Target,
15+
message: json::Json)
16+
-> FromCompiler<'a> {
17+
FromCompiler {
18+
reason: "compiler-message",
19+
package_id: package_id,
20+
target: target,
21+
message: message,
22+
}
23+
}
24+
25+
pub fn emit(self) {
26+
let json = json::encode(&self).unwrap();
27+
println!("{}", json);
28+
}
29+
}
30+

src/cargo/util/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ pub mod errors;
2525
pub mod graph;
2626
pub mod hex;
2727
pub mod important_paths;
28+
pub mod job;
29+
pub mod lev_distance;
30+
pub mod machine_message;
31+
pub mod network;
2832
pub mod paths;
2933
pub mod process_builder;
3034
pub mod profile;
3135
pub mod to_semver;
3236
pub mod to_url;
3337
pub mod toml;
34-
pub mod lev_distance;
35-
pub mod job;
36-
pub mod network;
3738
mod cfg;
3839
mod dependency_queue;
3940
mod rustc;

tests/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,7 @@ fn compiler_json_error_format() {
23052305
.arg("--message-format").arg("json-v1"),
23062306
execs().with_json(r#"
23072307
{
2308-
"reason":"rustc-message",
2308+
"reason":"compiler-message",
23092309
"package_id":"bar 0.5.0 ([..])",
23102310
"target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"},
23112311
"message":{
@@ -2321,7 +2321,7 @@ fn compiler_json_error_format() {
23212321
}
23222322
23232323
{
2324-
"reason":"rustc-message",
2324+
"reason":"compiler-message",
23252325
"package_id":"foo 0.5.0 ([..])",
23262326
"target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"},
23272327
"message":{

0 commit comments

Comments
 (0)