Skip to content

Add support for adding attributes to enum variants #46

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 4 commits into
base: main
Choose a base branch
from

Conversation

Kriskras99
Copy link
Contributor

When dealing with serde and serde_as it can be useful to add attributes to enum variants:

#[superstruct(
    variants(V2122, V20, V19, V18),
    variant_attributes(derive(Debug, Serialize, Deserialize, Clone)),
    enum_variant_attributes(serde(borrow))
)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub struct ConfigFilesPath<'a> {
    #[superstruct(only(V20, V2122))]
    #[serde(borrow)]
    pub gachacontent: HipStr<'a>,
    #[superstruct(only(V20, V2122))]
    #[serde(borrow)]
    pub ftuesteps: HipStr<'a>,
    #[superstruct(only(V20))]
    #[serde(borrow)]
    pub anthology: HipStr<'a>,
    #[superstruct(only(V20, V2122))]
    #[serde(borrow)]
    pub objectives: HipStr<'a>,
    #[superstruct(only(V19, V20, V2122))]
    #[serde(borrow)]
    pub playlist: HipStr<'a>,
    #[superstruct(only(V20, V2122))]
    #[serde(borrow)]
    pub portraitborders: HipStr<'a>,
    #[superstruct(only(V2122))]
    #[serde(borrow)]
    pub quickplayrules: HipStr<'a>,
    #[superstruct(only(V20, V2122))]
    #[serde(borrow)]
    pub scheduledquests: HipStr<'a>,
    #[superstruct(only(V18, V19))]
    #[serde(borrow)]
    pub dmconfig: HipStr<'a>,
    #[superstruct(only(V19))]
    #[serde(borrow)]
    pub postcards: HipStr<'a>,
}

This PR does need tests, but I'm not sure how to tests this. Do you have any ideas for testing this?

@michaelsproul
Copy link
Member

Sorry this slipped off my radar

To test this I think your example using serde(borrow) could be a good start. We could write a test that only passes (or only compiles) if serde(borrow) is being applied.

Kriskras99 and others added 2 commits April 12, 2025 10:35
If `enum_variant_attributes(serde(borrow))` is removed or commented,
the compilation will fail with `error: lifetime may not live long enough`.
@Kriskras99 Kriskras99 force-pushed the enum-variant-attributes branch from 32df986 to 2c00daf Compare April 12, 2025 08:36
@Kriskras99
Copy link
Contributor Author

Also fell of my radar, I was just using a git dependency in my project.

I've added a test that fails to compile when the enum_variant_attributes line is removed.

Also fixes some small docs issues (unlinked page, wrong link, double word)
@Kriskras99
Copy link
Contributor Author

I've now also added documentation for specific_enum_variant_attributes and fixed some small issues in the book. It's ready for review now @michaelsproul

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.

2 participants