Enums in Minitab

Our enum type ServerState has an underlying int type.

enum ServerState {
    StateIdle,
    StateConnected,
    StateError,
    StateRetrying,
}

impl std::fmt::Display for ServerState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let state_name = match self {
            ServerState::StateIdle => "idle",
            ServerState::StateConnected => "connected",
            ServerState::StateError => "error",
            ServerState::StateRetrying => "retrying",
        };
        write!(f, "{}", state_name)
    }
}

fn transition(state: ServerState) -> ServerState {
    match state {
        ServerState::StateIdle => ServerState::StateConnected,
        ServerState::StateConnected | ServerState::StateRetrying => ServerState::StateIdle,
        ServerState::StateError => ServerState::StateError,
        _ => panic!("unknown state: {}", state),
    }
}

fn main() {
    let ns = transition(ServerState::StateIdle);
    println!("{}", ns);

    let ns2 = transition(ns);
    println!("{}", ns2);
}

To run the program, compile the code using rustc and execute the binary.

$ rustc enums.rs
$ ./enums
connected
idle