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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Holds serializable pubsub stuff
//!
//! Use [`TopicSubscribe::to_command`] to send subscription listen and parse the responses with [`Response::parse`]
//! # Notes
//!
//! If you find that a pubsub topic reply has a field that has not yet been added to this crate, and you don't need that field, you can enable the
//! <span
//!   class="module-item stab portability"
//!   style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
//! ><code>allow_unknown_fields</code></span>
//! feature for this crate in your Cargo manifest to ignore it (and other) fields.
//!
//! # Undocumented features
//!
//! This crate has some pubsub topics that are not documented by twitch. These may stop working at any time. To enable these, use feature
//! <span
//!   class="module-item stab portability"
//!   style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
//! ><code>unsupported</code></span>
//! to use them. Note that this crate doesn't try to keep changes to these pubsub topics semver compatible.

static ERROR_TRYFROM: &str = "no match";

/// Implement `From<$type> for String` for serializing and `TryFrom<String> for $type` for deserializing.
macro_rules! impl_de_ser {
    (@field $e:expr) => {".{}"};
    ($type:ident, $fmt:literal, $($field:ident),* $(,)? $(?$opt_field:ident),* $(,)?) => {
        impl From<$type> for String {
            fn from(t: $type) -> Self { format!(concat!($fmt, $(impl_de_ser!(@field $field),)+ $(impl_de_ser!(@field $opt_field),)*), $(t.$field,)*$(t.$opt_field.map(|f| f.to_string()).unwrap_or_default(),)*).trim_end_matches(".").to_owned() }
        }
        impl<'a> From<&'a $type> for String {
            fn from(t: &'a $type) -> Self { format!(concat!($fmt, $(impl_de_ser!(@field $field),)+ $(impl_de_ser!(@field $opt_field),)*), $(t.$field,)*$(t.$opt_field.map(|f| f.to_string()).unwrap_or_default(),)*).trim_end_matches(".").to_owned() }
        }

        impl ::std::convert::TryFrom<String> for $type {
            type Error = &'static str;

            fn try_from(s: String) -> ::std::result::Result<Self, Self::Error> {
                if s.starts_with($fmt) {
                    let sub_s = s.strip_prefix($fmt).ok_or("could not strip str, this should never be hit")?;
                    match sub_s.split('.').collect::<Vec<_>>().as_slice() {
                        ["", $($field,)* $($opt_field,)*] => {
                            Ok($type {
                                $(
                                    $field: $field.parse()
                                            .map_err(|_| concat!("could not parse field <", stringify!($field), ">"))?,
                                )*
                                $(
                                    $opt_field: Some($opt_field.parse()
                                            .map_err(|_| concat!("could not parse field <", stringify!($opt_field), ">"))?),
                                )*
                            } )
                        }
                        #[allow(unreachable_patterns)]
                        ["", $($field,)*] => {
                            Ok($type {
                                $(
                                    $field: $field.parse()
                                            .map_err(|_| concat!("could not parse field <", stringify!($field), ">"))?,
                                )*
                                $(
                                    $opt_field: None,
                                )*
                            } )
                        }
                        _ => Err(crate::pubsub::ERROR_TRYFROM)
                    }
                } else {
                    Err(crate::pubsub::ERROR_TRYFROM)
                }
            }
        }
    };
}

use serde::{Deserialize, Deserializer, Serialize};
pub mod channel_bits;
pub mod channel_bits_badge;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod channel_cheer;
pub mod channel_points;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod channel_sub_gifts;
pub mod channel_subscriptions;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod community_points;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod following;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod hypetrain;
pub mod moderation;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod raid;
#[cfg(feature = "unsupported")]
#[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
pub mod video_playback;

/// A logical partition of messages that clients may subscribe to, to get messages.
///
/// also known as event
#[cfg_attr(nightly, doc(spotlight))]
pub trait Topic: Serialize + Into<String> {
    /// Scopes needed by this topic
    ///
    /// This constant
    /// <span
    ///   class="module-item stab portability"
    ///   style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
    /// ><code>unsupported</code></span>
    #[cfg(feature = "twitch_oauth2")]
    #[cfg_attr(nightly, doc(feature = "twitch_oauth2"))]
    const SCOPE: &'static [twitch_oauth2::Scope];
}

/// Command that can be serialized to be sent to twitchs PubSub server to subscribe or unsubscribe to a [Topic]
pub enum TopicSubscribe {
    /// Subscribe/Listen
    Listen {
        /// Random string to identify the response associated with this request.
        nonce: Option<String>,
        /// List of topics to listen on.
        topics: Vec<String>,
        /// OAuth token required to listen on some topics.
        auth_token: String,
    },
    /// Unsubscribe/Unlisten
    Unlisten {
        /// Random string to identify the response associated with this request.
        nonce: Option<String>,
        /// List of topics to listen on.
        topics: Vec<String>,
        /// OAuth token required to listen on some topics.
        auth_token: String,
    },
}

impl Serialize for TopicSubscribe {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: serde::Serializer {
        #[derive(Serialize)]
        struct ITopicSubscribeData<'a> {
            topics: &'a [String],
            auth_token: &'a str,
        }
        #[derive(Serialize)]
        struct ITopicSubscribe<'a> {
            #[serde(rename = "type")]
            _type: &'static str,
            nonce: Option<&'a str>,
            data: ITopicSubscribeData<'a>,
        }

        match self {
            TopicSubscribe::Listen {
                nonce,
                topics,
                auth_token,
            } => ITopicSubscribe {
                _type: "LISTEN",
                nonce: nonce.as_deref(),
                data: ITopicSubscribeData {
                    topics: topics.as_slice(),
                    auth_token,
                },
            }
            .serialize(serializer),
            TopicSubscribe::Unlisten {
                nonce,
                topics,
                auth_token,
            } => ITopicSubscribe {
                _type: "UNLISTEN",
                nonce: nonce.as_deref(),
                data: ITopicSubscribeData {
                    topics: topics.as_slice(),
                    auth_token,
                },
            }
            .serialize(serializer),
        }
    }
}

