Search code examples
rustdynamicenums

How to dynamically create an enum based on user input in Rust


I'm making a game that allows you to choose the number of players you're playing against; we use the number of players to create a player turns enum that we then progress through.

enum MetaGamePhase{
    ChoosePlayerCount,
    ConfirmLibrary,
    ChooseFirstTurn,
    InitialDraw,
    MulliganChoice,
    ActiveGame,
    EndGame,
    DeclareWinner,
    None
}

// need to set the contents of this based on the players choosing the player count
enum Turn{
    Player1,
    Player2,
    Player3,
}

enum Action {
    ProgressPlayerTurn
}

fn reducer(mut state: GameState, action: Action) -> GameState {
    match action{
        Action::ProgressPlayerTurn => GameState{
            turn: {
                use Turn::*;
// will also need to set this dynamically based on the enum
                match state.turn {
                    Player1 => Player2,
                    Player2 => Player3,
                    Player3 => Player1,
                }
            },
            ..state
        }
    }
}

struct GameState{
    turn: Turn,
    meta_game_phase: MetaGamePhase
}

How would I do this in rust?


Solution

  • The answer is you can't do that, and you should use a different language feature.