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
//! Wrappers for basic owned types that indicate their usage
#![allow(missing_docs)]

// {{{ macros
#[macro_export]
#[doc(hidden)]
macro_rules! field_wrapper_name {
    ($($type:ty => $field:expr),+) => {
        $(
            impl FieldValue for $type {
                fn field_name() -> &'static str {
                    $field
                }
            }
        )*
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! quick_deref_into {
    ($(($type:ty, $inner:ty)),+) => {
        $(
            impl std::ops::Deref for $type {
                type Target = $inner;
                fn deref(&self) -> &$inner {
                    &self.0
                }
            }

            impl std::ops::DerefMut for $type {
                fn deref_mut(&mut self) -> &mut $inner {
                    &mut self.0
                }
            }

            impl $type {
                /// Take the inner value of this object
                pub fn into_inner(self) -> $inner {
                    self.0
                }
            }

        )*
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! from_inner {
    ($(($type:ty, $inner:ty)),+) => {
        $(
            impl<T: Into<$inner>> From<T> for $type {
                fn from(f: T) -> Self {
                    Self(f.into())
                }
            }
        )*
    }
}

// }}}

use serde::{Deserialize, Serialize};

/// Values for broadcaster objects and requests
pub mod broadcasters {
    use super::*;

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The id number of a broadcaster object
    pub struct BroadcasterId(String);

    use super::users::UserId;
    impl From<UserId> for BroadcasterId {
        fn from(u: UserId) -> Self {
            Self(u.into_inner())
        }
    }

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The display name of a channel object
    pub struct BroadcasterName(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The language of a broadcaster object
    pub struct BroadcasterLanguage(ISOLanguage);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The type of broadcaster: Affiliate, Partner, or empty
    pub struct BroadcasterType(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The total number of views on this broadcasters channel
    pub struct BroadcasterViews(u64);

    field_wrapper_name![
        BroadcasterId => "broadcaster_id",
        BroadcasterName => "broadcaster_name",
        BroadcasterLanguage => "broadcaster_language",
        BroadcasterType => "broadcaster_type",
        BroadcasterViews => "view_count"
    ];

    quick_deref_into![
        (BroadcasterId, String),
        (BroadcasterName, String),
        (BroadcasterLanguage, ISOLanguage),
        (BroadcasterType, String),
        (BroadcasterViews, u64)
    ];

    from_inner![
        (BroadcasterId, String),
        (BroadcasterName, String),
        (BroadcasterLanguage, ISOLanguage),
        (BroadcasterType, String),
        (BroadcasterViews, u64)
    ];
}

/// Values for game objects and requests
pub mod games {
    use super::*;

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct GameName(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The ID number of a game on twitch
    pub struct GameId(String);

    field_wrapper_name![
        GameName => "game_name",
        GameId => "game_id"
    ];

    quick_deref_into![(GameName, String), (GameId, String)];
    from_inner![(GameName, String), (GameId, String)];
}

/// Values for extension objects and requests
pub mod extensions {
    use super::*;

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The id assined to an extension when it was created
    pub struct ExtensionId(String);

    field_wrapper_name![
        ExtensionId => "extension_id"
    ];

    quick_deref_into![(ExtensionId, String)];
    from_inner![(ExtensionId, String)];
}

/// Values for clip objects and requests
pub mod clips {
    use super::*;

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct ClipId(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct ClipTitle(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct ViewCount(u32);

    field_wrapper_name![
        ClipId => "id",
        ClipTitle => "title",
        ViewCount => "view_count"
    ];

    quick_deref_into![(ClipId, String), (ClipTitle, String), (ViewCount, u32)];

    from_inner![(ClipId, String), (ClipTitle, String), (ViewCount, u32)];
}

/// Values for user objects and requests
pub mod users {
    use super::*;

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct UserId(String);

    use super::broadcasters::BroadcasterId;
    impl From<BroadcasterId> for UserId {
        fn from(b: BroadcasterId) -> Self {
            Self(b.into_inner())
        }
    }

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct UserName(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// the login name for this user
    pub struct UserLogin(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The type of this user: Staff, Admin, Global_Mod, or empty
    pub struct UserType(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    /// The email of the user, generally only returned by a request if the access token
    /// provided has the [`scope`] 'user:read:email'
    ///
    /// [`scope`]: https://dev.twitch.tv/docs/authentication#scopes
    pub struct UserEmail(String);

    field_wrapper_name![
        UserId => "id",
        UserName => "user_name",
        UserLogin => "login",
        UserType => "type",
        UserEmail => "email"
    ];

    quick_deref_into![
        (UserId, String),
        (UserName, String),
        (UserLogin, String),
        (UserType, String),
        (UserEmail, String)
    ];

    from_inner![
        (UserId, String),
        (UserName, String),
        (UserLogin, String),
        (UserType, String),
        (UserEmail, String)
    ];
}

pub mod videos {
    use super::*;

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct VideoId(String);

    #[repr(transparent)]
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    #[serde(transparent)]
    pub struct VideoLanguage(ISOLanguage);

    field_wrapper_name![
        VideoId => "id",
        VideoLanguage => "language"
    ];

    quick_deref_into![(VideoId, String), (VideoLanguage, ISOLanguage)];
    from_inner![(VideoId, String), (VideoLanguage, ISOLanguage)];
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// A Pagination key for enpoints that may return more than 100 results
pub struct Pagination {
    pub cursor: Option<String>,
}

impl From<String> for Pagination {
    fn from(inner: String) -> Self {
        Self {
            cursor: Some(inner),
        }
    }
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// The max amount returned per page, used in requests like
/// [`crate::resource::clips::get_clips::GetClipsRequest`]
pub struct Count(u32);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// Represents a time window
pub struct Period {
    pub started_at: StartedAt,
    pub ended_at: EndedAt,
}

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents a [`RFC3339`] formatted datetime
///
/// [`RFC3339`]: https://datatracker.ietf.org/doc/rfc3339/
pub struct RFC3339Time(String);

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents the beginning of a time window
pub struct StartedAt(RFC3339Time);

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents the end of a time window
pub struct EndedAt(RFC3339Time);

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents a language, either a [`ISO 639-1`] two-letter language code or 'other'
///
/// [`ISO 639-1`]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
pub struct ISOLanguage(String);

#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Url(String);

field_wrapper_name![
    Pagination => "pagination",
    Count => "count",

    RFC3339Time => "time",
    Period => "period",
    StartedAt => "started_at",
    EndedAt => "ended_at",
    ISOLanguage => "language"
];

quick_deref_into![
    (RFC3339Time, String),
    (StartedAt, RFC3339Time),
    (EndedAt, RFC3339Time),
    (ISOLanguage, String),
    (Url, String)
];

from_inner![
    (RFC3339Time, String),
    (StartedAt, RFC3339Time),
    (EndedAt, RFC3339Time),
    (ISOLanguage, String),
    (Url, String)
];

/// Used to indicated that this type is used a field value
pub trait FieldValue {
    /// Get the commonly used name of a field of this type that twitch is expecting
    fn field_name() -> &'static str;
}