ferrispot 0.4.3

A wrapper for the Spotify Web API
Documentation
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
//! Everything related to users.
//!
//! Contains the three different kinds of users; [PrivateUser], [CurrentUser] and [PublicUser].
//!
//! - [PrivateUser]: contains all available information about the current user authenticated to the API. Retrieved from
//!   the [`current_user_profile`-function](crate::client::ScopedClient::current_user_profile) *if* the application has
//!   been granted the [`user-read-private`-scope](crate::scope::Scope::UserReadPrivate).
//! - [CurrentUser]: contains all non-private information about the current user authenticated to the API. Retrieved
//!   from the [`current_user_profile`-function](crate::client::ScopedClient::current_user_profile) *if* the application
//!   has been granted the [`user-read-email`-scope](crate::scope::Scope::UserReadEmail), but *not* the
//!   [`user-read-private`-scope](crate::scope::Scope::UserReadPrivate).
//! - [PublicUser]: contains all public information about a user. Retrieved from the
//!   [`user_profile-`function](crate::client::UnscopedClient::user_profile).
//!
//! Additionally, there is the [User] enum that encompasses all three kinds of users.
//!
//! The user object Spotify returns from the API is not directly available. The three user objects, or the [User] enum,
//! may be serialized to get almost all of the original API response back. The model strips certain unnecessary or
//! redundant fields from the response.

mod private {
    use serde::{Deserialize, Serialize};

    use super::{ExplicitContent, Followers};
    use crate::model::{
        id::{Id, UserId},
        object_type::{object_type_serialize, TypeUser},
        CountryCode, ExternalUrls, Image,
    };

    pub(super) trait CommonFields {
        fn common_fields(&self) -> &CommonUserFields;
    }

    pub(super) trait CurrentFields {
        fn current_fields(&self) -> &CurrentUserFields;
    }

    pub(super) trait PrivateFields {
        fn private_fields(&self) -> &PrivateUserFields;
    }

    /// This struct covers all the possible user responses from Spotify's API. It has a function that converts it into
    /// a [User], depending on which fields are set.
    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub struct UserObject {
        /// Fields available in every user.
        #[serde(flatten)]
        pub(crate) common: CommonUserFields,

        /// Fields only in the current user.
        #[serde(flatten)]
        pub(crate) current: Option<CurrentUserFields>,

        /// Fields only in the current private user.
        #[serde(flatten)]
        pub(crate) private: Option<PrivateUserFields>,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub(crate) struct CommonUserFields {
        pub(crate) display_name: Option<String>,
        #[serde(default)]
        pub(crate) external_urls: ExternalUrls,
        pub(crate) followers: Followers,
        pub(crate) id: Id<'static, UserId>,
        #[serde(default)]
        pub(crate) images: Vec<Image>,

        #[serde(rename = "type", with = "object_type_serialize")]
        pub(crate) item_type: TypeUser,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub(crate) struct CurrentUserFields {
        pub(crate) email: String,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub(crate) struct PrivateUserFields {
        pub(crate) country: CountryCode,
        pub(crate) explicit_content: ExplicitContent,
        // TODO: this should really be an enum, but I don't know what all the variants could be. there's at least
        // "premium", "free" aka "open", but there's also something like "family" maybe? "duo", "student"? even
        // more?
        pub(crate) product: String,
    }
}

use serde::{Deserialize, Serialize};

use self::private::{CommonUserFields, CurrentUserFields, PrivateUserFields, UserObject};
use super::{
    id::{Id, UserId},
    CountryCode, ExternalUrls, Image,
};
use crate::{error::ConversionError, prelude::IdTrait};

/// Information about a user's followers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Followers {
    // the API documents a href parameter but says it's always null, so it's not included here
    pub total: u32,
}

/// Information about a user's explicit content settings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExplicitContent {
    /// When `true`, indicates that explicit content should not be played.
    pub filter_enabled: bool,
    /// When `true`, indicates that the explicit content setting is locked and can't be changed by the user.
    pub filter_locked: bool,
}

/// Functions for retrieving information that is common to every user type.
pub trait CommonUserInformation: crate::private::Sealed {
    /// The user's display name if available.
    fn display_name(&self) -> Option<&str>;
    /// The external URLs for the user.
    fn external_urls(&self) -> &ExternalUrls;
    /// Information about the user's followers.
    fn followers(&self) -> Followers;
    /// The user's ID.
    fn id(&self) -> Id<'_, UserId>;
    /// The user's images.
    fn images(&self) -> &[Image];
}

/// Functions for retrieving information only in the current user.
pub trait CurrentUserInformation: crate::private::Sealed {
    /// The user's email.
    fn email(&self) -> &str;
}

/// Functions for retrieving information only in the current private user.
pub trait PrivateUserInformation: crate::private::Sealed {
    /// The user's country.
    fn country(&self) -> CountryCode;
    /// The user's explicit content settings.
    fn explicit_content(&self) -> ExplicitContent;
    /// The user's subscription level.
    fn product(&self) -> &str;
}

impl<T> CommonUserInformation for T
where
    T: private::CommonFields + crate::private::Sealed,
{
    fn display_name(&self) -> Option<&str> {
        self.common_fields().display_name.as_deref()
    }

    fn external_urls(&self) -> &ExternalUrls {
        &self.common_fields().external_urls
    }

    fn followers(&self) -> Followers {
        self.common_fields().followers
    }

    fn id(&self) -> Id<'_, UserId> {
        self.common_fields().id.as_borrowed()
    }

