jonasbb / serde_with

This crate provides custom de/serialization helpers to use in combination with serde's `with`-annotation and with the improved `serde_as`-annotation.
https://docs.rs/serde_with
Apache License 2.0
667 stars 72 forks source link

`with_prefix` attribute #759

Closed xamgore closed 4 months ago

xamgore commented 5 months ago

Hi, I am wondering, is it technically possible to omit separate macro-calls (as in #9) and inline them into the list of attributes?

#[derive(Serialize, Deserialize)]
struct Match {
    #[serde(flatten, with_prefix = "player1_")]
    player1: Player,
    #[serde(flatten, with_prefix = "player2_")]
    player2: Player,
}

#[derive(Serialize, Deserialize)]
struct Player {
    name: String,
    votes: u64,
}

I'm in the middle of writing a docx-template library, that takes user data and distributes it to a document's {placeholders}. Things get worse, when you have a lot of keys, so flatten+with_prefix would allow avoiding name conflicts.

jonasbb commented 4 months ago

I think to do that you would need something like feature(adt_const_params) https://github.com/rust-lang/rust/issues/95174 You need some way to "store" the prefix somehow, but there are no variables to do so. This leaves the option to encode it into the code, which is what the current with_prefix! macro does by generating a specific code snippet for each prefix.