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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Everything related to albums.
//!
//! Contains the three different kinds of albums; [FullAlbum], [PartialAlbum] and [LocalAlbum].
//!
//! - [FullAlbum]: may contain all possible information about an album. Generally retrieved from the album- and
//!   albums-endpoints (TODO: make links once implemented)
//! - [PartialAlbum]: contains most information about an album. Generally retrieved as part of a response to, for
//!   example, an artist listing (TODO: make a link to the artist endpoint once it exists).
//! - [LocalAlbum]: contains only the basic information about an album. Only retrieved through a playlist that contains
//!   local tracks.
//!
//! Additionally, there is the [Album] enum that encompasses all three kinds of albums.
//!
//! The album object Spotify returns from the API is not directly available. The three album objects, or the [Album]
//! 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.
//!
//! # Album equality
//!
//! Two albums are considered equal when their Spotify IDs are the same. However, since [LocalAlbum] doesn't have a
//! Spotify ID, it resorts to comparing all available fields.

mod private {
    use std::collections::HashSet;

    use serde::{Deserialize, Serialize};

    use crate::model::{
        album::{AlbumTracks, AlbumType},
        artist::PartialArtist,
        id::{AlbumId, Id},
        object_type::{object_type_serialize, TypeAlbum},
        Copyright, CountryCode, DatePrecision, ExternalIds, ExternalUrls, Image, Restrictions,
    };

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

    pub(super) trait FullFields {
        fn full_fields(&self) -> &FullAlbumFields;
    }

    pub(super) trait NonLocalFields {
        fn non_local_fields(&self) -> &NonLocalAlbumFields;
    }

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

        /// Fields only in non-local albums
        #[serde(flatten)]
        pub(crate) non_local: Option<NonLocalAlbumFields>,

        /// Fields only in full albums
        #[serde(flatten)]
        pub(crate) full: Option<FullAlbumFields>,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub(crate) struct CommonAlbumFields {
        // basic information
        pub(crate) name: String,
        pub(crate) artists: Vec<PartialArtist>,
        pub(crate) images: Vec<Image>,
        #[serde(default)]
        pub(crate) external_urls: ExternalUrls,
        #[serde(rename = "type", with = "object_type_serialize")]
        pub(crate) item_type: TypeAlbum,

        // track relinking
        #[serde(default)]
        pub(crate) available_markets: HashSet<CountryCode>,
        #[serde(default)]
        pub(crate) restrictions: Restrictions,
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub(crate) struct FullAlbumFields {
        pub(crate) copyrights: Vec<Copyright>,
        pub(crate) external_ids: ExternalIds,
        pub(crate) genres: Vec<String>,
        pub(crate) label: String,
        pub(crate) popularity: u32,
        pub(crate) tracks: AlbumTracks,
        // TODO: the artist album thing with the album group field
    }

    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
    pub(crate) struct NonLocalAlbumFields {
        pub(crate) album_type: AlbumType,
        pub(crate) id: Id<'static, AlbumId>,
        pub(crate) release_date: String, // TODO: proper date type pls
        pub(crate) release_date_precision: DatePrecision,
    }
}

use std::{collections::HashSet, marker::PhantomData};

use serde::{Deserialize, Serialize, Serializer};

pub(crate) use self::private::{AlbumObject, CommonAlbumFields, FullAlbumFields, NonLocalAlbumFields};
use super::{
    artist::PartialArtist,
    country_code::CountryCode,
    id::{AlbumId, Id, IdTrait},
    page::{Page, PageInformation, PageObject},
    track::{PartialTrack, TrackObject},
    Copyright, DatePrecision, ExternalIds, ExternalUrls, Image, Restrictions,
};
use crate::error::ConversionError;

/// Functions for retrieving information that is common to every album type.
pub trait CommonAlbumInformation: crate::private::Sealed {
    /// The album's name.
    fn name(&self) -> &str;
    /// The artists of the album.
    fn artists(&self) -> &[PartialArtist];
    /// The images for the album.
    fn images(&self) -> &[Image];
    /// The external URLs for the album.
    fn external_urls(&self) -> &ExternalUrls;
    /// The countries the album is available in.
    fn available_markets(&self) -> &HashSet<CountryCode>;
    /// The restrictions on the album.
    fn restrictions(&self) -> &Restrictions;
}

/// Functions for retrieving information only in full albums.
pub trait FullAlbumInformation: crate::private::Sealed {
    // TODO: the artist album thing with the album group field

