Skip to content

Prevent duplicate .id() calls at compile time #66

@TheRedDeveloper

Description

@TheRedDeveloper

Summary

Add compile-time protection to prevent calling .id() multiple times on the same ElementBuilder.

Motivation

When building complex UI patterns, it's easy to accidentally call .id() twice. This bug is silent and hard to catch.

Proposed API

Use type-state pattern with phantom types to enforce single .id() call:

pub struct NoId;
pub struct WithId;

pub fn id(self) -> ElementBuilder<'ply, WithId, CustomElementData> {
    // ...
}

Add a .getId() -> &Id to ElementBuilder<'ply, WithId, CustomElementData> to let the user check the established Id.

Keep other builder methods ambiguous.

Behavior

  • First .id() call transitions the builder's type state to WithId
  • Subsequent .id() calls are compile-time errors
  • .getId() lets the user check the established Id
  • All other builder methods work in both NoId and WithId states
  • Elements without explicit .id() calls continue to work as before

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedA feature that has been approvedfeatNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions