twilight_model/gateway/presence/
activity_type.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
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum ActivityType {
    Playing,
    Streaming,
    Listening,
    Watching,
    Custom,
    Competing,
    Unknown(u8),
}

impl From<u8> for ActivityType {
    fn from(value: u8) -> Self {
        match value {
            0 => ActivityType::Playing,
            1 => ActivityType::Streaming,
            2 => ActivityType::Listening,
            3 => ActivityType::Watching,
            4 => ActivityType::Custom,
            5 => ActivityType::Competing,
            unknown => ActivityType::Unknown(unknown),
        }
    }
}

impl From<ActivityType> for u8 {
    fn from(value: ActivityType) -> Self {
        match value {
            ActivityType::Playing => 0,
            ActivityType::Streaming => 1,
            ActivityType::Listening => 2,
            ActivityType::Watching => 3,
            ActivityType::Custom => 4,
            ActivityType::Competing => 5,
            ActivityType::Unknown(unknown) => unknown,
        }
    }
}

impl Default for ActivityType {
    fn default() -> Self {
        Self::Playing
    }
}

#[cfg(test)]
mod tests {
    use super::ActivityType;
    use serde_test::Token;

    #[test]
    fn default() {
        assert_eq!(ActivityType::Playing, ActivityType::default());
    }

    #[test]
    fn variants() {
        serde_test::assert_tokens(&ActivityType::Playing, &[Token::U8(0)]);
        serde_test::assert_tokens(&ActivityType::Streaming, &[Token::U8(1)]);
        serde_test::assert_tokens(&ActivityType::Listening, &[Token::U8(2)]);
        serde_test::assert_tokens(&ActivityType::Watching, &[Token::U8(3)]);
        serde_test::assert_tokens(&ActivityType::Custom, &[Token::U8(4)]);
        serde_test::assert_tokens(&ActivityType::Competing, &[Token::U8(5)]);
        serde_test::assert_tokens(&ActivityType::Unknown(99), &[Token::U8(99)]);
    }
}