diff --git a/Cargo.toml b/Cargo.toml index 30c5b7ec..fae58d6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo_metadata" -version = "0.2.1" +version = "0.2.2" authors = ["Oliver Schneider "] repository = "https://github.com/oli-obk/cargo_metadata" description = "structured access to the output of `cargo metadata`" diff --git a/src/lib.rs b/src/lib.rs index 9df05c73..6cfa05e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,10 +23,27 @@ use std::io; pub struct Metadata { /// A list of all crates referenced by this crate (and the crate itself) pub packages: Vec, - resolve: Option<()>, + /// Dependencies graph + pub resolve: Option, version: usize, } +#[derive(Clone, Deserialize, Debug)] +/// A dependency graph +pub struct Resolve { + /// Nodes in a dependencies graph + pub nodes: Vec, +} + +#[derive(Clone, Deserialize, Debug)] +/// A node in a dependencies graph +pub struct Node { + /// An opaque identifier for a package + pub id: String, + /// List of opaque identifiers for this node's dependencies + pub dependencies: Vec, +} + #[derive(Clone, Deserialize, Debug)] /// A crate pub struct Package { @@ -34,14 +51,15 @@ pub struct Package { pub name: String, /// Version given in the `Cargo.toml` pub version: String, - id: String, + /// An opaque identifier for a package + pub id: String, source: Option, /// List of dependencies of this particular package pub dependencies: Vec, /// Targets provided by the crate (lib, bin, example, test, ...) pub targets: Vec, features: HashMap>, - /// path containing the `Cargo.toml` + /// Path containing the `Cargo.toml` pub manifest_path: String, } @@ -102,11 +120,30 @@ impl From for Error { } } -/// The main entry point to obtaining metadata +/// Obtain metadata only about the root package and don't fetch dependencies +/// +/// # Parameters +/// +/// - `manifest_path_arg`: Path to the manifest. pub fn metadata(manifest_path_arg: Option<&str>) -> Result { + metadata_deps(manifest_path_arg, false) +} + +/// The main entry point to obtaining metadata +/// +/// # Parameters +/// +/// - `manifest_path_arg`: Path to the manifest. +/// - `deps`: Whether to include dependencies. +pub fn metadata_deps(manifest_path_arg: Option<&str>, deps: bool) -> Result { let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo")); let mut cmd = Command::new(cargo); - cmd.arg("metadata").arg("--no-deps"); + cmd.arg("metadata"); + + if !deps { + cmd.arg("--no-deps"); + } + cmd.arg("--format-version").arg("1"); if let Some(mani) = manifest_path_arg { cmd.arg(mani); diff --git a/tests/selftest.rs b/tests/selftest.rs index 43f0fd9c..4f7148aa 100644 --- a/tests/selftest.rs +++ b/tests/selftest.rs @@ -1,8 +1,9 @@ extern crate cargo_metadata; #[test] -fn foo() { +fn metadata() { let metadata = cargo_metadata::metadata(None).unwrap(); + assert_eq!(metadata.packages[0].name, "cargo_metadata"); assert_eq!(metadata.packages[0].targets.len(), 2); @@ -14,3 +15,20 @@ fn foo() { assert_eq!(metadata.packages[0].targets[1].kind[0], "test"); assert_eq!(metadata.packages[0].targets[1].crate_types[0], "bin"); } + +#[test] +fn metadata_deps() { + let metadata = cargo_metadata::metadata_deps(None, true).unwrap(); + let this = metadata.packages.iter().find(|package| package.name == "cargo_metadata").expect("Did not find ourselves"); + + assert_eq!(this.name, "cargo_metadata"); + assert_eq!(this.targets.len(), 2); + + assert_eq!(this.targets[0].name, "cargo_metadata"); + assert_eq!(this.targets[0].kind[0], "lib"); + assert_eq!(this.targets[0].crate_types[0], "lib"); + + assert_eq!(this.targets[1].name, "selftest"); + assert_eq!(this.targets[1].kind[0], "test"); + assert_eq!(this.targets[1].crate_types[0], "bin"); +}