Skip to content

Commit

Permalink
Add WorkspaceMember
Browse files Browse the repository at this point in the history
The implementation of WorkspaceMember is almost identical to PackageId in cargo.
The difference is that its deserialization is simplified and it does not use
Arc internally.
  • Loading branch information
topecongiro committed Dec 20, 2017
1 parent db0c5b8 commit 596815f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT"
readme = "README.md"

[dependencies]
cargo = "0.23"
error-chain = "0.11.0"
serde = "1.0.2"
serde_derive = "1.0.2"
Expand Down
42 changes: 38 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
//! cargo_metadata::metadata(matches.value_of("manifest-path").map(Path::new)).unwrap();
//! ```

extern crate cargo;
#[macro_use]
extern crate error_chain;
extern crate semver;
Expand All @@ -87,8 +88,10 @@ use std::path::Path;
use std::process::Command;
use std::str::from_utf8;

use cargo::core::source::SourceId;
pub use errors::{Error, ErrorKind, Result};
pub use dependency::{Dependency, DependencyKind};
use serde::de;

mod errors;
mod dependency;
Expand All @@ -99,8 +102,7 @@ pub struct Metadata {
/// A list of all crates referenced by this crate (and the crate itself)
pub packages: Vec<Package>,
/// A list of all workspace members
#[serde(default)]
pub workspace_members: Vec<String>,
pub workspace_members: Vec<WorkspaceMember>,
/// Dependencies graph
pub resolve: Option<Resolve>,
version: usize,
Expand Down Expand Up @@ -159,12 +161,44 @@ pub struct Target {
pub src_path: String,
}

#[derive(Clone, Debug)]
/// A workspace member. This is basically identical to `cargo::core::package_id::PackageId`, expect
/// that this does not use `Arc` internally.
pub struct WorkspaceMember {
/// A name of workspace member.
pub name: String,
/// A version of workspace member.
pub version: semver::Version,
/// A source id of workspace member.
pub url: SourceId,
}

impl<'de> de::Deserialize<'de> for WorkspaceMember {
fn deserialize<D>(d: D) -> std::result::Result<WorkspaceMember, D::Error>
where
D: de::Deserializer<'de>,
{
let string = String::deserialize(d)?;
let mut s = string.splitn(3, ' ');
let name = s.next().unwrap();
let version = s.next().unwrap();
let version = semver::Version::parse(&version).map_err(de::Error::custom)?;
let url = &s.next().unwrap();
let url = &url[1..url.len() - 1];
Ok(WorkspaceMember {
name: name.to_owned(),
version: version,
url: SourceId::from_url(&url).unwrap(),
})
}
}

/// Obtain metadata only about the root package and don't fetch dependencies
///
/// # Parameters
///
/// - `manifest_path`: Path to the manifest.
pub fn metadata(manifest_path: Option<&Path>) -> Result<Metadata> {
pub fn metadata(manifest_path: Option<&Path>) -> errors::Result<Metadata> {
metadata_deps(manifest_path, false)
}

Expand All @@ -174,7 +208,7 @@ pub fn metadata(manifest_path: Option<&Path>) -> Result<Metadata> {
///
/// - `manifest_path`: Path to the manifest.
/// - `deps`: Whether to include dependencies.
pub fn metadata_deps(manifest_path: Option<&Path>, deps: bool) -> Result<Metadata> {
pub fn metadata_deps(manifest_path: Option<&Path>, deps: bool) -> errors::Result<Metadata> {
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
let mut cmd = Command::new(cargo);
cmd.arg("metadata");
Expand Down

0 comments on commit 596815f

Please sign in to comment.