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,
|
|
|
|
source: Option<()>,
|
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,
|
|
|
|
features: Vec<HashMap<String, String>>,
|
|
|
|
target: Option<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(RustcDecodable, Debug)]
|
|
|
|
pub enum Kind {
|
|
|
|
dylib,
|
|
|
|
test,
|
|
|
|
bin,
|
|
|
|
lib,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcDecodable, Debug)]
|
|
|
|
pub struct Target {
|
|
|
|
pub name: String,
|
|
|
|
pub kind: Vec<Kind>,
|
|
|
|
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 {
|
|
|
|
fn from(err: io::Error) -> Self { Error::Io(err) }
|
|
|
|
}
|
|
|
|
impl From<Utf8Error> for Error {
|
|
|
|
fn from(err: Utf8Error) -> Self { Error::Utf8(err) }
|
|
|
|
}
|
|
|
|
impl From<json::DecoderError> for Error {
|
|
|
|
fn from(err: json::DecoderError) -> Self { Error::Json(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metadata() -> Result<Metadata, Error> {
|
|
|
|
let output = Command::new("cargo").args(&["metadata", "--no-deps"]).output()?;
|
|
|
|
let stdout = from_utf8(&output.stdout)?;
|
|
|
|
Ok(json::decode(stdout)?)
|
|
|
|
}
|