codegen: idiomatic UpperCamelCase enum aliases#152
Merged
Conversation
Protobuf enum values are SHOUTY_SNAKE_CASE (e.g. RULE_LEVEL_HIGH), which is unidiomatic in Rust. Those names remain the definitive variants, but codegen now additionally emits associated consts with the enum-name prefix stripped and the name converted to UpperCamelCase (RuleLevel::High), so downstream code can use idiomatic names. The aliases are purely additive: existing references and Debug output are unchanged, and the aliases work in pattern position with exhaustiveness checking preserved. The conversion is lossy, so two values can collide (FOO_BAR and FOO__BAR both map to FooBar). The rule is all-or-nothing per enum: if any two values collide after conversion, or a value would yield an invalid identifier, no aliases are emitted for that enum. A structured CodeGenWarning (surfaced as a build warning) and an enum doc note explain why, naming the exact values. This guarantees a match is never forced to mix conventions. Enabled by default via the new idiomatic_enum_aliases config flag (and the buffa-build builder setter). Diagnostics are returned by the new generate_with_diagnostics; the existing generate() delegates and discards them. Regenerates the checked-in WKT, bootstrap descriptor, and logging-example code. Closes #13
|
All contributors have signed the CLA ✍️ ✅ |
rpb-ant
approved these changes
May 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #13. Protobuf enum values are
SHOUTY_SNAKE_CASE(e.g.RULE_LEVEL_HIGH), which is unidiomatic in Rust. This makes codegen additionally emit idiomaticUpperCamelCasealiases.The proto names remain the definitive variants — this is purely additive:
So
RuleLevel::RULE_LEVEL_HIGHkeeps working unchanged (includingDebugoutput), andRuleLevel::Highis now also available. The aliases are associatedconsts, which work in pattern position with exhaustiveness checking preserved (verified on the 1.85 MSRV); a non-exhaustivematcherror just names the underlyingSHOUTY_SNAKE_CASEvariant.Collision handling
The CamelCase conversion is lossy (
FOO_BARandFOO__BARboth →FooBar). The rule is all-or-nothing per enum: if any two values collide after conversion — or a value would yield an invalid identifier — no aliases are emitted for that enum. A structuredCodeGenWarning(surfaced as acargo:warning=bybuffa-build, on stderr by the generators) and a doc note on the generated enum explain why, naming the exact values. This guarantees amatchis never forced to mix conventions.Edge cases handled and tested: lossy fold, the silent variant↔const shadow (
FOO_BARvs a literalFooBar), empty/leading-digit remainders after prefix stripping (fall back to unstripped), Rust keywords (KIND_SELF→Self_), and already-CamelCase values (skipped as redundant). Prefix stripping is all-or-nothing per enum, so the result never mixes stripped and unstripped names.API
CodeGenConfig::idiomatic_enum_aliases(default on — additive, so backward-compatible) and abuffa_build::Builder::idiomatic_enum_aliasessetter.generate_with_diagnosticsreturningVec<CodeGenWarning>; the existinggenerate()delegates and discards them, soconnectrpc-codegenis unaffected.Reviewed by
rust-code-reviewerandrust-api-ergonomics-reviewer(two rounds); all Critical/High/Medium findings addressed.