    /// The tracks in the album.
    fn tracks(&self) -> Page<AlbumTracks, PartialTrack>;
    /// The album's copyrights.
    fn copyrights(&self) -> &[Copyright];
    /// The external IDs for the album.
    fn external_ids(&self) -> &ExternalIds;
    /// The album's genres.
    fn genres(&self) -> &[String];
    /// The album's label.
    fn label(&self) -> &str;
    /// The album's popularity.
    fn popularity(&self) -> u32;
}

/// Functions for retrieving information that is available in non-local albums.
pub trait NonLocalAlbumInformation: crate::private::Sealed {
    /// The album's type.
    fn album_type(&self) -> AlbumType;
    /// The album's Spotify ID.
    fn id(&self) -> Id<'_, AlbumId>;
    /// The album's release date.
    fn release_date(&self) -> &str;
    /// The album's release date's precision.
    fn release_date_precision(&self) -> DatePrecision;
}

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

    fn artists(&self) -> &[PartialArtist] {
        &self.common_fields().artists
    }

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

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

    fn available_markets(&self) -> &HashSet<CountryCode> {
        &self.common_fields().available_markets
    }

    fn restrictions(&self) -> &Restrictions {
        &self.common_fields().restrictions
    }
}

impl<T> FullAlbumInformation for T
where
    T: private::FullFields + crate::private::Sealed,
{
    fn tracks(&self) -> Page<AlbumTracks, PartialTrack> {
        Page {
            inner: self.full_fields().tracks.clone(),
            phantom: PhantomData,
        }
    }

    fn copyrights(&self) -> &[Copyright] {
        &self.full_fields().copyrights
    }

    fn external_ids(&self) -> &ExternalIds {
        &self.full_fields().external_ids
    }

    fn genres(&self) -> &[String] {
        &self.full_fields().genres
    }

    fn label(&self) -> &str {
        &self.full_fields().label
    }

    fn popularity(&self) -> u32 {
        self.full_fields().popularity
    }
}

impl<T> NonLocalAlbumInformation for T
where
    T: private::NonLocalFields + crate::private::Sealed,
{
    fn album_type(&self) -> AlbumType {
        self.non_local_fields().album_type
    }

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

    fn release_date(&self) -> &str {
        &self.non_local_fields().release_date
    }

    fn release_date_precision(&self) -> DatePrecision {
        self.non_local_fields().release_date_precision
    }
}

/// An enum that encompasses all album types.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub enum Album {
    Full(Box<FullAlbum>),
    Partial(Box<PartialAlbum>),
    Local(Box<LocalAlbum>),
}

/// This struct's only purpose is to make serializing more efficient by holding only references to its data. When
/// attempting to serialize an album object, its fields will be passed as references to this object which is then
/// serialized. This avoids having to clone the entire album in order to reconstruct a AlbumObject.
#[derive(Serialize)]
struct AlbumObjectRef<'a> {
    #[serde(flatten)]
    common: &'a CommonAlbumFields,
    #[serde(flatten)]
    non_local: Option<&'a NonLocalAlbumFields>,
    #[serde(flatten)]
    full: Option<&'a FullAlbumFields>,
}

/// A page of tracks in an album.
///
/// This object is retrieved only through the [tracks](FullAlbumInformation::tracks)-function. You won't be interacting
/// objects of this type directly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[doc(hidden)]
pub struct AlbumTracks {
    #[serde(flatten)]
    page: PageObject<TrackObject>,
}

