Skip to content

Add support for #[serde(untagged)] attributes on individual enum variants #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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