Skip to content
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

Enum support #25

Closed
wants to merge 3 commits into from
Closed

Enum support #25

wants to merge 3 commits into from

Conversation

Deaths-Door
Copy link

I've been working on supporting enum variants (#23) (currently only named enum variants have any progress) with the #[builder] proc-macro in bon. While I've made progress, there are a couple of issues and limitations I'm stuck on and would appreciate some guidance/help.

Issues:

  • Double struct generation: The generated code seems to define the generated EnumVariantBuilder struct twice . The builder implementation, however, is only defined once.
  • Original enum disappearance: When placing the #[builder] macro on an enum, the original enum definition disappears from the expanded code. This can be bypassed by defining the enum twice, but this is a hack.

Example Code

#[builder]
enum Expression {
  None,
  Binary {
    operation: char,
    left: u32,
    right: u32,
  }
}

This code results in the strange behavior mentioned above. Defining the enum twice allows compilation:

#[builder]
enum Expression {
  None,
  Binary {
    operation: char,
    left: u32,
    right: u32,
  }
}

enum Expression {
  None,
  Binary {
    operation: char,
    left: u32,
    right: u33,
  }
}

Limitations

  • No generics support currently: The current implementation doesn't support generics for enums. I'm unsure how to approach defining minimal generics (mainly bounds) for the enum variants. My idea is to introduce a new attribute like bon(bound = "..."), similar to serde(bound = "..."). (which is obviously up for discussion)

  • Customization of the generated struct is disabled : Currently, customizing the generated struct is not supported. This is due to the StructInputParms struct encountering an error when using from_meta::from_list on enum variants, as it returns an error, which has a gist of unknown field Binary.

Total Generated Code from latest commit

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