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
use super::{
Copyright, ExternalIds, ExternalUrls, Image, ItemType, Market, Page, ReleaseDatePrecision,
Restrictions, SimplifiedArtist, SimplifiedTrack,
};
use serde::{Deserialize, Serialize};
/// The type of an album.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AlbumType {
#[serde(alias = "ALBUM")]
Album,
#[serde(alias = "SINGLE")]
Single,
#[serde(alias = "COMPILATION")]
Compilation,
#[serde(alias = "APPEARS_ON")]
AppearsOn,
}
impl std::fmt::Display for AlbumType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Album => "Album",
Self::Single => "Single",
Self::Compilation => "Compilation",
Self::AppearsOn => "Appears on",
};
write!(f, "{s}")
}
}
impl AlbumType {
pub fn all() -> &'static [Self] {
&[
Self::Album,
Self::Single,
Self::Compilation,
Self::AppearsOn,
]
}
pub fn snake_case(&self) -> &'static str {
match self {
Self::Album => "album",
Self::Single => "single",
Self::Compilation => "compilation",
Self::AppearsOn => "appears_on",
}
}
}
/// A full album object from the Spotify catalog.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Album {
/// The type of the album.
pub album_type: AlbumType,
/// The number of tracks in the album.
pub total_tracks: usize,
/// The markets in which the album is available: [ISO 3166-1 alpha-2 country codes](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
///
/// # Note
/// An album is considered available in a market when at least 1 of its tracks is available in that market.
#[cfg(feature = "markets")]
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub available_markets: Vec<Market>,
/// Known external URLs for this album.
pub external_urls: ExternalUrls,
/// A link to the Web API endpoint providing full details of the album.
pub href: String,
/// The [Spotify ID](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the album.
pub id: String,
/// The cover art for the album in various sizes, widest first.
pub images: Vec<Image>,
/// The name of the album. In case of an album takedown, the value may be an empty string.
pub name: String,
/// The date the album was first released.
///
/// Example: "1981-12"
pub release_date: String,
/// The precision with which `release_date` value is known.
pub release_date_precision: ReleaseDatePrecision,
/// Included in the response when a content restriction is applied.
/// Albums may be restricted if the content is not available in a given market, to the user's subscription type, or when the user's account is set to not play explicit content. Additional reasons may be added in the future.
#[serde(skip_serializing_if = "Option::is_none")]
pub restrictions: Option<Restrictions>,
/// The object type.
///
/// Allowed values: "album"
#[serde(rename = "type")]
pub type_: ItemType,
/// The [Spotify URI](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the album.
pub uri: String,
/// The artists of the album. Each artist object includes a link in href to more detailed information about the artist.
pub artists: Vec<SimplifiedArtist>,
/// The tracks of the album.
#[cfg(feature = "page_items")]
pub tracks: Page<SimplifiedTrack>,
/// The copyright statements of the album.
pub copyrights: Vec<Copyright>,
/// Known external IDs for the album.
pub external_ids: ExternalIds,
/// The label associated with the album.
pub label: String,
/// The popularity of the album. The value will be between 0 and 100, with 100 being the most popular.
pub popularity: u8,
/// This field describes the relationship between the artist and the album.
#[serde(skip_serializing_if = "Option::is_none")]
pub album_group: Option<AlbumType>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SimplifiedAlbum {
/// The type of the album.
pub album_type: AlbumType,
/// The number of tracks in the album.
pub total_tracks: usize,
/// The markets in which the album is available: [ISO 3166-1 alpha-2 country codes](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
///
/// # Note
/// An album is considered available in a market when at least 1 of its tracks is available in that market.
#[cfg(feature = "markets")]
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub available_markets: Vec<Market>,
/// Known external URLs for this album.
pub external_urls: ExternalUrls,
/// A link to the Web API endpoint providing full details of the album.
pub href: String,
/// The [Spotify ID](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the album.
pub id: String,
/// The cover art for the album in various sizes, widest first.
pub images: Vec<Image>,
/// The name of the album. In case of an album takedown, the value may be an empty string.
pub name: String,
/// The date the album was first released.
///
/// Example: "1981-12"
#[serde(skip_serializing_if = "Option::is_none")]
pub release_date: Option<String>,
/// The precision with which `release_date` value is known.
#[serde(skip_serializing_if = "Option::is_none")]
pub release_date_precision: Option<ReleaseDatePrecision>,
/// Included in the response when a content restriction is applied.
/// Albums may be restricted if the content is not available in a given market, to the user's subscription type, or when the user's account is set to not play explicit content. Additional reasons may be added in the future.
#[serde(skip_serializing_if = "Option::is_none")]
pub restrictions: Option<Restrictions>,
/// The object type.
///
/// Allowed values: "album"
#[serde(rename = "type")]
pub type_: ItemType,
/// The [Spotify URI](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the album.
pub uri: String,
/// The artists of the album. Each artist object includes a link in href to more detailed information about the artist.
pub artists: Vec<SimplifiedArtist>,
}
impl From<Album> for SimplifiedAlbum {
fn from(album: Album) -> Self {
Self {
album_type: album.album_type,
total_tracks: album.total_tracks,
#[cfg(feature = "markets")]
available_markets: album.available_markets,
external_urls: album.external_urls,
href: album.href,
id: album.id,
images: album.images,
name: album.name,
release_date: Some(album.release_date),
release_date_precision: Some(album.release_date_precision),
restrictions: album.restrictions,
type_: album.type_,
uri: album.uri,
artists: album.artists,
}
}
}
/// Spotify catalog information for several albums
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Albums {
pub albums: Vec<Option<Album>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SavedAlbum {
/// The date and time the track was saved.
/// Timestamps are returned in ISO 8601 format as Coordinated Universal Time (UTC) with a zero offset: YYYY-MM-DDTHH:MM:SSZ.
/// If the time is imprecise (for example, the date/time of an album release), an additional field indicates the precision;
/// see for example, `release_date` in an album object.
pub added_at: String,
/// Information about the album.
pub album: Album,
}
/// A list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NewReleases {
pub albums: Page<SimplifiedAlbum>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn album() {
let json = r#"
{
"album_type": "compilation",
"total_tracks": 9,
"available_markets": ["CA", "BR", "IT"],
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "2up3OPMp9Tb4dAKM2erWXQ",
"images": [
{
"url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228",
"height": 300,
"width": 300
}
],
"name": "string",
"release_date": "1981-12",
"release_date_precision": "year",
"restrictions": {
"reason": "market"
},
"type": "album",
"uri": "spotify:album:2up3OPMp9Tb4dAKM2erWXQ",
"artists": [
{
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"name": "string",
"type": "artist",
"uri": "string"
}
],
"tracks": {
"href": "https://api.spotify.com/v1/me/shows?offset=0&limit=20",
"limit": 20,
"next": "https://api.spotify.com/v1/me/shows?offset=1&limit=1",
"offset": 0,
"previous": "https://api.spotify.com/v1/me/shows?offset=1&limit=1",
"total": 4,
"items": [
{
"artists": [
{
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"name": "string",
"type": "artist",
"uri": "string"
}
],
"available_markets": ["CA", "BR", "IT"],
"disc_number": 0,
"duration_ms": 0,
"explicit": false,
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"is_playable": false,
"linked_from": {
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"type": "track",
"uri": "string"
},
"restrictions": {
"reason": "string"
},
"name": "string",
"preview_url": "string",
"track_number": 0,
"type": "track",
"uri": "string",
"is_local": false
}
]
},
"copyrights": [
{
"text": "string",
"type": "P"
}
],
"external_ids": {
"isrc": "string",
"ean": "string",
"upc": "string"
},
"genres": ["Egg punk", "Noise rock"],
"label": "string",
"popularity": 0
}
"#;
crate::test::assert_deserialized!(Album, json);
}
#[test]
fn simplified_album() {
let json = r#"
{
"album_type": "compilation",
"total_tracks": 9,
"available_markets": ["CA", "BR", "IT"],
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "2up3OPMp9Tb4dAKM2erWXQ",
"images": [
{
"url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228",
"height": 300,
"width": 300
}
],
"name": "string",
"release_date": "1981-12",
"release_date_precision": "year",
"restrictions": {
"reason": "market"
},
"type": "album",
"uri": "spotify:album:2up3OPMp9Tb4dAKM2erWXQ",
"artists": [
{
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"name": "string",
"type": "artist",
"uri": "string"
}
]
}
"#;
crate::test::assert_deserialized!(SimplifiedAlbum, json);
}
}