statig icon indicating copy to clipboard operation
statig copied to clipboard

State Definition Flexibility

Open christianheussy opened this issue 1 year ago • 1 comments

Current Limitation

Currently, the macro does not support user-defined state enums. This limitation is particularly noticeable when users require fine-grained control over Serde serialization/deserialization, specifically when needing to leverage attributes like skip and default. Workarounds involve either manual implementation or forgoing the macro entirely, which can be cumbersome and less efficient.

Proposed Solution

Introduce a "Bring Your Own State" (BYOS) mechanism. This will allow users to define their own custom State enum by adding a custom_state option to the state attribute within the macro. The macro should enforce that the name item within the state attribute matches the variant names defined in the custom State enum.

struct Foo;

MyState {
    On,
    Off
}

#[state_machine(state(custom_state, name = "MyState"))]
impl Foo {
    #[state(name = "On")]
    fn on() -> Response<MyState> {
        todo!()
    }
    
    #[state(name = "Off")]
    fn on() -> Response<MyState> {
        todo!()
    }
}

christianheussy avatar Jan 21 '25 21:01 christianheussy

This ticket is a good excuse for me to become more familiar with the macro. I'll take a stab at it.

christianheussy avatar Jan 21 '25 21:01 christianheussy

Resolved by #49

mdeloof avatar May 21 '25 21:05 mdeloof

Woot!

christianheussy avatar May 21 '25 21:05 christianheussy