twitch_types/
basic.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
manual_braid! {
    /// A user ID.
    pub struct UserId;
    pub struct UserIdRef;
}
impl_extra!(numeric, UserId, UserIdRef);

manual_braid! {
    /// A users display name
    pub struct DisplayName;
    pub struct DisplayNameRef;
}
impl_extra!(DisplayName, DisplayNameRef);

manual_braid! {
    /// A nickname, not capitalized.
    pub struct Nickname;
    pub struct NicknameRef;
}
impl_extra!(ascii, Nickname, NicknameRef);

/// A username, also specified as login. Should not be capitalized.
pub type UserName = Nickname;

/// A reference to a borrowed [`UserName`], also specified as login. Should not be capitalized.
pub type UserNameRef = NicknameRef;

manual_braid! {
    /// A message ID
    pub struct MsgId;
    pub struct MsgIdRef;
}
impl_extra!(MsgId, MsgIdRef);

/// Broadcaster types: "partner", "affiliate", or "".
#[derive(Copy, Clone, Default, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde_derive::Deserialize))]
pub enum BroadcasterType {
    /// Partner
    #[cfg_attr(feature = "serde", serde(rename = "partner"))]
    Partner,
    /// Affiliate
    #[cfg_attr(feature = "serde", serde(rename = "affiliate"))]
    Affiliate,
    /// None
    #[cfg_attr(feature = "serde", serde(other))]
    #[default]
    None,
}

#[cfg(feature = "serde")]
impl serde::Serialize for BroadcasterType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: serde::Serializer {
        serializer.serialize_str(match self {
            BroadcasterType::Partner => "partner",
            BroadcasterType::Affiliate => "affiliate",
            BroadcasterType::None => "",
        })
    }
}

/// User types: "staff", "admin", "global_mod", or "".
#[derive(Copy, Clone, Default, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde_derive::Deserialize))]

pub enum UserType {
    /// Staff
    #[cfg_attr(feature = "serde", serde(rename = "staff"))]
    Staff,
    /// Admin
    #[cfg_attr(feature = "serde", serde(rename = "admin"))]
    Admin,
    /// Global Moderator
    #[cfg_attr(feature = "serde", serde(rename = "global_mod"))]
    GlobalMod,
    /// None
    #[cfg_attr(feature = "serde", serde(other))]
    #[default]
    None,
}

impl UserType {
    /// Parse a string into a [`UserType`]
    pub fn parse(input: &str) -> Self {
        match input {
            "admin" => Self::Admin,
            "global_mod" => Self::GlobalMod,
            "staff" => Self::Staff,
            _ => Self::None,
        }
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for UserType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: serde::Serializer {
        serializer.serialize_str(match self {
            UserType::Staff => "staff",
            UserType::Admin => "admin",
            UserType::GlobalMod => "global_mod",
            UserType::None => "",
        })
    }
}