Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Attribute macro to apply prefix to name of serialized fields or variants #19

@dtolnay

Description

@dtolnay

Some JSON protocols represent variant types using a common prefix. For example a network result may be transmitted as "NetworkResult_Success" / "NetworkResult_Failure". In Rust we would like to represent such values as enum variants NetworkResult::Success and NetworkResult::Failure. This is possible with Serde rename attributes but somewhat verbose.

#[derive(Deserialize)]
enum NetworkResult {
    #[serde(rename = "NetworkResult_Success")]
    Success,
    #[serde(rename = "NetworkResult_Failure")]
    Failure,
}

It would be nicer to have an attribute macro that inserts a given prefix as a rename attribute on each field, generating the code above.

#[prefix_all("NetworkResult_")]
#[derive(Deserialize)]
enum NetworkResult {
    Success,
    Failure,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions