Skip to content

Commit ebb488f

Browse files
committed
Switched to a slightly less disgusting JSON parsing approach.
1 parent beb3aa8 commit ebb488f

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ failure = "0.1"
3030
log = "0.4"
3131
rand = "0.6"
3232
semver = "0.9"
33+
serde = "1.0.84"
34+
serde_derive = "1.0.84"
3335
serde_json = "1.0.34"
3436

3537
[dev-dependencies]

src/bin/cargo_semver.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#![feature(set_stdio)]
44

55
extern crate getopts;
6+
extern crate serde;
7+
#[macro_use]
8+
extern crate serde_derive;
69
extern crate serde_json;
710

811
use cargo::core::{Package, PackageId, PackageSet, Source, SourceId, SourceMap, Workspace};
912
use log::debug;
10-
use serde_json::Value;
1113
use std::{
1214
env,
1315
io::BufReader,
@@ -19,6 +21,17 @@ use std::{
1921

2022
pub type Result<T> = cargo::util::CargoResult<T>;
2123

24+
#[derive(Debug, Deserialize)]
25+
struct Invocation {
26+
package_name: String,
27+
outputs: Vec<PathBuf>,
28+
}
29+
30+
#[derive(Debug, Deserialize)]
31+
struct BuildPlan {
32+
invocations: Vec<Invocation>,
33+
}
34+
2235
/// Main entry point.
2336
///
2437
/// Parse CLI arguments, handle their semantics, and provide for proper error handling.
@@ -394,25 +407,13 @@ impl<'a> WorkInfo<'a> {
394407
let compilation = cargo::ops::compile(&self.workspace, &opts)?;
395408
env::remove_var("RUSTFLAGS");
396409

397-
let build_plan: Value =
410+
let build_plan: BuildPlan =
398411
serde_json::from_reader(BufReader::new(File::open(&outdir)?))?;
399412

400-
// FIXME: yuck drilling
401-
if let Value::Object(m) = build_plan {
402-
if let Some(Value::Array(v)) = m.get("invocations") {
403-
for s in v {
404-
if let Value::Object(m2) = s {
405-
if let Some(Value::String(s2)) = m2.get("package_name") {
406-
if s2 != name { continue; }
407-
}
408-
409-
if let Some(Value::Array(v2)) = m2.get("outputs") {
410-
if let Some(Value::String(s)) = v2.get(0) {
411-
return Ok((PathBuf::from(s), compilation.deps_output));
412-
}
413-
}
414-
}
415-
}
413+
// TODO: handle multiple outputs gracefully
414+
for i in &build_plan.invocations {
415+
if i.package_name == name {
416+
return Ok((i.outputs[0].clone(), compilation.deps_output));
416417
}
417418
}
418419

0 commit comments

Comments
 (0)