Add enum_attribute API for enum-only Rust attributes#50
Merged
Conversation
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 derives like #[derive(strum::EnumIter)] on enums had to either also paint every matching message with the same attribute or build an awkward union of multiple specific type matchers. Add enum_attribute as the inverse of message_attribute. - New CodeGenConfig::enum_attributes field, populated by the new buffa-build::Config::enum_attribute builder method. - Wired into enumeration.rs codegen alongside the existing type_attributes match — emitted in addition to (not instead of) type-attribute output, so users get both layers if they want. - Same path-matching, normalization, and error semantics as the existing trio. Tests cover: enum-only landing, scoping to a specific enum, non-bleed onto sibling messages, and the build-layer normalization.
asacamano
approved these changes
Apr 16, 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.
Follow-up to #44, partly addressing #23.
The custom-attributes API from #44 has
type_attribute(matches both messages and enums) andmessage_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_attributeis the symmetric inverse ofmessage_attribute.API
Same path-matching, normalization (leading-dot prepend, trailing-dot trim, segment-aware prefix), insertion-order accumulation, and
CodeGenError::InvalidCustomAttributesemantics as the existing trio.Codegen
In
enumeration.rs, after the existingtype_attributematch, also matchenum_attributesand emit both attribute streams above thepub enumdeclaration.type_attributecontinues to apply to enums as well —enum_attributeis additive, not exclusive.Tests
test_enum_attribute_on_enum_not_struct— catch-allenum_attributelands 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_attributeas a way to inject#[derive(strum::EnumIter)]. PR #49 (currently in flight) addresses the underlying use case directly by addingEnumeration::values(), removing the need for the third-partyEnumIterderive in most cases. This PR addsenum_attributebecause the API symmetry is still genuinely useful (e.g. for serde, schemars, custom derives) and the absence was an asymmetry surprise.