structdiff icon indicating copy to clipboard operation
structdiff copied to clipboard

serde diff enum representation

Open carlocorradini opened this issue 7 months ago • 2 comments

Allow for customization of the generated diff enum serde representation. Currently, each diff is serialized/deserialized using the default Externally tagged enum representation, with the key being the field's name and the value being the updated value. This is difficult to parse effectively and safely in TypeScript since the object lacks a common discriminant field. By allowing us to change the enum representation, we gain a lot of versatility because we can select between:

  • Externally tagged (default)

    #[derive(Serialize, Deserialize, Difference)]
    struct Example { }
    
  • Internally tagged

    #[derive(Serialize, Deserialize, Difference)]
    #[difference(serde(tag = "type"))]
    struct Example { }
    
  • Adjacently tagged

    #[derive(Serialize, Deserialize, Difference)]
    #[difference(serde(tag = "t", content = "c"))]
    struct Example { }
    
  • Untagged

    #[derive(Serialize, Deserialize, Difference)]
    #[difference(serde(untagged))]
    struct Example { }
    

carlocorradini avatar Jul 03 '25 13:07 carlocorradini

I think this is reasonable. I would prefer to solve this by allowing arbitrary pass through to the derived enum type

knickish avatar Jul 04 '25 02:07 knickish

Would you agree with something like the following?

#[derive(Serialize, Deserialize, Difference)]
#[difference(attr(
    #[derive(tag = "field", content = "value")]
))]
pub struct ProjectMeta { ... }

carlocorradini avatar Jul 04 '25 07:07 carlocorradini