Skip to content

Implement EnumCount Derive Macro #1548

@Wandalen

Description

@Wandalen

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

  1. Functionality: Count variants for all enum types; error if not an enum.
  2. Attributes: #[enum_count(debug)], #[enum_count(const_name = "CUSTOM_NAME")].
  3. Integration: Implement in derive_tools_meta, expose via derive_tools. Add derive_enum_count feature flag. Update build.rs.
  4. Testing: Full tests (manual, derive, shared, compile-fail) in derive_tools/tests/inc/enum_count/.
  5. Docs: Rustdoc, Readme, examples.
  6. 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:

  1. Manual Code: First, write the code you expect the macro to generate by hand. Put this in tests/inc/enum_count/*_manual.rs.
  2. 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.
  3. 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.
  4. Derive Site: Create a *_derive.rs file. Use the same enum structure, but this time apply #[derive(EnumCount)]. Also include! the shared test here.
  5. Implement Macro: Now, write or update the EnumCount macro logic in derive_tools_meta so it generates code that matches your validated manual version.
  6. 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.
  7. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions