Skip to main content

twitch_irc/message/commands/
join.rs

1use crate::message::IRCMessage;
2use crate::message::commands::{IRCMessageParseExt, ServerMessageParseError};
3use std::convert::TryFrom;
4
5#[cfg(feature = "with-serde")]
6use {serde::Deserialize, serde::Serialize};
7
8/// Message received when you successfully join a channel.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
11pub struct JoinMessage {
12    /// Login name of the channel you joined.
13    pub channel_login: String,
14    /// The login name of the logged in user (the login name of the user that joined the channel,
15    /// which is the logged in user).
16    pub user_login: String,
17
18    /// The message that this `JoinMessage` was parsed from.
19    pub source: IRCMessage,
20}
21
22impl TryFrom<IRCMessage> for JoinMessage {
23    type Error = ServerMessageParseError;
24
25    fn try_from(source: IRCMessage) -> Result<JoinMessage, ServerMessageParseError> {
26        if source.command != "JOIN" {
27            return Err(ServerMessageParseError::MismatchedCommand(Box::new(source)));
28        }
29
30        Ok(JoinMessage {
31            channel_login: source.try_get_channel_login()?.to_owned(),
32            user_login: source.try_get_prefix_nickname()?.to_owned(),
33            source,
34        })
35    }
36}
37
38impl From<JoinMessage> for IRCMessage {
39    fn from(msg: JoinMessage) -> IRCMessage {
40        msg.source
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use crate::message::{IRCMessage, JoinMessage};
47    use std::convert::TryFrom;
48
49    #[test]
50    pub fn test_basic() {
51        let src = ":randers811!randers811@randers811.tmi.twitch.tv JOIN #pajlada";
52        let irc_message = IRCMessage::parse(src).unwrap();
53        let msg = JoinMessage::try_from(irc_message.clone()).unwrap();
54
55        assert_eq!(
56            msg,
57            JoinMessage {
58                channel_login: "pajlada".to_owned(),
59                user_login: "randers811".to_owned(),
60                source: irc_message
61            }
62        );
63    }
64}