impl TopicSubscribe {
    /// Convert this [`TopicSubscribe`] to a string which you can send with your client
    pub fn to_command(&self) -> Result<String, serde_json::Error> { serde_json::to_string(&self) }

    /// Create a listen command.
    ///
    /// # Example
    ///
    /// Create a listen message for moderator actions
    ///
    /// ```rust
    /// # use twitch_api2::pubsub;
    /// // We want to subscribe to moderator actions on channel with id 1234
    /// // as if we were a user with id 4321 that is moderator on the channel.
    /// let topic = pubsub::moderation::ChatModeratorActions {
    ///     user_id: 4321,
    ///     channel_id: 1234,
    /// };
    ///
    /// // Create the topic command to send to twitch
    /// let command = pubsub::TopicSubscribe::listen(
    ///     &[topic],
    ///     "authtoken".to_string(),
    ///     "super se3re7 random string".to_string(),
    /// )
    /// .to_command().expect("serializing failed");
    /// // Send the message with your favorite websocket client
    /// send_command(command).unwrap();
    /// // To parse the websocket messages, use pubsub::Response::parse
    /// # fn send_command(command: String) -> Result<(),()> {Ok(())}
    /// ```
    pub fn listen<'a, T: Topic, O: Into<Option<String>>>(
        topics: &'a [T],
        auth_token: String,
        nonce: O,
    ) -> TopicSubscribe
    where
        &'a T: Into<String>,
    {
        TopicSubscribe::Listen {
            nonce: nonce.into(),
            topics: topics.iter().map(|t| t.into()).collect(),
            auth_token,
        }
    }

    /// Create a unlisten command.
    pub fn unlisten<'a, T: Topic, O: Into<Option<String>>>(
        topics: &'a [T],
        auth_token: String,
        nonce: O,
    ) -> TopicSubscribe
    where
        &'a T: Into<String>,
    {
        TopicSubscribe::Unlisten {
            nonce: nonce.into(),
            topics: topics.iter().map(|t| t.into()).collect(),
            auth_token,
        }
    }
}
/// Response from twitch PubSub
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct TwitchResponse {
    /// The nonce that was passed in the request, if one was provided there
    pub nonce: Option<String>,
    /// The error message associated with the request, or an empty string if there is no error
    pub error: Option<String>,
}

impl TwitchResponse {
    /// Whether response indicates success or not
    pub fn is_successful(&self) -> bool { self.error.as_ref().map_or(true, |s| s.is_empty()) }
}