/// A full album. Contains [full information](self::FullAlbumInformation), in addition to all
/// [common](self::CommonAlbumInformation) and [non-local](self::NonLocalAlbumInformation) information about an album.
#[derive(Debug, Clone, Eq, Deserialize)]
#[serde(try_from = "AlbumObject")]
pub struct FullAlbum {
    common: CommonAlbumFields,
    non_local: NonLocalAlbumFields,
    full: FullAlbumFields,
    // TODO: there's a total_tracks field in I think common fields but make sure anyways and add it
}

/// A partial album. Contains all [common](self::CommonAlbumInformation) and [non-local](self::NonLocalAlbumInformation)
/// information about an album.
#[derive(Debug, Clone, Eq, Deserialize)]
#[serde(try_from = "AlbumObject")]
pub struct PartialAlbum {
    common: CommonAlbumFields,
    non_local: NonLocalAlbumFields,
}

/// A local album. Contains only the information [common to every album](self::CommonAlbumInformation).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(try_from = "AlbumObject")]
pub struct LocalAlbum {
    common: CommonAlbumFields,
}

/// An album's type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlbumType {
    #[serde(alias = "ALBUM")]
    Album,
    #[serde(alias = "SINGLE")]
    Single,
    #[serde(alias = "COMPILATION")]
    Compilation,
}

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

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

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

impl PartialEq<LocalAlbum> for FullAlbum {
    fn eq(&self, other: &LocalAlbum) -> bool {
        self.common == other.common
    }
}

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

impl PartialEq<LocalAlbum> for PartialAlbum {
    fn eq(&self, other: &LocalAlbum) -> bool {
        self.common == other.common
    }
}

impl PartialEq<FullAlbum> for LocalAlbum {
    fn eq(&self, other: &FullAlbum) -> bool {
        self.common == other.common
    }
}

impl PartialEq<PartialAlbum> for LocalAlbum {
    fn eq(&self, other: &PartialAlbum) -> bool {
        self.common == other.common
    }
}

impl TryFrom<AlbumObject> for Album {
    type Error = ConversionError;

    fn try_from(obj: AlbumObject) -> Result<Self, Self::Error> {
        match (obj.non_local, obj.full) {
            (Some(non_local), Some(full)) => Ok(Self::Full(Box::new(FullAlbum {
                common: obj.common,
                non_local,
                full,
            }))),

            (Some(non_local), None) => Ok(Self::Partial(Box::new(PartialAlbum {
                common: obj.common,
                non_local,
            }))),

            (None, None) => Ok(Self::Local(Box::new(LocalAlbum { common: obj.common }))),

            (non_local, full) => Err(ConversionError(
                format!(
                    "impossible case trying to convert AlbumObject into Album: non-local album fields is \
                     {non_local:?} while full album fields is {full:?}"
                )
                .into(),
            )),
        }
    }
}

impl From<PartialAlbum> for Album {
    fn from(partial: PartialAlbum) -> Self {
        Self::Partial(Box::new(partial))
    }
}

impl From<FullAlbum> for Album {
    fn from(full: FullAlbum) -> Self {
        Self::Full(Box::new(full))
    }
}

impl From<LocalAlbum> for Album {
    fn from(local: LocalAlbum) -> Self {
        Self::Local(Box::new(local))
    }
}

impl TryFrom<Album> for FullAlbum {
    type Error = ConversionError;

    fn try_from(album: Album) -> Result<Self, Self::Error> {
        match album {
            Album::Full(full) => Ok(*full),

            Album::Partial(_) => Err(ConversionError(
                "attempt to convert partial album into full album".into(),
            )),

            Album::Local(_) => Err(ConversionError("attempt to convert local album into full album".into())),
        }
    }
}

impl TryFrom<AlbumObject> for FullAlbum {
    type Error = ConversionError;

    fn try_from(obj: AlbumObject) -> Result<Self, Self::Error> {
        match (obj.non_local, obj.full) {
            (Some(non_local), Some(full)) => Ok(FullAlbum {
                common: obj.common,
                non_local,
                full,
            }),

            (non_local, full) => Err(ConversionError(
                format!(
                    "attempt to convert non-full album object into full album (non-local album fields is \
                     {non_local:?}, full album fields is {full:?})"
                )
                .into(),
            )),
        }
    }
}

