Skip to main content

rspotify_model/
track.rs

1//! All kinds of tracks object
2
3use chrono::prelude::*;
4use chrono::Duration;
5use serde::{Deserialize, Serialize};
6
7use std::collections::HashMap;
8
9use crate::{
10    custom_serde::duration_ms, PlayableId, Restriction, SimplifiedAlbum, SimplifiedArtist, TrackId,
11    Type,
12};
13
14/// Full track object
15#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
16pub struct FullTrack {
17    pub album: SimplifiedAlbum,
18    pub artists: Vec<SimplifiedArtist>,
19    #[deprecated(
20        since = "0.16.0",
21        note = "Spotify has removed this field. See https://github.com/ramsayleung/rspotify/issues/550"
22    )]
23    #[serde(skip_serializing_if = "Vec::is_empty", default)]
24    pub available_markets: Vec<String>,
25    pub disc_number: i32,
26    #[serde(with = "duration_ms", rename = "duration_ms")]
27    pub duration: Duration,
28    pub explicit: bool,
29    pub external_ids: HashMap<String, String>,
30    pub external_urls: HashMap<String, String>,
31    pub href: Option<String>,
32    /// Note that a track may not have an ID/URI if it's local
33    pub id: Option<TrackId<'static>>,
34    pub is_local: bool,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub is_playable: Option<bool>,
37    #[deprecated(
38        since = "0.16.0",
39        note = "Spotify has removed this field. See https://github.com/ramsayleung/rspotify/issues/550"
40    )]
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub linked_from: Option<TrackLink>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub restrictions: Option<Restriction>,
45    pub name: String,
46    #[deprecated(
47        since = "0.16.0",
48        note = "Spotify has removed this field. See https://github.com/ramsayleung/rspotify/issues/550"
49    )]
50    #[serde(default)]
51    pub popularity: u32,
52    pub preview_url: Option<String>,
53    pub track_number: u32,
54    pub r#type: Type,
55}
56
57/// Track link object
58/// [track-relinking](https://developer.spotify.com/documentation/web-api/concepts/track-relinking)
59#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
60pub struct TrackLink {
61    pub external_urls: HashMap<String, String>,
62    pub href: String,
63    pub id: Option<TrackId<'static>>,
64    pub r#type: Type,
65    pub uri: String,
66}
67
68/// Intermediate full track wrapped by `Vec`
69#[derive(Deserialize)]
70pub struct FullTracks {
71    pub tracks: Vec<FullTrack>,
72}
73
74/// Simplified track object.
75///
76/// `is_playable`, `linked_from` and `restrictions` will only be present when
77/// relinking is applied.
78#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
79pub struct SimplifiedTrack {
80    #[serde(skip_serializing_if = "Option::is_none")]
81    pub album: Option<SimplifiedAlbum>,
82    pub artists: Vec<SimplifiedArtist>,
83    #[deprecated(
84        since = "0.16.0",
85        note = "Spotify has removed this field. See https://github.com/ramsayleung/rspotify/issues/550"
86    )]
87    pub available_markets: Option<Vec<String>>,
88    pub disc_number: i32,
89    #[serde(with = "duration_ms", rename = "duration_ms")]
90    pub duration: Duration,
91    pub explicit: bool,
92    pub external_urls: HashMap<String, String>,
93    #[serde(default)]
94    pub href: Option<String>,
95    pub id: Option<TrackId<'static>>,
96    pub is_local: bool,
97    pub is_playable: Option<bool>,
98    #[deprecated(
99        since = "0.16.0",
100        note = "Spotify has removed this field. See https://github.com/ramsayleung/rspotify/issues/550"
101    )]
102    pub linked_from: Option<TrackLink>,
103    pub restrictions: Option<Restriction>,
104    pub name: String,
105    pub preview_url: Option<String>,
106    pub track_number: u32,
107}
108
109/// Saved track object
110#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
111pub struct SavedTrack {
112    pub added_at: DateTime<Utc>,
113    pub track: FullTrack,
114}
115
116/// Track id with specific positions track in a playlist
117///
118/// This is a short-lived struct for endpoint parameters, so it uses
119/// `PlayableId<'a>` instead of `PlayableId<'static>` to avoid the unnecessary
120/// allocation. Same goes for the positions slice instead of vector.
121pub struct ItemPositions<'a> {
122    pub id: PlayableId<'a>,
123    pub positions: &'a [u32],
124}