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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use {super::*, crate::*};

/// An enum of all possible Twitch messages.

///

/// This is useful if you just want to subscribe to ***all** messages.

#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub enum Commands<'a> {
    /// An raw event occured

    Raw(IrcMessage<'a>),
    /// A capabilities event occured

    IrcReady(IrcReady<'a>),
    /// A ClearChat event occured

    Ready(Ready<'a>),
    /// A ClearMsg event occured

    Cap(Cap<'a>),
    /// A GlobalUserState event occured

    ClearChat(ClearChat<'a>),
    /// A HostTarget event occured

    ClearMsg(ClearMsg<'a>),
    /// A IrcReady event occured

    GlobalUserState(GlobalUserState<'a>),
    /// A Join event occured

    HostTarget(HostTarget<'a>),
    /// A Notice event occured

    Join(Join<'a>),
    /// A Part event occured

    Notice(Notice<'a>),
    /// A Ping event occured

    Part(Part<'a>),
    /// A Pong event occured

    Ping(Ping<'a>),
    /// A Privmsg event occured

    Pong(Pong<'a>),
    /// A Ready event occured

    Privmsg(Privmsg<'a>),
    /// A Reconnect event occured

    Reconnect(Reconnect<'a>),
    /// A RoomState event occured

    RoomState(RoomState<'a>),
    /// A UserNotice event occured

    UserNotice(UserNotice<'a>),
    /// A UserState event occured

    UserState(UserState<'a>),
    /// A Whisper event occured

    Whisper(Whisper<'a>),
}

impl<'a> Commands<'a> {
    /// Get the raw string out of this

    pub fn raw(&'a self) -> &'a str {
        match self {
            Self::Raw(msg) => msg.get_raw(),
            Self::IrcReady(msg) => msg.raw(),
            Self::Ready(msg) => msg.raw(),
            Self::Cap(msg) => msg.raw(),
            Self::ClearChat(msg) => msg.raw(),
            Self::ClearMsg(msg) => msg.raw(),
            Self::GlobalUserState(msg) => msg.raw(),
            Self::HostTarget(msg) => msg.raw(),
            Self::Join(msg) => msg.raw(),
            Self::Notice(msg) => msg.raw(),
            Self::Part(msg) => msg.raw(),
            Self::Ping(msg) => msg.raw(),
            Self::Pong(msg) => msg.raw(),
            Self::Privmsg(msg) => msg.raw(),
            Self::Reconnect(msg) => msg.raw(),
            Self::RoomState(msg) => msg.raw(),
            Self::UserNotice(msg) => msg.raw(),
            Self::UserState(msg) => msg.raw(),
            Self::Whisper(msg) => msg.raw(),
        }
    }
}

impl<'a> IntoOwned<'a> for Commands<'a> {
    type Output = Commands<'static>;

    fn into_owned(self) -> Self::Output {
        match self {
            Self::Raw(s) => Commands::Raw(s.into_owned()),
            Self::IrcReady(s) => Commands::IrcReady(s.into_owned()),
            Self::Ready(s) => Commands::Ready(s.into_owned()),
            Self::Cap(s) => Commands::Cap(s.into_owned()),
            Self::ClearChat(s) => Commands::ClearChat(s.into_owned()),
            Self::ClearMsg(s) => Commands::ClearMsg(s.into_owned()),
            Self::GlobalUserState(s) => Commands::GlobalUserState(s.into_owned()),
            Self::HostTarget(s) => Commands::HostTarget(s.into_owned()),
            Self::Join(s) => Commands::Join(s.into_owned()),
            Self::Notice(s) => Commands::Notice(s.into_owned()),
            Self::Part(s) => Commands::Part(s.into_owned()),
            Self::Ping(s) => Commands::Ping(s.into_owned()),
            Self::Pong(s) => Commands::Pong(s.into_owned()),
            Self::Privmsg(s) => Commands::Privmsg(s.into_owned()),
            Self::Reconnect(s) => Commands::Reconnect(s.into_owned()),
            Self::RoomState(s) => Commands::RoomState(s.into_owned()),
            Self::UserNotice(s) => Commands::UserNotice(s.into_owned()),
            Self::UserState(s) => Commands::UserState(s.into_owned()),
            Self::Whisper(s) => Commands::Whisper(s.into_owned()),
        }
    }
}

impl<'a> FromIrcMessage<'a> for Commands<'a> {
    type Error = MessageError;

    fn from_irc(msg: IrcMessage<'a>) -> Result<Self, Self::Error> {
        macro_rules! map {
            ($ident:ident) => {
                Self::$ident($ident::from_irc(msg)?)
            };
        }

        use IrcMessage as M;
        let this = match msg.get_command() {
            M::IRC_READY => map!(IrcReady),
            M::READY => map!(Ready),
            M::CAP => map!(Cap),
            M::CLEAR_CHAT => map!(ClearChat),
            M::CLEAR_MSG => map!(ClearMsg),
            M::GLOBAL_USER_STATE => map!(GlobalUserState),
            M::HOST_TARGET => map!(HostTarget),
            M::JOIN => map!(Join),
            M::NOTICE => map!(Notice),
            M::PART => map!(Part),
            M::PING => map!(Ping),
            M::PONG => map!(Pong),
            M::PRIVMSG => map!(Privmsg),
            M::RECONNECT => map!(Reconnect),
            M::ROOM_STATE => map!(RoomState),
            M::USER_NOTICE => map!(UserNotice),
            M::USER_STATE => map!(UserState),
            M::WHISPER => map!(Whisper),
            _ => Self::Raw(IrcMessage::from_irc(msg).expect("infallible conversion")),
        };

        Ok(this)
    }

    /// Consumes this wrapper and returns the raw `MaybeOwned<'a>`

    fn into_inner(self) -> MaybeOwned<'a> {
        match self {
            Self::Raw(msg) => msg.into_inner(),
            Self::IrcReady(msg) => msg.into_inner(),
            Self::Ready(msg) => msg.into_inner(),
            Self::Cap(msg) => msg.into_inner(),
            Self::ClearChat(msg) => msg.into_inner(),
            Self::ClearMsg(msg) => msg.into_inner(),
            Self::GlobalUserState(msg) => msg.into_inner(),
            Self::HostTarget(msg) => msg.into_inner(),
            Self::Join(msg) => msg.into_inner(),
            Self::Notice(msg) => msg.into_inner(),
            Self::Part(msg) => msg.into_inner(),
            Self::Ping(msg) => msg.into_inner(),
            Self::Pong(msg) => msg.into_inner(),
            Self::Privmsg(msg) => msg.into_inner(),
            Self::Reconnect(msg) => msg.into_inner(),
            Self::RoomState(msg) => msg.into_inner(),
            Self::UserNotice(msg) => msg.into_inner(),
            Self::UserState(msg) => msg.into_inner(),
            Self::Whisper(msg) => msg.into_inner(),
        }
    }
}

macro_rules! from_other {
    ($($ident:tt)*) => {
        $(impl<'a> From<$ident<'a>> for Commands<'a> {
            fn from(msg: $ident<'a>) -> Self {
                Self::$ident(msg)
            }
        })*
    };
}

type Raw<'a> = IrcMessage<'a>;

from_other! {
    Raw
    IrcReady
    Ready
    Cap
    ClearChat
    ClearMsg
    GlobalUserState
    HostTarget
    Join
    Notice
    Part
    Ping
    Pong
    Privmsg
    Reconnect
    RoomState
    UserNotice
    UserState
    Whisper
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[cfg(feature = "serde")]
    fn commands_serde() {
        let input = ":test!test@test PRIVMSG #museun :this is a test\r\n";
        crate::serde::round_trip_json::<Commands>(input);
        crate::serde::round_trip_rmp::<Commands>(input);
    }

    #[test]
    fn ensure_const_match() {
        let input = ":test!test@test PRIVMSG #museun :this is a test\r\n";
        let msg = IrcMessage::parse(MaybeOwned::Borrowed(input)).unwrap();
        let all = Commands::from_irc(msg).unwrap();
        assert!(matches!(all, Commands::Privmsg{..}));
    }
}