    fn images(&self) -> &[Image] {
        &self.common_fields().images
    }
}

impl<T> CurrentUserInformation for T
where
    T: private::CurrentFields + crate::private::Sealed,
{
    fn email(&self) -> &str {
        &self.current_fields().email
    }
}

impl<T> PrivateUserInformation for T
where
    T: private::PrivateFields + crate::private::Sealed,
{
    fn country(&self) -> CountryCode {
        self.private_fields().country
    }

    fn explicit_content(&self) -> ExplicitContent {
        self.private_fields().explicit_content
    }

    fn product(&self) -> &str {
        &self.private_fields().product
    }
}

/// An enum that encompasses all user types.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(untagged)]
pub enum User {
    Private(PrivateUser),
    Current(CurrentUser),
    Public(PublicUser),
}

/// This struct's only purpose is to make serializing more efficient by holding only references to its data. When
/// attempting to serialize a user object, its fields will be passed as references to this object which is then
/// serialized. This avoids having to clone the entire user in order to reconstruct a UserObject.
#[derive(Serialize)]
struct UserObjectRef<'a> {
    #[serde(flatten)]
    common: &'a CommonUserFields,
    #[serde(flatten)]
    current: Option<&'a CurrentUserFields>,
    #[serde(flatten)]
    private: Option<&'a PrivateUserFields>,
}

/// Private information about the current user. Contains [private information](self::PrivateUserInformation), in
/// addition to all [common](self::CommonUserInformation) and [current](self::CurrentUserInformation) user information
/// about the user.
#[derive(Debug, Clone, Eq, Deserialize)]
#[serde(try_from = "UserObject")]
pub struct PrivateUser {
    common: CommonUserFields,
    current: CurrentUserFields,
    private: PrivateUserFields,
}

/// Public information about the current user. Contains all [common](self::CommonUserInformation) and
/// [current](self::CurrentUserInformation) user information about the user.
#[derive(Debug, Clone, Eq, Deserialize)]
#[serde(try_from = "UserObject")]
pub struct CurrentUser {
    common: CommonUserFields,
    current: CurrentUserFields,
}

/// Public information about a user. Contains only the information [common to every user](self::CommonUserInformation).
#[derive(Debug, Clone, Eq, Deserialize)]
#[serde(try_from = "UserObject")]
pub struct PublicUser {
    common: CommonUserFields,
}

