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 support for package.metadata table #37

Merged
merged 7 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ version = "0.9"
[dev-dependencies]
clap = "2.26.0"
docopt = "0.8.1"

[package.metadata.cargo_metadata_test]
some_field = true
other_field = "foo"
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ pub struct Package {
pub features: HashMap<String, Vec<String>>,
/// Path containing the `Cargo.toml`
pub manifest_path: String,
/// Contents of the free form package.metadata section
pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be an Option? Can't it simply be an empty table? I don't want to distinguish between no table and an empty table. That feels like noise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty table doesn't parse if the key is not present. But I pushed a new version that makes the field a serde_json::Value (null if not present).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried #[serde(default)] on the metadata field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the metadata field is always present and null if not set. serde(default) fails in this case. So I think serde_json::Value is the correct type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But serde(default) helps that no “missing field metadata” error occurs on stable.

#[doc(hidden)]
#[serde(skip)]
__do_not_match_exhaustively: (),
Expand Down
10 changes: 10 additions & 0 deletions tests/selftest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ fn metadata() {
assert_eq!(metadata.packages[0].targets[1].name, "selftest");
assert_eq!(metadata.packages[0].targets[1].kind[0], "test");
assert_eq!(metadata.packages[0].targets[1].crate_types[0], "bin");

let package_metadata = metadata.packages[0].metadata.as_ref().expect("metadata key missing");
assert_eq!(package_metadata.len(), 1);
let test_package_metadata = match package_metadata.get("cargo_metadata_test").unwrap() {
serde_json::Value::Object(metadata) => metadata,
_ => panic!(),
};
assert_eq!(test_package_metadata.len(), 2);
assert_eq!(test_package_metadata.get("some_field"), Some(&serde_json::Value::Bool(true)));
assert_eq!(test_package_metadata.get("other_field"), Some(&serde_json::Value::String("foo".into())));
}

#[test]
Expand Down