Commit b065d14
authored
Add Enumeration::values() static slice for variant iteration (#49)
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 for
`strum::EnumIter` (the example in #23) and additionally enables
`.contains()`, `.len()`, indexing, binary search, etc.
## Example
For
```protobuf
enum Status {
UNSPECIFIED = 0;
ACTIVE = 1;
INACTIVE = 2;
}
```
users can now write
```rust
for v in Status::values() {
println!("{:?} = {}", v, v.to_i32());
}
assert!(Status::values().contains(&Status::ACTIVE));
assert_eq!(Status::values().len(), 3);
```
## Behavior
- **Aliases** (additional names sharing an existing value when `option
allow_alias = true`) are skipped — they remain accessible as the
existing `pub const` aliases on the enum, but they aren't enum variants
in Rust so they don't belong in `values()`.
- **Order** matches the `.proto` declaration order, which is also the
order `from_i32` resolves to (so `Status::values()[i].to_i32()` lines up
with `from_i32` for unique values).
## Trait change
`Enumeration::values` is added with a default implementation returning
an empty slice so out-of-tree consumers implementing `Enumeration`
against an older buffa version continue to compile. Codegen
unconditionally overrides the default with the real impl.
## Tests
- New unit tests in `tests/generation.rs`:
`test_enum_values_emits_static_slice_in_declaration_order` and
`test_enum_values_skips_aliases`.
- Workspace tests, clippy `-D warnings`, rustfmt, and markdownlint
clean.
- `task gen-wkt-types` and `task gen-bootstrap-types` run; the resulting
drift in `buffa-types/src/generated/` and
`buffa-descriptor/src/generated/` is just the new `values()` impls.
## Follow-up
A separate PR will add `enum_attribute` (the symmetric inverse of
`message_attribute` from #44) for users who want to inject Rust
attributes onto enums but not messages.1 parent fc7ceb5 commit b065d14
File tree
7 files changed
+234
-0
lines changed- buffa-codegen/src
- tests
- buffa-descriptor/src/generated
- buffa-types/src/generated
- buffa/src
- docs
7 files changed
+234
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
119 | 124 | | |
120 | 125 | | |
121 | 126 | | |
| |||
162 | 167 | | |
163 | 168 | | |
164 | 169 | | |
| 170 | + | |
165 | 171 | | |
166 | 172 | | |
167 | 173 | | |
| |||
257 | 263 | | |
258 | 264 | | |
259 | 265 | | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
260 | 270 | | |
261 | 271 | | |
262 | 272 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
156 | 222 | | |
157 | 223 | | |
158 | 224 | | |
| |||
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
732 | 732 | | |
733 | 733 | | |
734 | 734 | | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
735 | 742 | | |
736 | 743 | | |
737 | 744 | | |
| |||
Lines changed: 115 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
110 | 127 | | |
111 | 128 | | |
112 | 129 | | |
| |||
152 | 169 | | |
153 | 170 | | |
154 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
155 | 175 | | |
156 | 176 | | |
157 | 177 | | |
| |||
1809 | 1829 | | |
1810 | 1830 | | |
1811 | 1831 | | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
1812 | 1835 | | |
1813 | 1836 | | |
1814 | 1837 | | |
| |||
2603 | 2626 | | |
2604 | 2627 | | |
2605 | 2628 | | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
| 2646 | + | |
| 2647 | + | |
| 2648 | + | |
| 2649 | + | |
| 2650 | + | |
2606 | 2651 | | |
2607 | 2652 | | |
2608 | 2653 | | |
| |||
2647 | 2692 | | |
2648 | 2693 | | |
2649 | 2694 | | |
| 2695 | + | |
| 2696 | + | |
| 2697 | + | |
2650 | 2698 | | |
2651 | 2699 | | |
2652 | 2700 | | |
| |||
4639 | 4687 | | |
4640 | 4688 | | |
4641 | 4689 | | |
| 4690 | + | |
| 4691 | + | |
| 4692 | + | |
4642 | 4693 | | |
4643 | 4694 | | |
4644 | 4695 | | |
| |||
5624 | 5675 | | |
5625 | 5676 | | |
5626 | 5677 | | |
| 5678 | + | |
| 5679 | + | |
| 5680 | + | |
5627 | 5681 | | |
5628 | 5682 | | |
5629 | 5683 | | |
| |||
5667 | 5721 | | |
5668 | 5722 | | |
5669 | 5723 | | |
| 5724 | + | |
| 5725 | + | |
| 5726 | + | |
5670 | 5727 | | |
5671 | 5728 | | |
5672 | 5729 | | |
| |||
5714 | 5771 | | |
5715 | 5772 | | |
5716 | 5773 | | |
| 5774 | + | |
| 5775 | + | |
| 5776 | + | |
5717 | 5777 | | |
5718 | 5778 | | |
5719 | 5779 | | |
| |||
5805 | 5865 | | |
5806 | 5866 | | |
5807 | 5867 | | |
| 5868 | + | |
| 5869 | + | |
| 5870 | + | |
| 5871 | + | |
| 5872 | + | |
| 5873 | + | |
| 5874 | + | |
| 5875 | + | |
| 5876 | + | |
| 5877 | + | |
| 5878 | + | |
| 5879 | + | |
| 5880 | + | |
| 5881 | + | |
5808 | 5882 | | |
5809 | 5883 | | |
5810 | 5884 | | |
| |||
7266 | 7340 | | |
7267 | 7341 | | |
7268 | 7342 | | |
| 7343 | + | |
| 7344 | + | |
| 7345 | + | |
7269 | 7346 | | |
7270 | 7347 | | |
7271 | 7348 | | |
| |||
8079 | 8156 | | |
8080 | 8157 | | |
8081 | 8158 | | |
| 8159 | + | |
| 8160 | + | |
| 8161 | + | |
| 8162 | + | |
| 8163 | + | |
| 8164 | + | |
| 8165 | + | |
| 8166 | + | |
8082 | 8167 | | |
8083 | 8168 | | |
8084 | 8169 | | |
| |||
8121 | 8206 | | |
8122 | 8207 | | |
8123 | 8208 | | |
| 8209 | + | |
| 8210 | + | |
| 8211 | + | |
8124 | 8212 | | |
8125 | 8213 | | |
8126 | 8214 | | |
| |||
8167 | 8255 | | |
8168 | 8256 | | |
8169 | 8257 | | |
| 8258 | + | |
| 8259 | + | |
| 8260 | + | |
8170 | 8261 | | |
8171 | 8262 | | |
8172 | 8263 | | |
| |||
8209 | 8300 | | |
8210 | 8301 | | |
8211 | 8302 | | |
| 8303 | + | |
| 8304 | + | |
| 8305 | + | |
8212 | 8306 | | |
8213 | 8307 | | |
8214 | 8308 | | |
| |||
8251 | 8345 | | |
8252 | 8346 | | |
8253 | 8347 | | |
| 8348 | + | |
| 8349 | + | |
| 8350 | + | |
8254 | 8351 | | |
8255 | 8352 | | |
8256 | 8353 | | |
| |||
8295 | 8392 | | |
8296 | 8393 | | |
8297 | 8394 | | |
| 8395 | + | |
| 8396 | + | |
| 8397 | + | |
8298 | 8398 | | |
8299 | 8399 | | |
8300 | 8400 | | |
| |||
8337 | 8437 | | |
8338 | 8438 | | |
8339 | 8439 | | |
| 8440 | + | |
| 8441 | + | |
| 8442 | + | |
8340 | 8443 | | |
8341 | 8444 | | |
8342 | 8445 | | |
| |||
8485 | 8588 | | |
8486 | 8589 | | |
8487 | 8590 | | |
| 8591 | + | |
| 8592 | + | |
| 8593 | + | |
| 8594 | + | |
| 8595 | + | |
| 8596 | + | |
| 8597 | + | |
| 8598 | + | |
| 8599 | + | |
8488 | 8600 | | |
8489 | 8601 | | |
8490 | 8602 | | |
| |||
9837 | 9949 | | |
9838 | 9950 | | |
9839 | 9951 | | |
| 9952 | + | |
| 9953 | + | |
| 9954 | + | |
9840 | 9955 | | |
9841 | 9956 | | |
9842 | 9957 | | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments