biome icon indicating copy to clipboard operation
biome copied to clipboard

📎 `derive(biome_deserialize::Deserializable)`: add support for enum with payload

Open Conaclos opened this issue 4 months ago • 0 comments

Description

Currently, an enum with a payload (struct or tuple) cannot derive biome_deserialize::Deserializable. For instance, the following code don't compile:

#[derive(Deserializable)]
enum Message {
    Request { id: String, method: String, params: Params },
    Response { id: String, result: Value },
}

We could support this kind of code using the same strategies as serde. I propose to only support the internally tagged pattern. We could require settings the same attribute as serde:

#[derive(Deserializable, Serialize, Deserialize)]
#[serde(tag = "type")]
#[deserializable(tag = "type")]
enum Message {
    Request { id: String, method: String, params: Params },
    Response { id: String, result: Value },
}

We could also implement the adjacent tagging pattern. We could require the use of this second pattern for enum with a tuple payload:

#[derive(Deserializable, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
#[deserializable(tag = "type", content = "value")]
enum Message {
    Request(Request),
    Response(Response),
}

NOTE: I am not sure how serde handle enum with tuple payload with the other strategy.

Conaclos avatar Feb 16 '24 21:02 Conaclos