2016-05-27 08:31:19 -05:00
|
|
|
use std::collections::HashMap;
|
2016-05-30 05:47:04 -05:00
|
|
|
use std::process::Command;
|
|
|
|
use std::str::{from_utf8, Utf8Error};
|
|
|
|
use std::io;
|
|
|
|
use rustc_serialize::json;
|
2016-05-27 08:31:19 -05:00
|
|
|
|
|
|
|
#[derive(RustcDecodable, Debug)]
|
|
|
|
pub struct Metadata {
|
|
|
|
pub packages: Vec<Package>,
|
|
|
|
resolve: Option<()>,
|
|
|
|
pub version: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcDecodable, Debug)]
|
|
|
|
pub struct Package {
|
2016-05-30 05:47:04 -05:00
|
|
|
pub name: String,
|
|
|
|
pub version: String,
|
2016-05-27 08:31:19 -05:00
|
|
|
id: String,
|
2016-06-02 10:29:25 -05:00
|
|
|
source: Option<String>,
|
2016-05-30 05:47:04 -05:00
|
|
|
pub dependencies: Vec<Dependency>,
|
2016-05-27 08:31:19 -05:00
|
|
|
pub targets: Vec<Target>,
|
|
|
|
features: HashMap<String, Vec<String>>,
|
|
|
|
manifest_path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcDecodable, Debug)]
|
|
|
|
pub struct Dependency {
|
2016-05-30 05:47:04 -05:00
|
|
|
pub name: String,
|
2016-05-27 08:31:19 -05:00
|
|
|
source: Option<String>,
|
2016-05-30 05:47:04 -05:00
|
|
|
pub req: String,
|
2016-05-27 08:31:19 -05:00
|
|
|
kind: Option<String>,
|
|
|
|
optional: bool,
|
|
|
|
uses_default_features: bool,
|
2016-06-02 10:29:25 -05:00
|
|
|
features: Vec<String>,
|
2016-06-06 04:03:15 -05:00
|
|
|
target: Option<String>,
|
2016-05-27 08:31:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcDecodable, Debug)]
|
|
|
|
pub struct Target {
|
|
|
|
pub name: String,
|
2016-06-02 10:29:25 -05:00
|
|
|
pub kind: Vec<String>,
|
2016-05-27 08:31:19 -05:00
|
|
|
src_path: String,
|
|
|
|
}
|
2016-05-30 05:47:04 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Io(io::Error),
|
|
|
|
Utf8(Utf8Error),
|
|
|
|
Json(json::DecoderError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
2016-06-05 18:42:39 -05:00
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
Error::Io(err)
|
|
|
|
}
|
2016-05-30 05:47:04 -05:00
|
|
|
}
|
|
|
|
impl From<Utf8Error> for Error {
|
2016-06-05 18:42:39 -05:00
|
|
|
fn from(err: Utf8Error) -> Self {
|
|
|
|
Error::Utf8(err)
|
|
|
|
}
|
2016-05-30 05:47:04 -05:00
|
|
|
}
|
|
|
|
impl From<json::DecoderError> for Error {
|
2016-06-05 18:42:39 -05:00
|
|
|
fn from(err: json::DecoderError) -> Self {
|
|
|
|
Error::Json(err)
|
|
|
|
}
|
2016-05-30 05:47:04 -05:00
|
|
|
}
|
|
|
|
|
2016-06-21 05:31:30 -05:00
|
|
|
pub fn metadata(manifest_path: Option<String>) -> Result<Metadata, Error> {
|
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
cmd.arg("metadata").arg("--no-deps");
|
|
|
|
if let Some(ref mani) = manifest_path {
|
|
|
|
cmd.arg(mani);
|
|
|
|
}
|
|
|
|
let output = cmd.output()?;
|
2016-05-30 05:47:04 -05:00
|
|
|
let stdout = from_utf8(&output.stdout)?;
|
|
|
|
Ok(json::decode(stdout)?)
|
|
|
|
}
|