rspotify_model/
page.rs

1//! All kinds of page object
2use serde::{de::DeserializeOwned, Deserialize, Serialize};
3
4/// Custom deserializer to handle `Vec<Option<T>>` and filter out `None` values
5/// This is useful for deserializing lists that may contain null values that are not relevants
6fn vec_without_nulls<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
7where
8    T: serde::Deserialize<'de>,
9    D: serde::Deserializer<'de>,
10{
11    let v = Vec::<Option<T>>::deserialize(deserializer)?;
12    Ok(v.into_iter().flatten().collect())
13}
14
15/// Paging object
16#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
17pub struct Page<T: DeserializeOwned> {
18    pub href: String,
19    #[serde(deserialize_with = "vec_without_nulls")]
20    pub items: Vec<T>,
21    pub limit: u32,
22    pub next: Option<String>,
23    pub offset: u32,
24    pub previous: Option<String>,
25    /// This field could mismatch the actual number of items in `items` field
26    /// because sometimes the API returns `null` items that are not included in the `items` field.
27    pub total: u32,
28}
29
30/// Cursor-based paging object
31#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
32pub struct CursorBasedPage<T> {
33    pub href: String,
34    pub items: Vec<T>,
35    pub limit: u32,
36    pub next: Option<String>,
37    pub cursors: Option<Cursor>,
38    /// Absent if it has read all data items. This field doesn't match what
39    /// Spotify document says
40    pub total: Option<u32>,
41}
42
43/// Cursor object
44#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
45pub struct Cursor {
46    pub after: Option<String>,
47}