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

Conversation

phil-opp
Copy link
Contributor

Since rust-lang/cargo#5360 cargo metadata outputs the contents of the optional package.metadata table. This PR adds support for the table to this crate. Since the table can contain arbitrary content, the return type is a dynamic serde_json::Map.

Unresolved questions:

  • Is it ok to expose types of the serde_json dependency in the public API? Or do we need to make the serde_json dependency public?
  • The test doesn't work on stable yet. Is there a way to conditionally execute the test only on nightly and beta?

Copy link
Owner

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

The test doesn't work on stable yet. Is there a way to conditionally execute the test only on nightly and beta?

There should be env vars in travis.

Is it ok to expose types of the serde_json dependency in the public API? Or do we need to make the serde_json dependency public?

Maybe we could make the field generic. If it's generic the user can choose their own metadata type instead of having to get their data from a serde map.

For backwards compat you could rename all generic types to GenericFoo or sth and reexport via type Foo = GenericFoo<()>; or type Foo = GenericFoo<json::Map<String, serde_json::Value>>;

no need to reexport serde_json, bumping to a potential 2.0 would require a version bump anyway.

src/lib.rs Outdated
@@ -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.

@phil-opp
Copy link
Contributor Author

There should be env vars in travis.

Ok. Should I add an if condition to the test that checks the environment variable? Or should I create a new nightly/beta only test and put the condition in the travis.yml?

Maybe we could make the field generic. If it's generic the user can choose their own metadata type instead of having to get their data from a serde map.

Sounds like a good idea, but I'm not quite sure if this would work with multiple packages. The metadata struct stores a Vec<Package>, so if we make Package generic all packages must have the same metadata package.

@oli-obk
Copy link
Owner

oli-obk commented May 31, 2018

Ok. Should I add an if condition to the test that checks the environment variable? Or should I create a new nightly/beta only test and put the condition in the travis.yml?

just put it in the test, maybe with a nice error message if the condition is not set, so users running locally know what's going on.

Sounds like a good idea, but I'm not quite sure if this would work with multiple packages. The metadata struct stores a Vec, so if we make Package generic all packages must have the same metadata package.

oh god. yea let's not do anything here. Just store the json and maybe provide an example in the readme showing how to deserialize that into a custom structure.

@phil-opp
Copy link
Contributor Author

I added a “NOTE: This test currently only works on the beta and nightly channel.” error message and a short example how to transform a serde_json::Value into a custom struct.

@oli-obk oli-obk merged commit 49824e5 into oli-obk:master May 31, 2018
@phil-opp phil-opp deleted the metadata-table branch May 31, 2018 14:00
@phil-opp
Copy link
Contributor Author

Thanks! Could you release a new version to crates.io?

@oli-obk
Copy link
Owner

oli-obk commented May 31, 2018

I do have a deskop open for it and even ran cargo test then forgot about it -.- Thanks for the reminder

@oli-obk
Copy link
Owner

oli-obk commented May 31, 2018

published as v0.5.5

@phil-opp
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants