twilight_model/channel/message/
activity.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use serde::{Deserialize, Serialize};

/// Activity associated with a message.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct MessageActivity {
    /// [`MessageActivityType`]
    #[serde(rename = "type")]
    pub kind: MessageActivityType,
    /// ID of the player's party, lobby or group.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub party_id: Option<String>,
}

/// Activity of this message.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum MessageActivityType {
    /// Join the the party.
    Join,
    /// Spectate on or with the party.
    Spectate,
    /// Listen to or with the party.
    Listen,
    /// Request to join the party.
    JoinRequest,
    /// Variant value is unknown to the library.
    Unknown(u8),
}

impl From<u8> for MessageActivityType {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::Join,
            2 => Self::Spectate,
            3 => Self::Listen,
            5 => Self::JoinRequest,
            unknown => Self::Unknown(unknown),
        }
    }
}

impl From<MessageActivityType> for u8 {
    fn from(value: MessageActivityType) -> Self {
        match value {
            MessageActivityType::Join => 1,
            MessageActivityType::Spectate => 2,
            MessageActivityType::Listen => 3,
            MessageActivityType::JoinRequest => 5,
            MessageActivityType::Unknown(unknown) => unknown,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{MessageActivity, MessageActivityType};
    use serde_test::Token;

    #[test]
    fn message_activity() {
        let value = MessageActivity {
            kind: MessageActivityType::Join,
            party_id: None,
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "MessageActivity",
                    len: 1,
                },
                Token::Str("type"),
                Token::U8(1),
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn message_activity_complete() {
        let value = MessageActivity {
            kind: MessageActivityType::Join,
            party_id: Some("test".to_owned()),
        };

        serde_test::assert_tokens(
            &value,
            &[
                Token::Struct {
                    name: "MessageActivity",
                    len: 2,
                },
                Token::Str("type"),
                Token::U8(1),
                Token::Str("party_id"),
                Token::Some,
                Token::Str("test"),
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn variants() {
        serde_test::assert_tokens(&MessageActivityType::Join, &[Token::U8(1)]);
        serde_test::assert_tokens(&MessageActivityType::Spectate, &[Token::U8(2)]);
        serde_test::assert_tokens(&MessageActivityType::Listen, &[Token::U8(3)]);
        serde_test::assert_tokens(&MessageActivityType::JoinRequest, &[Token::U8(5)]);
        serde_test::assert_tokens(&MessageActivityType::Unknown(99), &[Token::U8(99)]);
    }
}