Skip to content

Commit

Permalink
Merge pull request #130 from naerbnic/add_workspace_metadata_support
Browse files Browse the repository at this point in the history
Add workspace metadata field.
  • Loading branch information
oli-obk committed Oct 12, 2020
2 parents 10effcd + 4ec5ac0 commit 98fc1e7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib.rs
Expand Up @@ -120,6 +120,14 @@ impl std::fmt::Display for PackageId {
}
}

// Helpers for default metadata fields
fn is_null(value: &serde_json::Value) -> bool {
match value {
serde_json::Value::Null => true,
_ => false,
}
}

#[derive(Clone, Serialize, Deserialize, Debug)]
/// Starting point for metadata returned by `cargo metadata`
pub struct Metadata {
Expand All @@ -133,6 +141,9 @@ pub struct Metadata {
pub workspace_root: PathBuf,
/// Build directory
pub target_directory: PathBuf,
/// The workspace-level metadata object. Null if non-existent.
#[serde(rename = "metadata", default, skip_serializing_if = "is_null")]
pub workspace_metadata: serde_json::Value,
version: usize,
#[doc(hidden)]
#[serde(skip)]
Expand Down Expand Up @@ -305,7 +316,7 @@ pub struct Package {
/// }
///
/// ```
#[serde(default)]
#[serde(default, skip_serializing_if = "is_null")]
pub metadata: serde_json::Value,
/// The name of a native library the package is linking to.
pub links: Option<String>,
Expand Down
6 changes: 6 additions & 0 deletions tests/all/Cargo.toml
Expand Up @@ -49,3 +49,9 @@ edition = '2015'
[[bin]]
name = "reqfeat"
required-features = ["feat2"]

[workspace]
exclude = ["bdep", "benches", "devdep", "examples", "featdep", "namedep", "oldname", "path-dep", "windep"]

[workspace.metadata.testobject]
myvalue = "abc"
19 changes: 19 additions & 0 deletions tests/test_samples.rs
Expand Up @@ -112,6 +112,7 @@ fn old_minimal() {
);
assert!(meta.resolve.is_none());
assert_eq!(meta.workspace_root, PathBuf::from("/foo"));
assert_eq!(meta.workspace_metadata, serde_json::Value::Null);
assert_eq!(meta.target_directory, PathBuf::from("/foo/target"));
}

Expand Down Expand Up @@ -140,6 +141,16 @@ fn cargo_version() -> semver::Version {
ver
}

#[derive(serde::Deserialize, PartialEq, Eq, Debug)]
struct WorkspaceMetadata {
testobject: TestObject,
}

#[derive(serde::Deserialize, PartialEq, Eq, Debug)]
struct TestObject {
myvalue: String,
}

#[test]
fn all_the_fields() {
// All the fields currently generated as of 1.47. This tries to exercise as
Expand All @@ -165,6 +176,14 @@ fn all_the_fields() {
meta.workspace_root.file_name().unwrap(),
PathBuf::from("all")
);
assert_eq!(
serde_json::from_value::<WorkspaceMetadata>(meta.workspace_metadata).unwrap(),
WorkspaceMetadata {
testobject: TestObject {
myvalue: "abc".to_string()
}
}
);
assert_eq!(meta.workspace_members.len(), 1);
assert!(meta.workspace_members[0].to_string().starts_with("all"));

Expand Down

0 comments on commit 98fc1e7

Please sign in to comment.