zero_trust_rps/common/rps/
move_kind.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::common::message::ClientMessage;

use super::state::{RpsState, UserMoveMethods};

#[repr(u8)]
pub enum UserMoveKind {
    JoinRoom,
    Play,
    ConfirmPlay,
    BackToRoom,
}

impl UserMoveMethods for UserMoveKind {
    #[inline]
    fn allowed_state(&self) -> RpsState {
        use UserMoveKind::*;
        match self {
            JoinRoom => RpsState::BeforeRoom,
            Play => RpsState::InRoom,
            ConfirmPlay => RpsState::Played,
            BackToRoom => RpsState::Confirmed,
        }
    }

    #[inline]
    fn resulting_state(&self) -> RpsState {
        use UserMoveKind::*;
        match self {
            JoinRoom => RpsState::InRoom,
            Play => RpsState::Played,
            ConfirmPlay => RpsState::Confirmed,
            BackToRoom => RpsState::InRoom,
        }
    }
}

impl UserMoveKind {
    #[allow(dead_code)]
    pub fn from_client_message(message: &ClientMessage) -> Option<UserMoveKind> {
        message.try_into().ok()
    }
}

impl TryFrom<&ClientMessage> for UserMoveKind {
    type Error = ();

    #[inline]
    fn try_from(value: &ClientMessage) -> Result<Self, Self::Error> {
        use ClientMessage::*;
        match value {
            Ping { c: _ } => Err(()),
            Join {
                room_id: _,
                user: _,
            } => Ok(UserMoveKind::JoinRoom),
            Play { value: _, round: _ } => Ok(UserMoveKind::Play),
            ConfirmPlay(_) => Ok(UserMoveKind::ConfirmPlay),
            RoundFinished { round_id: _ } => Ok(UserMoveKind::BackToRoom),
        }
    }
}