use super::{
ExternalUrls, Followers, Image, ItemType, Page, TrackItem, TrackReference, UserReference,
VideoThumbnail,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Playlist {
pub collaborative: bool,
pub description: Option<String>,
pub external_urls: ExternalUrls,
pub followers: Followers,
pub href: String,
pub id: String,
pub images: Option<Vec<Image>>,
pub name: String,
pub owner: UserReference,
pub primary_color: Option<String>,
pub public: Option<bool>,
pub snapshot_id: String,
#[cfg(feature = "page_items")]
pub tracks: Page<PlaylistTrack>,
#[serde(rename = "type")]
pub type_: ItemType,
pub uri: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SimplifiedPlaylist {
pub collaborative: bool,
pub description: Option<String>,
pub external_urls: ExternalUrls,
pub href: String,
pub id: String,
pub images: Option<Vec<Image>>,
pub name: String,
pub owner: UserReference,
pub primary_color: Option<String>,
pub public: Option<bool>,
pub snapshot_id: String,
pub tracks: Option<TrackReference>,
#[serde(rename = "type")]
pub type_: ItemType,
pub uri: String,
}
impl From<Playlist> for SimplifiedPlaylist {
fn from(playlist: Playlist) -> Self {
let tracks = {
#[cfg(feature = "page_items")]
{
Some(TrackReference {
href: playlist.tracks.href,
total: playlist.tracks.total,
})
}
#[cfg(not(feature = "page_items"))]
{
None
}
};
Self {
collaborative: playlist.collaborative,
description: playlist.description,
external_urls: playlist.external_urls,
href: playlist.href,
id: playlist.id,
images: playlist.images,
name: playlist.name,
owner: playlist.owner,
primary_color: playlist.primary_color,
public: playlist.public,
snapshot_id: playlist.snapshot_id,
tracks,
type_: playlist.type_,
uri: playlist.uri,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PlaylistTrack {
pub added_at: Option<String>,
pub added_by: Option<AddedBy>,
pub is_local: bool,
pub primary_color: Option<String>,
pub video_thumbnail: Option<VideoThumbnail>,
pub track: TrackItem,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AddedBy {
pub external_urls: ExternalUrls,
#[serde(skip_serializing_if = "Option::is_none")]
pub followers: Option<Followers>,
pub href: String,
pub id: String,
#[serde(rename = "type")]
pub type_: ItemType,
pub uri: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SnapshotId {
pub snapshot_id: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn playlist() {
let json = r#"
{
"collaborative": false,
"description": "string",
"external_urls": {
"spotify": "string"
},
"followers": {
"href": "string",
"total": 0
},
"href": "string",
"id": "string",
"images": [
{
"url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228",
"height": 300,
"width": 300
}
],
"name": "string",
"owner": {
"external_urls": {
"spotify": "string"
},
"followers": {
"href": "string",
"total": 0
},
"href": "string",
"id": "string",
"type": "user",
"uri": "string",
"display_name": "string"
},
"primary_color": null,
"public": false,
"snapshot_id": "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": [
{
"added_at": "string",
"added_by": {
"external_urls": {
"spotify": "string"
},
"followers": {
"href": "string",
"total": 0
},
"href": "string",
"id": "string",
"type": "user",
"uri": "string"
},
"is_local": false,
"primary_color": null,
"video_thumbnail": {
"url": null
},
"track": {
"album": {
"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"
}
]
},
"artists": [
{
"external_urls": {
"spotify": "string"
},
"followers": {
"href": "string",
"total": 0
},
"genres": ["Prog rock", "Grunge"],
"href": "string",
"id": "string",
"images": [
{
"url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228",
"height": 300,
"width": 300
}
],
"name": "string",
"popularity": 0,
"type": "artist",
"uri": "string"
}
],
"available_markets": ["US"],
"disc_number": 0,
"duration_ms": 0,
"explicit": false,
"external_ids": {
"isrc": "string",
"ean": "string",
"upc": "string"
},
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"is_playable": false,
"linked_from": {},
"restrictions": {
"reason": "string"
},
"name": "string",
"popularity": 0,
"preview_url": "string",
"track_number": 0,
"type": "track",
"uri": "string",
"is_local": false
}
}
]
},
"type": "playlist",
"uri": "string"
}
"#;
crate::test::assert_deserialized!(Playlist, json);
}
#[test]
fn simplified_playlist() {
let json = r#"
{
"collaborative": false,
"description": "string",
"external_urls": {
"spotify": "string"
},
"href": "string",
"id": "string",
"images": [
{
"url": "https://i.scdn.co/image/ab67616d00001e02ff9ca10b55ce82ae553c8228",
"height": 300,
"width": 300
}
],
"name": "string",
"owner": {
"external_urls": {
"spotify": "string"
},
"followers": {
"href": "string",
"total": 0
},
"href": "string",
"id": "string",
"type": "user",
"uri": "string",
"display_name": "string"
},
"primary_color": null,
"public": false,
"snapshot_id": "string",
"tracks": {
"href": "string",
"total": 0
},
"type": "playlist",
"uri": "string"
}
"#;
crate::test::assert_deserialized!(SimplifiedPlaylist, json);
}
}