Skip to content

codegen: idiomatic UpperCamelCase enum aliases#152

Merged
iainmcgin merged 1 commit into
mainfrom
feat/idiomatic-enum-aliases
May 24, 2026
Merged

codegen: idiomatic UpperCamelCase enum aliases#152
iainmcgin merged 1 commit into
mainfrom
feat/idiomatic-enum-aliases

Conversation

@iainmcgin
Copy link
Copy Markdown
Collaborator

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 idiomatic UpperCamelCase aliases.

The proto names remain the definitive variants — this is purely additive:

pub enum RuleLevel { RULE_LEVEL_UNKNOWN = 0, RULE_LEVEL_HIGH = 1, /* ... */ }

impl RuleLevel {
    pub const Unknown: Self = Self::RULE_LEVEL_UNKNOWN;
    pub const High: Self = Self::RULE_LEVEL_HIGH;
}

So RuleLevel::RULE_LEVEL_HIGH keeps working unchanged (including Debug output), and RuleLevel::High is now also available. The aliases are associated consts, which work in pattern position with exhaustiveness checking preserved (verified on the 1.85 MSRV); a non-exhaustive match error just names the underlying SHOUTY_SNAKE_CASE variant.

Collision handling

The CamelCase conversion is lossy (FOO_BAR and FOO__BAR both → 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 cargo:warning= by buffa-build, on stderr by the generators) and a doc note on the generated enum explain why, naming the exact values. This guarantees a match is never forced to mix conventions.

Edge cases handled and tested: lossy fold, the silent variant↔const shadow (FOO_BAR vs a literal FooBar), empty/leading-digit remainders after prefix stripping (fall back to unstripped), Rust keywords (KIND_SELFSelf_), 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

  • New CodeGenConfig::idiomatic_enum_aliases (default on — additive, so backward-compatible) and a buffa_build::Builder::idiomatic_enum_aliases setter.
  • New generate_with_diagnostics returning Vec<CodeGenWarning>; the existing generate() delegates and discards them, so connectrpc-codegen is unaffected.
  • Regenerates the checked-in WKT, bootstrap descriptor, and logging-example code.

Reviewed by rust-code-reviewer and rust-api-ergonomics-reviewer (two rounds); all Critical/High/Medium findings addressed.

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
@github-actions
Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@iainmcgin iainmcgin marked this pull request as ready for review May 24, 2026 03:57
@iainmcgin iainmcgin enabled auto-merge (squash) May 24, 2026 03:57
@iainmcgin iainmcgin requested a review from rpb-ant May 24, 2026 03:57
@iainmcgin iainmcgin merged commit 87214bb into main May 24, 2026
7 checks passed
@iainmcgin iainmcgin deleted the feat/idiomatic-enum-aliases branch May 24, 2026 14:38
@github-actions github-actions Bot locked and limited conversation to collaborators May 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enumeration are not idomatic

2 participants