// FIXME: Add example
/// Message response from twitch PubSub.
///
/// See [TwitchResponse]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(remote = "Self")]
pub enum TopicData {
    /// Response from the [channel_bits::ChannelBitsEventsV2] topic.
    ChannelBitsEventsV2 {
        /// Topic message
        topic: channel_bits::ChannelBitsEventsV2,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_bits::ChannelBitsEventsV2Reply>,
    },
    /// Response from the [channel_bits_badge::ChannelBitsBadgeUnlocks] topic.
    ChannelBitsBadgeUnlocks {
        /// Topic message
        topic: channel_bits_badge::ChannelBitsBadgeUnlocks,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_bits_badge::ChannelBitsBadgeUnlocksReply>,
    },
    /// Response from the [moderation::ChatModeratorActions] topic.
    ChatModeratorActions {
        /// Topic message
        topic: moderation::ChatModeratorActions,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<moderation::ChatModeratorActionsReply>,
    },
    /// Response from the [channel_points::ChannelPointsChannelV1] topic.
    ChannelPointsChannelV1 {
        /// Topic message
        topic: channel_points::ChannelPointsChannelV1,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_points::ChannelPointsChannelV1Reply>,
    },
    /// Response from the [channel_subscriptions::ChannelSubscribeEventsV1] topic.
    ChannelSubscribeEventsV1 {
        /// Topic message
        topic: channel_subscriptions::ChannelSubscribeEventsV1,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_subscriptions::ChannelSubscribeEventsV1Reply>, // FIXME: :)
    },
    /// Response from the [community_points::CommunityPointsChannelV1] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    CommunityPointsChannelV1 {
        /// Topic message
        topic: community_points::CommunityPointsChannelV1,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_points::ChannelPointsChannelV1Reply>,
    },
    /// Response from the [channel_cheer::ChannelCheerEventsPublicV1] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    ChannelCheerEventsPublicV1 {
        /// Topic message
        topic: channel_cheer::ChannelCheerEventsPublicV1,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_cheer::ChannelCheerEventsPublicV1Reply>,
    },
    /// Response from the [channel_sub_gifts::ChannelSubGiftsV1] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    ChannelSubGiftsV1 {
        /// Topic message
        topic: channel_sub_gifts::ChannelSubGiftsV1,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<channel_sub_gifts::ChannelSubGiftsV1Reply>,
    },

    /// Response from the [video_playback::VideoPlayback] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    VideoPlayback {
        /// Topic message
        topic: video_playback::VideoPlayback,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<video_playback::VideoPlaybackReply>,
    },
    /// Response from the [video_playback::VideoPlaybackById] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    VideoPlaybackById {
        /// Topic message
        topic: video_playback::VideoPlaybackById,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<video_playback::VideoPlaybackReply>,
    },
    /// Response from the [hypetrain::HypeTrainEventsV1] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    HypeTrainEventsV1 {
        /// Topic message
        topic: hypetrain::HypeTrainEventsV1,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<hypetrain::HypeTrainEventsV1Reply>, // FIXME: May not be correct
    },
    /// Response from the [hypetrain::HypeTrainEventsV1Rewards] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    HypeTrainEventsV1Rewards {
        /// Topic message
        topic: hypetrain::HypeTrainEventsV1Rewards,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<hypetrain::HypeTrainEventsV1Reply>,
    },
    /// Response from the [following::Following] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    Following {
        /// Topic message
        topic: following::Following,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<following::FollowingReply>,
    },
    /// Response from the [raid::Raid] topic.
    #[cfg(feature = "unsupported")]
    #[cfg_attr(nightly, doc(cfg(feature = "unsupported")))]
    Raid {
        /// Topic message
        topic: raid::Raid,
        /// Message reply from topic subscription
        #[serde(rename = "message")]
        reply: Box<raid::RaidReply>,
    },
}

// This impl is here because otherwise we hide the errors from deser
impl<'de> Deserialize<'de> for TopicData {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        // FIXME: make into macro or actually upstream into serde..., untagged_force = "field"

