Skip to content

Conversation

@regexident
Copy link

@regexident regexident commented May 11, 2025

By lacking support for #[serde(untagged)] attributes on individual enum variants the crate fails to support a common pattern for modeling string-based enums:

type Kind = "foo" | "bar" | "baz" | "blee" | string;
#[derive(Serialize, TypeDef)]
#[serde(rename_all = "kebab-case")]
pub enum Kind {
    Foo,
    Bar,
    Baz,
    Blee,
    #[serde(untagged)]
    Unknown(String),
}

Deserializing unknown "kind" values as Kind::Unknown(…) makes Kind future-proof, which is particularly import in scenarios where one does not control the source providing the serialized representations of Kind, but also has a requirement to never fail on unexpected values.

In the particular scenario above the following would work just fine, as is:

#[derive(Serialize, TypeDef)]
#[serde(untagged, rename_all = "kebab-case")]
pub enum Kind {
    Foo,
    Bar,
    Baz,
    Blee,
    Unknown(String),
}

… but there are other scenarios where an enum-level #[serde(untagged)] is undesirable, such as if individual "known" variants had additional fields.

@regexident regexident force-pushed the untagged-enum-variant branch from 1e37184 to 960cb7f Compare May 11, 2025 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant