Skip to main content

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