        #[derive(Deserialize, PartialEq, Eq, Debug)]
        #[serde(untagged)]
        enum ITopicMessage {
            #[cfg(feature = "unsupported")]
            CommunityPointsChannelV1(community_points::CommunityPointsChannelV1),
            ChannelBitsEventsV2(channel_bits::ChannelBitsEventsV2),
            ChannelBitsBadgeUnlocks(channel_bits_badge::ChannelBitsBadgeUnlocks),
            #[cfg(feature = "unsupported")]
            ChannelCheerEventsPublicV1(channel_cheer::ChannelCheerEventsPublicV1),
            #[cfg(feature = "unsupported")]
            ChannelSubGiftsV1(channel_sub_gifts::ChannelSubGiftsV1),
            ChatModeratorActions(moderation::ChatModeratorActions),
            ChannelPointsChannelV1(channel_points::ChannelPointsChannelV1),
            ChannelSubscribeEventsV1(channel_subscriptions::ChannelSubscribeEventsV1),
            #[cfg(feature = "unsupported")]
            VideoPlayback(video_playback::VideoPlayback),
            #[cfg(feature = "unsupported")]
            VideoPlaybackById(video_playback::VideoPlaybackById),
            #[cfg(feature = "unsupported")]
            HypeTrainEventsV1(hypetrain::HypeTrainEventsV1),
            #[cfg(feature = "unsupported")]
            HypeTrainEventsV1Rewards(hypetrain::HypeTrainEventsV1Rewards),
            #[cfg(feature = "unsupported")]
            Following(following::Following),
            #[cfg(feature = "unsupported")]
            Raid(raid::Raid),
        }

        #[derive(Deserialize, Debug)]
        struct ITopicData {
            topic: ITopicMessage,
            message: String,
        }
        let reply = ITopicData::deserialize(deserializer).map_err(|e| {
            serde::de::Error::custom(format!("could not deserialize topic reply: {}", e))
        })?;
        Ok(match reply.topic {
            #[cfg(feature = "unsupported")]
            ITopicMessage::CommunityPointsChannelV1(topic) => TopicData::CommunityPointsChannelV1 {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            ITopicMessage::ChannelBitsEventsV2(topic) => TopicData::ChannelBitsEventsV2 {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            ITopicMessage::ChannelBitsBadgeUnlocks(topic) => TopicData::ChannelBitsBadgeUnlocks {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::ChannelSubGiftsV1(topic) => TopicData::ChannelSubGiftsV1 {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::ChannelCheerEventsPublicV1(topic) => {
                TopicData::ChannelCheerEventsPublicV1 {
                    topic,
                    reply: serde_json::from_str(&reply.message)
                        .map_err(serde::de::Error::custom)?,
                }
            }
            ITopicMessage::ChatModeratorActions(topic) => TopicData::ChatModeratorActions {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            ITopicMessage::ChannelPointsChannelV1(topic) => TopicData::ChannelPointsChannelV1 {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            ITopicMessage::ChannelSubscribeEventsV1(topic) => TopicData::ChannelSubscribeEventsV1 {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::VideoPlayback(topic) => TopicData::VideoPlayback {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::VideoPlaybackById(topic) => TopicData::VideoPlaybackById {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::HypeTrainEventsV1(topic) => TopicData::HypeTrainEventsV1 {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::HypeTrainEventsV1Rewards(topic) => TopicData::HypeTrainEventsV1Rewards {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::Following(topic) => TopicData::Following {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
            #[cfg(feature = "unsupported")]
            ITopicMessage::Raid(topic) => TopicData::Raid {
                topic,
                reply: serde_json::from_str(&reply.message).map_err(serde::de::Error::custom)?,
            },
        })
    }
}

/// Response from twitchs PubSub server.
/// Either a response indicating status of something or a message from a topic
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(tag = "type")]
pub enum Response {
    /// Response from a subscription/unsubscription
    #[serde(rename = "RESPONSE")]
    Response(TwitchResponse),
    /// Message received containing all applicable data
    #[serde(rename = "MESSAGE")]
    Message {
        /// Data corresponding to [topic](Topic) message
        data: TopicData,
    },
    /// Response from a ping
    #[serde(rename = "PONG")]
    Pong,
    /// Request for the client to reconnect
    #[serde(rename = "RECONNECT")]
    Reconnect,
}

impl Response {
    // FIXME: Add example
    /// Parse string slice as a response.
    pub fn parse(source: &str) -> Result<Response, serde_json::Error> {
        serde_json::from_str(source)
    }
}

/// Deserialize 'null' as <T as Default>::Default
fn deserialize_default_from_null<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: Deserialize<'de> + Default, {
    Ok(Option::deserialize(deserializer)?.unwrap_or_default())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn error() {
        let source = r#"
{
    "type": "RESPONSE",
    "nonce": "44h1k13746815ab1r2",
    "error": ""
}
"#;
        let expected = Response::Response(TwitchResponse {
            nonce: Some(String::from("44h1k13746815ab1r2")),
            error: Some(String::new()),
        });
        let actual = Response::parse(source).unwrap();
        assert_eq!(expected, actual);
    }
}