impl TryFrom<Album> for PartialAlbum {
    type Error = ConversionError;

    fn try_from(album: Album) -> Result<Self, Self::Error> {
        match album {
            Album::Full(full) => Ok(PartialAlbum {
                common: full.common,
                non_local: full.non_local,
            }),

            Album::Partial(partial) => Ok(*partial),

            Album::Local(_) => Err(ConversionError(
                "attempt to convert local album into partial album".into(),
            )),
        }
    }
}

impl TryFrom<AlbumObject> for PartialAlbum {
    type Error = ConversionError;

    fn try_from(obj: AlbumObject) -> Result<Self, Self::Error> {
        if let Some(non_local) = obj.non_local {
            Ok(PartialAlbum {
                common: obj.common,
                non_local,
            })
        } else {
            Err(ConversionError(
                format!(
                    "attempt to convert local album object into partial album (non-local album fields is {:?})",
                    obj.non_local
                )
                .into(),
            ))
        }
    }
}

impl From<Album> for LocalAlbum {
    fn from(album: Album) -> Self {
        match album {
            Album::Full(full) => LocalAlbum { common: full.common },
            Album::Partial(partial) => LocalAlbum { common: partial.common },
            Album::Local(local) => *local,
        }
    }
}

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

impl From<FullAlbum> for AlbumObject {
    fn from(value: FullAlbum) -> Self {
        Self {
            common: value.common,
            non_local: Some(value.non_local),
            full: Some(value.full),
        }
    }
}

impl From<PartialAlbum> for AlbumObject {
    fn from(value: PartialAlbum) -> Self {
        Self {
            common: value.common,
            non_local: Some(value.non_local),
            full: None,
        }
    }
}

impl From<LocalAlbum> for AlbumObject {
    fn from(value: LocalAlbum) -> Self {
        Self {
            common: value.common,
            non_local: None,
            full: None,
        }
    }
}

impl crate::private::Sealed for FullAlbum {}
impl crate::private::Sealed for PartialAlbum {}
impl crate::private::Sealed for LocalAlbum {}
impl crate::private::Sealed for AlbumTracks {}

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

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

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

impl private::NonLocalFields for FullAlbum {
    fn non_local_fields(&self) -> &NonLocalAlbumFields {
        &self.non_local
    }
}

impl private::NonLocalFields for PartialAlbum {
    fn non_local_fields(&self) -> &NonLocalAlbumFields {
        &self.non_local
    }
}

impl private::FullFields for FullAlbum {
    fn full_fields(&self) -> &FullAlbumFields {
        &self.full
    }
}

impl PageInformation<PartialTrack> for AlbumTracks {
    type Items = Vec<PartialTrack>;

    fn items(&self) -> Self::Items {
        self.page.items()
    }

    fn take_items(self) -> Self::Items {
        self.page.take_items()
    }

    fn next(self) -> Option<String> {
        <PageObject<TrackObject> as PageInformation<PartialTrack>>::next(self.page)
    }
}

impl Serialize for Album {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Album::Full(full_album) => full_album.serialize(serializer),
            Album::Partial(partial_album) => partial_album.serialize(serializer),
            Album::Local(local_album) => local_album.serialize(serializer),
        }
    }
}

impl Serialize for FullAlbum {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        AlbumObjectRef {
            common: &self.common,
            non_local: Some(&self.non_local),
            full: Some(&self.full),
        }
        .serialize(serializer)
    }
}

impl Serialize for PartialAlbum {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        AlbumObjectRef {
            common: &self.common,
            non_local: Some(&self.non_local),
            full: None,
        }
        .serialize(serializer)
    }
}

impl Serialize for LocalAlbum {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        AlbumObjectRef {
            common: &self.common,
            non_local: None,
            full: None,
        }
        .serialize(serializer)
    }
}

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