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

Allow passing a function name to rename_all and rename_all_fields #2852

Open
njames93 opened this issue Oct 31, 2024 · 1 comment
Open

Allow passing a function name to rename_all and rename_all_fields #2852

njames93 opened this issue Oct 31, 2024 · 1 comment

Comments

@njames93
Copy link

njames93 commented Oct 31, 2024

Currently rename_all is pretty restrictive in what it allows. It would be nice if you could pass the name of a function and use that to rename the fields.

The function should have the signature

fn rename_field(&str) -> String

The specific use case is for tagged enums, where the tag in my case has a bit of preamble at the start of the string that is shared with all tags.

[
  {
    "type": "MyLongPreamble.Foo"
  },
  {
    "type": "MyLongPreamble.Bar"
  }
]

In this case it would be nice to have a function like this

fn remap(val: &str) -> String {
  "MyLongPreamble.".to_owned() + val
}

#[serde(tag="type", rename_all = "remap")]
pub enum Item {
  Foo,
  Bar
}
@oli-obk
Copy link
Member

oli-obk commented Nov 1, 2024

I don't think this is doable. These functions would need to be run at derive time, which is a concept rustc does not support yet (unclear if ever).

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

No branches or pull requests

2 participants