rust/clippy_lints/src/utils/cargo.rs

78 lines
1.7 KiB
Rust
Raw Normal View History

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;
#[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,
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>,
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,
source: Option<String>,
2016-05-30 05:47:04 -05:00
pub req: String,
kind: Option<String>,
optional: bool,
uses_default_features: bool,
2016-06-02 10:29:25 -05:00
features: Vec<String>,
target: Option<String>,
}
#[derive(RustcDecodable, Debug)]
pub struct Target {
pub name: String,
2016-06-02 10:29:25 -05:00
pub kind: Vec<String>,
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)
}
2016-05-30 05:47:04 -05:00
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self {
Error::Utf8(err)
}
2016-05-30 05:47:04 -05:00
}
impl From<json::DecoderError> for Error {
fn from(err: json::DecoderError) -> Self {
Error::Json(err)
}
2016-05-30 05:47:04 -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)?)
}