Implement a Type-safe State machine with error handling

Hi everyone!

I've read a few articles about implementing state machines in Rust.
One of them is Pretty State Machine Patterns in Rust.

I need advice on correctly implementing a type-safe state machine with states that may return an error. I have an abstract example of the state machine with the required functionality.
My StateMachine consists of the two states: StateA and StateB. A transition from StateA to StateB may fail in certain circumstances.

What do you think about such an implementation? I'm sincerely glad about your proposals and improvements.

Also, I'm considering saving this state machine in the database and extracting the current state from the database.

My first thoughts were placing the StateMachine as an enum of all possible states and saving this enum as a string with Serde-rs. However, such implementation causes a lot of code duplication. Do you have any thoughts on how to implement such behavior?

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.