impl PartialEq for PrivateUser {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq for CurrentUser {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq for PublicUser {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq<CurrentUser> for PrivateUser {
    fn eq(&self, other: &CurrentUser) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq<PublicUser> for PrivateUser {
    fn eq(&self, other: &PublicUser) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq<PrivateUser> for CurrentUser {
    fn eq(&self, other: &PrivateUser) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq<PublicUser> for CurrentUser {
    fn eq(&self, other: &PublicUser) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq<PrivateUser> for PublicUser {
    fn eq(&self, other: &PrivateUser) -> bool {
        self.id() == other.id()
    }
}

impl PartialEq<CurrentUser> for PublicUser {
    fn eq(&self, other: &CurrentUser) -> bool {
        self.id() == other.id()
    }
}

impl TryFrom<UserObject> for User {
    type Error = ConversionError;

    fn try_from(obj: UserObject) -> Result<Self, Self::Error> {
        match (obj.current, obj.private) {
            (Some(current), Some(private)) => Ok(Self::Private(PrivateUser {
                common: obj.common,
                current,
                private,
            })),

            (Some(current), None) => Ok(Self::Current(CurrentUser {
                common: obj.common,
                current,
            })),

            (None, None) => Ok(Self::Public(PublicUser { common: obj.common })),

            (current, private) => Err(ConversionError(
                format!(
                    "impossible case trying to convert UserObject into User: current user fields is {current:?} while \
                     private user fields is {private:?}"
                )
                .into(),
            )),
        }
    }
}

impl From<PrivateUser> for User {
    fn from(private: PrivateUser) -> Self {
        Self::Private(private)
    }
}

impl From<CurrentUser> for User {
    fn from(current: CurrentUser) -> Self {
        Self::Current(current)
    }
}

impl From<PublicUser> for User {
    fn from(public: PublicUser) -> Self {
        Self::Public(public)
    }
}

impl TryFrom<User> for PrivateUser {
    type Error = ConversionError;

    fn try_from(user: User) -> Result<Self, Self::Error> {
        match user {
            User::Private(private) => Ok(private),

            User::Current(_) => Err(ConversionError(
                "attempt to convert current user into private user".into(),
            )),

            User::Public(_) => Err(ConversionError(
                "attempt to convert public user into private user".into(),
            )),
        }
    }
}

impl TryFrom<UserObject> for PrivateUser {
    type Error = ConversionError;

    fn try_from(obj: UserObject) -> Result<Self, Self::Error> {
        match (obj.current, obj.private) {
            (Some(current), Some(private)) => Ok(PrivateUser {
                common: obj.common,
                current,
                private,
            }),

            (current, private) => Err(ConversionError(
                format!(
                    "attempt to convert non-private user object into private user (current user fields is \
                     {current:?}, private user fields is {private:?})"
                )
                .into(),
            )),
        }
    }
}

impl TryFrom<User> for CurrentUser {
    type Error = ConversionError;

    fn try_from(user: User) -> Result<Self, Self::Error> {
        match user {
            User::Private(private) => Ok(CurrentUser {
                common: private.common,
                current: private.current,
            }),

            User::Current(current) => Ok(current),

            User::Public(_) => Err(ConversionError(
                "attempt to convert public user into current user".into(),
            )),
        }
    }
}

impl TryFrom<UserObject> for CurrentUser {
    type Error = ConversionError;

    fn try_from(obj: UserObject) -> Result<Self, Self::Error> {
        if let Some(current) = obj.current {
            Ok(CurrentUser {
                common: obj.common,
                current,
            })
        } else {
            Err(ConversionError(
                "attempt to convert public user object into current user".into(),
            ))
        }
    }
}

impl From<User> for PublicUser {
    fn from(user: User) -> Self {
        match user {
            User::Private(private) => PublicUser { common: private.common },
            User::Current(current) => PublicUser { common: current.common },
            User::Public(public) => public,
        }
    }
}

impl From<UserObject> for PublicUser {
    fn from(obj: UserObject) -> Self {
        PublicUser { common: obj.common }
    }
}

impl From<PrivateUser> for UserObject {
    fn from(value: PrivateUser) -> Self {
        Self {
            common: value.common,
            current: Some(value.current),
            private: Some(value.private),
        }
    }
}

impl From<CurrentUser> for UserObject {
    fn from(value: CurrentUser) -> Self {
        Self {
            common: value.common,
            current: Some(value.current),
            private: None,
        }
    }
}

impl From<PublicUser> for UserObject {
    fn from(value: PublicUser) -> Self {
        Self {
            common: value.common,
            current: None,
            private: None,
        }
    }
}

impl crate::private::Sealed for PrivateUser {}
impl crate::private::Sealed for CurrentUser {}
impl crate::private::Sealed for PublicUser {}

impl private::CommonFields for PrivateUser {
    fn common_fields(&self) -> &CommonUserFields {
        &self.common
    }
}

impl private::CommonFields for CurrentUser {
    fn common_fields(&self) -> &CommonUserFields {
        &self.common
    }
}

impl private::CommonFields for PublicUser {
    fn common_fields(&self) -> &CommonUserFields {
        &self.common
    }
}

impl private::CurrentFields for PrivateUser {
    fn current_fields(&self) -> &CurrentUserFields {
        &self.current
    }
}

impl private::CurrentFields for CurrentUser {
    fn current_fields(&self) -> &CurrentUserFields {
        &self.current
    }
}

impl private::PrivateFields for PrivateUser {
    fn private_fields(&self) -> &PrivateUserFields {
        &self.private
    }
}

impl Serialize for User {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            User::Private(private) => private.serialize(serializer),
            User::Current(current) => current.serialize(serializer),
            User::Public(public) => public.serialize(serializer),
        }
    }
}

impl Serialize for PrivateUser {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        UserObjectRef {
            common: &self.common,
            current: Some(&self.current),
            private: Some(&self.private),
        }
        .serialize(serializer)
    }
}

impl Serialize for CurrentUser {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        UserObjectRef {
            common: &self.common,
            current: Some(&self.current),
            private: None,
        }
        .serialize(serializer)
    }
}

impl Serialize for PublicUser {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        UserObjectRef {
            common: &self.common,
            current: None,
            private: None,
        }
        .serialize(serializer)
    }
}

// TODO: unit tests for all the various functions here. deserializing, serializing, equality between users, conversion
// between users