Goal
- We're adding a
#[derive(EnumCount)] macro to derive_tools.
- It will stick a
const COUNT: usize on enums, telling you how many variants they have. The name COUNT can be changed with #[enum_count(const_name = "YOUR_NAME_HERE")].
- This macro is strictly for enums; it should cause a compile error if used on anything else.
- We'll also add an
#[enum_count(debug)] option to print out the code it generates, which is handy for development.
- This new derive needs to be wired up in
derive_tools_meta (where the magic happens) and derive_tools (what users see), including a new feature flag derive_enum_count.
- Naturally, it needs good tests (manual vs. derived, and tests that check for compile errors) and documentation.
- We'll stick to the
wTools way of doing proc macros.
Key Requirements
- Functionality: Count variants for all enum types; error if not an enum.
- Attributes:
#[enum_count(debug)], #[enum_count(const_name = "CUSTOM_NAME")].
- Integration: Implement in
derive_tools_meta, expose via derive_tools. Add derive_enum_count feature flag. Update build.rs.
- Testing: Full tests (manual, derive, shared, compile-fail) in
derive_tools/tests/inc/enum_count/.
- Docs: Rustdoc, Readme, examples.
- Branching: All development work and new branches for this task must originate from the
alpha branch of the Wandalen/wTools repository.
Expected Behavior (EnumCount Macro)
- E1: For
enum MyEnum { A, B }, it generates impl MyEnum { pub const COUNT: usize = 2; }.
- E2: For
enum MyEnum {}, it generates impl MyEnum { pub const COUNT: usize = 0; }.
- E3: With
#[enum_count(const_name = "TOTAL_VARIANTS")] enum MyEnum { X }, it generates impl MyEnum { pub const TOTAL_VARIANTS: usize = 1; }.
- E4: If used on
struct MyStruct;, it must cause a compile-time error.
- E5: Using
#[enum_count(debug)] will print the generated code during compilation.
- E6: It should correctly count variants for simple enums, those with data (tuples/structs), and generic enums.
Proc Macro Development Workflow (Mandatory)
Stick to the wTools "Proc Macro: Development Workflow" design rule. For each test case or significant feature variation:
- Manual Code: First, write the code you expect the macro to generate by hand. Put this in
tests/inc/enum_count/*_manual.rs.
- Shared Test: Create a corresponding
*_only_test.rs file. This will have the actual #[test] functions that check if the COUNT (or custom named) constant is correct.
- Test Manual:
include! the shared test in your manual file. Get the user to run it and confirm it passes. This proves your target output is correct.
- Derive Site: Create a
*_derive.rs file. Use the same enum structure, but this time apply #[derive(EnumCount)]. Also include! the shared test here.
- Implement Macro: Now, write or update the
EnumCount macro logic in derive_tools_meta so it generates code that matches your validated manual version.
- Verify: Ask the user to run tests for both the manual and derive versions. If the derive version fails but the manual one passed, print the macro's output (using
debug if needed) and compare it to your manual code to find the bug.
- Compile-Fail Tests: For things like using
EnumCount on a struct, add tests in tests/inc/enum_count/compiletime/ that are supposed to fail compilation. This checks your error handling.
Goal
#[derive(EnumCount)]macro toderive_tools.const COUNT: usizeon enums, telling you how many variants they have. The nameCOUNTcan be changed with#[enum_count(const_name = "YOUR_NAME_HERE")].#[enum_count(debug)]option to print out the code it generates, which is handy for development.derive_tools_meta(where the magic happens) andderive_tools(what users see), including a new feature flagderive_enum_count.wToolsway of doing proc macros.Key Requirements
#[enum_count(debug)],#[enum_count(const_name = "CUSTOM_NAME")].derive_tools_meta, expose viaderive_tools. Addderive_enum_countfeature flag. Updatebuild.rs.derive_tools/tests/inc/enum_count/.alphabranch of theWandalen/wToolsrepository.Expected Behavior (EnumCount Macro)
enum MyEnum { A, B }, it generatesimpl MyEnum { pub const COUNT: usize = 2; }.enum MyEnum {}, it generatesimpl MyEnum { pub const COUNT: usize = 0; }.#[enum_count(const_name = "TOTAL_VARIANTS")] enum MyEnum { X }, it generatesimpl MyEnum { pub const TOTAL_VARIANTS: usize = 1; }.struct MyStruct;, it must cause a compile-time error.#[enum_count(debug)]will print the generated code during compilation.Proc Macro Development Workflow (Mandatory)
Stick to the
wTools"Proc Macro: Development Workflow" design rule. For each test case or significant feature variation:tests/inc/enum_count/*_manual.rs.*_only_test.rsfile. This will have the actual#[test]functions that check if theCOUNT(or custom named) constant is correct.include!the shared test in your manual file. Get the user to run it and confirm it passes. This proves your target output is correct.*_derive.rsfile. Use the same enum structure, but this time apply#[derive(EnumCount)]. Alsoinclude!the shared test here.EnumCountmacro logic inderive_tools_metaso it generates code that matches your validated manual version.debugif needed) and compare it to your manual code to find the bug.EnumCounton a struct, add tests intests/inc/enum_count/compiletime/that are supposed to fail compilation. This checks your error handling.