Procedural macro: auto generating forms

Consider the problem of generalizing Display/Debug. Given a struct Foo, we want to auto derive some GUI for editing struct Foo, where ATMOST 1 field can be "broken" at any time.

From a type perspective, we are given:

pub struct Foo {
    a: A,
    b: B,
    c: C,
    d: D,

and we want to generate something like:

pub enum Gen_Foo {

    Edit_A { a: Gen_A, b: B, c: C, d: D },
    Edit_B { a: A, b: Gen_B, c: C, d: D },
    Edit_C { a: A, b: B, c: Gen_C, d: D },
    Edit_D { a: A, b: B, c: C, d: Gen_D },

The four Done_* arms are to indicate "the Struct is in a valid state, and we are selecting one of the arms". The Edit_* fields are to indicate: the non-editing fields are valid, the edited field may or may not be valid.

Question: is there a name for this technique / pattern ? Is there a library / procedural macro that already done this?

It reminds me (slightly) of the builder pattern, so taking a look at derive_builder src might be inspiring if you go down the route of writing your own proc macro.

The linked crate uses Option instead of a single enum, so multiple fields will be able to be missing.

Presumably you have a way to start with all 4 fields done before allowing only one to be edited at a time.

Your whole approach is novel to me, couldn't name any crate that does this.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.