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
use super::*;

/// Sent on successful login, if TAGs caps have been sent beforehand
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GlobalUserState {
    /// IRC tags
    pub tags: Tags,
}

impl GlobalUserState {
    /// List of badges your user has
    pub fn badges(&self) -> Vec<Badge> {
        badges(self.get("badges").unwrap_or_default())
    }
    /// Your color, if set
    pub fn color(&self) -> Option<TwitchColor> {
        self.get("color").map(RGB::from_hex).map(Into::into)
    }
    /// Your dusplay name, if set
    pub fn display_name(&self) -> Option<&str> {
        self.get("display-name")
    }
    /// Your emote sets
    pub fn emote_sets(&self) -> Vec<u64> {
        self.get("emote-sets")
            .map(|s| {
                s.split_terminator(',')
                    .filter_map(|d| d.parse().ok())
                    .collect()
            })
            .unwrap_or_default()
    }
    /// Your user id
    pub fn user_id(&self) -> u64 {
        self.get_parsed("user-id").unwrap()
    }
}

impl Tag for GlobalUserState {
    fn get(&self, key: &str) -> Option<&str> {
        self.tags.get(key).map(AsRef::as_ref)
    }
}