-
Notifications
You must be signed in to change notification settings - Fork 2
[FEATURE]: Generics, lifetimes, where clauses and internal field variant formatting #5
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
base: main
Are you sure you want to change the base?
Conversation
Simplified the code a bit. It was previously checking that the user actually referenced each field before emitting variables for them, but this adds unnecessary complexity. We can just emit all field variables and let the compiler decide if they're unused. |
Btw, my specific use case was this: #[derive(Debug, Clone, PartialEq, Eq, EnumDisplay)]
#[enum_display(case = "Lower")]
pub enum ParserErrorKind<I> {
NomError(ErrorKind),
#[variant_display(format = "{variant}: {0}")]
Keyword(Keyword),
Ident(Ident<I>),
...
} Where I wanted to provide a general impl of Display for most variants, but be specific for required Keywords. |
Thanks for submitting these PRs! Both the generics impl and this. I'll have time next weekend to actually look at this and critique. From the 1000 mile overview it looks like a solid addition to this library. It seems like the build pipeline is currently failing and I'll investigate that more as well. Is it possibly due to different version of Thanks again! |
Cheers! Glad you like it on the surface. The generics side of things was actually really simple (almost like the code equivalent of copy paste - you take the generics from I've had a quick look at the build issue and it's very weird. Does indeed look like maybe a version mismatch. Anyway, yeah, like you, I'll be able to spend more time on this next weekend. |
@jngls I created a branch on top of your changes you pushed here and have got the CI/CD pipeline building and fixed a few other things here: #6. You can see the fixes that were needed. I'm unfortunately not a git master, so I think some of the history got borked with my changes being pushed upstream to my repo from your fork/branch. Feel free to add my commits to your PR and it works great if you want to properly preserve your impact on the codebase. Thanks again for the great code and suggestions here. I hope my fixes are good enough for you! |
Almost forgot, we should update the docs if there's anything missing and the |
This is a considerable update of the proc macro which supports optional custom format strings per variant with full access to variant fields via the
#[variant_display(format = "xyz")]
attribute.I recommend reading the code in isolation rather than as a diff.
Summary
EnumAttrs
which handles parsing of the mainenum_display
attributeVariantAttrs
which handles parsing of the newvariant_display
field attributevariant_display
field attribute allows the user to customise each variantformat = "xyz"
argument defines a custom display format (see below for Format Behaviour) - if omitted, the original crate behaviour is usedVariantInfo
holds properties common to all variant typesNamedVariantIR
,UnnamedVariantIR
, andUnitVariantIR
are the intermediate representations of variant typesVariantIR
wraps all of thosesyn
tokens is moved to thefrom_*()
methods ofEnumAttrs
,VariantAttrs
, andVariantIR
VariantIR::gen()
methodFormat Behaviour
Format strings behave almost identically to using
format!()
in normal rust code so it should be very familiar to users. Besides a bit of translation of unnamed fields, the string specified by the user is the string that ends up as the parameter toformat!()
in each match arm.The following considerations apply:
{variant}
placeholderVariant { my_field: u32 }
, you use the{my_field}
placeholderVariant(u32)
, you use the{0}
placeholder (to access the first tuple element){my_field:?}
displaysmy_field
using itsDebug
implementation,{0:5}
displays the first unnamed field with a width of 5 - however - format spec options which depend on extra arguments passed intoformat!()
are not supportedAdditional Notes
format!()
- perhaps instead, we should extern alloc and hide it behind a feature flag?main
- let me know if you want them separated. Otherwise merging Implement generics, lifetimes, bounds and where clause support #4 first or discarding and just merging this would also work.Full examples