rspotify_model/
context.rs

1//! All objects related to context
2
3use chrono::serde::ts_milliseconds;
4use chrono::{DateTime, Duration, Utc};
5use serde::{Deserialize, Deserializer, Serialize};
6
7use std::collections::HashMap;
8
9use crate::{
10    custom_serde::option_duration_ms, CurrentlyPlayingType, Device, DisallowKey, PlayableItem,
11    RepeatState, Type,
12};
13
14/// Context object
15#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
16pub struct Context {
17    /// The URI may be of any type, so it's not parsed into a [`crate::Id`]
18    pub uri: String,
19    pub href: String,
20    pub external_urls: HashMap<String, String>,
21    #[serde(rename = "type")]
22    pub _type: Type,
23}
24
25/// Currently playing object
26#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
27pub struct CurrentlyPlayingContext {
28    pub context: Option<Context>,
29    #[serde(with = "ts_milliseconds")]
30    pub timestamp: DateTime<Utc>,
31    #[serde(default)]
32    #[serde(with = "option_duration_ms", rename = "progress_ms")]
33    pub progress: Option<Duration>,
34    pub is_playing: bool,
35    pub item: Option<PlayableItem>,
36    pub currently_playing_type: CurrentlyPlayingType,
37    pub actions: Actions,
38}
39
40#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
41pub struct CurrentPlaybackContext {
42    pub device: Device,
43    pub repeat_state: RepeatState,
44    pub shuffle_state: bool,
45    pub context: Option<Context>,
46    #[serde(with = "ts_milliseconds")]
47    pub timestamp: DateTime<Utc>,
48    #[serde(default)]
49    #[serde(with = "option_duration_ms", rename = "progress_ms")]
50    pub progress: Option<Duration>,
51    pub is_playing: bool,
52    pub item: Option<PlayableItem>,
53    pub currently_playing_type: CurrentlyPlayingType,
54    pub actions: Actions,
55}
56#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
57pub struct CurrentUserQueue {
58    pub currently_playing: Option<PlayableItem>,
59    pub queue: Vec<PlayableItem>,
60}
61
62/// Actions object
63#[derive(Clone, Debug, Serialize, PartialEq, Eq, Default)]
64pub struct Actions {
65    pub disallows: Vec<DisallowKey>,
66}
67
68impl<'de> Deserialize<'de> for Actions {
69    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70    where
71        D: Deserializer<'de>,
72    {
73        #[derive(Deserialize)]
74        struct OriginalActions {
75            pub disallows: HashMap<DisallowKey, bool>,
76        }
77        let orignal_actions = OriginalActions::deserialize(deserializer)?;
78        Ok(Self {
79            disallows: orignal_actions
80                .disallows
81                .into_iter()
82                .filter(|(_, value)| *value)
83                .map(|(key, _)| key)
84                .collect(),
85        })
86    }
87}