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
/// IRC Prefix, not really used by Twitch once capabilities are enabled
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Prefix {
    /// User prefix. i.e nick!user@host
    User {
        nick: String,
        user: String,
        host: String,
    },
    /// Server prefix. i.e. tmi.twitch.tv
    Server { host: String },
}

impl Prefix {
    pub(super) fn parse(input: &str) -> Option<Self> {
        if !input.starts_with(':') {
            return None;
        }

        let s = input[1..input.find(' ').unwrap_or_else(|| input.len())].trim();
        match s.find('!') {
            Some(pos) => {
                let at = s.find('@')?;
                Some(Prefix::User {
                    nick: s[..pos].to_string(),
                    user: s[pos + 1..at].to_string(),
                    host: s[at + 1..].to_string(),
                })
            }
            None => Some(Prefix::Server {
                host: s.to_string(),
            }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn parse_user() {
        let input = ":museun_32[asdf]!~this_is_a_user@0xDEADBEEF.irc.local.1234.host";
        assert_eq!(
            Prefix::parse(&input).unwrap(),
            Prefix::User {
                nick: "museun_32[asdf]".into(),
                user: "~this_is_a_user".into(),
                host: "0xDEADBEEF.irc.local.1234.host".into()
            }
        )
    }

    #[test]
    fn parse_missing() {
        let input = "no_leading_colon";
        assert_eq!(Prefix::parse(&input), None)
    }

    #[test]
    fn parse_server() {
        let input = ":jtv";
        assert_eq!(
            Prefix::parse(&input).unwrap(),
            Prefix::Server { host: "jtv".into() }
        );
        let input = ":tmi.twitch.tv";
        assert_eq!(
            Prefix::parse(&input).unwrap(),
            Prefix::Server {
                host: "tmi.twitch.tv".into()
            }
        );
        let input = ":irc.some.server.local.domain";
        assert_eq!(
            Prefix::parse(&input).unwrap(),
            Prefix::Server {
                host: "irc.some.server.local.domain".into()
            }
        );
    }
}