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

Deserialize a map into a Vec of keys #541

Open
figsoda opened this issue Jan 20, 2023 · 2 comments
Open

Deserialize a map into a Vec of keys #541

figsoda opened this issue Jan 20, 2023 · 2 comments

Comments

@figsoda
Copy link

figsoda commented Jan 20, 2023

something like

#[serde_as]
#[derive(Deserialize)]
struct Foo {
  #[serde_as(as = "MapKeys<_>")]
  bar: Vec<String>,
}

that will deserialize

{
  "bar": {
    "foo": "bar",
    "baz": 42
  }
}

into

Foo {
  bar: vec!["foo", "baz"],
}

and discard the values

@jonasbb
Copy link
Owner

jonasbb commented Jan 22, 2023

Hi, thanks for the suggestion. Do you have a specific data source where that is needed? I wonder which data formats this would be used with and whether they are self describing or not.

@figsoda
Copy link
Author

figsoda commented Jan 22, 2023

This would be helpful for parsing pyproject poetry dependencies and discarding the version requirements, this is the solution I am using right now: https://github.com/nix-community/nix-init/blob/6c9f7838f58bb361ed692919eca26bb18ce87f47/src/python.rs#L27-L60, and I had to write extra boilerplate code for PoetryDependency just to be discarded

this is very similar to cargo's dependencies section, so you can just imagine that if that's easier for you, basically something like

anyhow = "1.0.68"
askalono = "0.4.6"
bstr = "1.1.0"
expand = "0.2.1"

would yield something like ["anyhow", "askalono", "bstr", "expand"] (but BTreeSet<String> in my case)

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

No branches or pull requests

2 participants