Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workspace metadata field. #130

Merged
merged 4 commits into from Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib.rs
Expand Up @@ -120,6 +120,11 @@ impl std::fmt::Display for PackageId {
}
}

// Helpers for default metadata fields
fn is_null(value: &serde_json::Value) -> bool {
matches!(value, serde_json::Value::Null)
}

#[derive(Clone, Serialize, Deserialize, Debug)]
/// Starting point for metadata returned by `cargo metadata`
pub struct Metadata {
Expand All @@ -133,6 +138,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 +313,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
3 changes: 3 additions & 0 deletions tests/all/Cargo.toml
Expand Up @@ -49,3 +49,6 @@ edition = '2015'
[[bin]]
name = "reqfeat"
required-features = ["feat2"]

[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(),
naerbnic marked this conversation as resolved.
Show resolved Hide resolved
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