Add Enumeration::values() static slice for variant iteration#49
Merged
Add Enumeration::values() static slice for variant iteration#49
Conversation
Generated enums now implement `Enumeration::values()` which returns a `&'static [Self]` slice of every primary variant in proto declaration order. This subsumes the most common reason users reach for `strum::EnumIter` (per #23), and additionally enables `.contains()`, `.len()`, indexing, and binary search. Aliases (additional names sharing an existing value when `allow_alias = true`) are skipped; they remain accessible as the existing `pub const` aliases. The trait method has a default implementation returning an empty slice so out-of-tree consumers implementing `Enumeration` against an older buffa version continue to compile — they should override the default after regenerating. Closes #23.
asacamano
approved these changes
Apr 16, 2026
iainmcgin
added a commit
that referenced
this pull request
Apr 17, 2026
Follow-up to #44, partly addressing #23. The custom-attributes API from #44 has `type_attribute` (matches both messages and enums) and `message_attribute` (struct-only), but no symmetric enum-only filter. Users wanting to inject a derive like `#[derive(strum::EnumIter)]` on enums had to either also paint every matching message with the same attribute (which usually fails to compile, since the derive only makes sense on enums) or hand-construct an awkward union of multiple `.type_attribute(".pkg.MyEnum1", ...)` / `.type_attribute(".pkg.MyEnum2", ...)` matchers. `enum_attribute` is the symmetric inverse of `message_attribute`. ## API ```rust buffa_build::Config::new() .enum_attribute(".my.pkg", "#[derive(strum::EnumIter)]") .files(&["proto/my_service.proto"]) .includes(&["proto/"]) .compile() .unwrap(); ``` Same path-matching, normalization (leading-dot prepend, trailing-dot trim, segment-aware prefix), insertion-order accumulation, and `CodeGenError::InvalidCustomAttribute` semantics as the existing trio. ## Codegen In `enumeration.rs`, after the existing `type_attribute` match, also match `enum_attributes` and emit both attribute streams above the `pub enum` declaration. `type_attribute` continues to apply to enums as well — `enum_attribute` is additive, not exclusive. ## Tests - `test_enum_attribute_on_enum_not_struct` — catch-all `enum_attribute` lands on the enum but not on a sibling message struct. - `test_enum_attribute_scoped_to_specific_enum` — prefix-targeted attribute lands on the matched enum only. - `test_enum_attribute_does_not_apply_to_struct` — defense-in-depth check on the non-bleed property. - `enum_attribute_forwards_normalized_path` — build-layer test confirming path normalization and that the other three attribute lists remain untouched. Workspace tests, clippy `-D warnings`, rustfmt clean. No codegen drift. ## Relation to #23 #23 originally asked for `enum_attribute` as a way to inject `#[derive(strum::EnumIter)]`. PR #49 (currently in flight) addresses the underlying use case directly by adding `Enumeration::values()`, removing the need for the third-party `EnumIter` derive in most cases. This PR adds `enum_attribute` because the API symmetry is still genuinely useful (e.g. for serde, schemars, custom derives) and the absence was an asymmetry surprise.
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.
Partially addresses #23 (at least, the most common use case for enum attributes).
Generated enums now expose
Enumeration::values(), returning a&'static [Self]slice of every primary variant in proto declaration order. This subsumes the most common reason users reach forstrum::EnumIter(the example in #23) and additionally enables.contains(),.len(), indexing, binary search, etc.Example
For
users can now write
Behavior
option allow_alias = true) are skipped — they remain accessible as the existingpub constaliases on the enum, but they aren't enum variants in Rust so they don't belong invalues()..protodeclaration order, which is also the orderfrom_i32resolves to (soStatus::values()[i].to_i32()lines up withfrom_i32for unique values).Trait change
Enumeration::valuesis added with a default implementation returning an empty slice so out-of-tree consumers implementingEnumerationagainst an older buffa version continue to compile. Codegen unconditionally overrides the default with the real impl.Tests
tests/generation.rs:test_enum_values_emits_static_slice_in_declaration_orderandtest_enum_values_skips_aliases.-D warnings, rustfmt, and markdownlint clean.task gen-wkt-typesandtask gen-bootstrap-typesrun; the resulting drift inbuffa-types/src/generated/andbuffa-descriptor/src/generated/is just the newvalues()impls.Follow-up
A separate PR will add
enum_attribute(the symmetric inverse ofmessage_attributefrom #44) for users who want to inject Rust attributes onto enums but not messages.