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
//! Contains all api objects
use async_trait::async_trait;
#[warn(missing_docs)]
use serde::{Deserialize, Serialize};

use crate::DeezerClient;
use crate::Result;

#[doc(inline)]
pub use self::album::*;
#[doc(inline)]
pub use self::artist::*;
#[doc(inline)]
pub use self::chart::*;
#[doc(inline)]
pub use self::comment::*;
#[doc(inline)]
pub use self::editorial::*;
#[doc(inline)]
pub use self::genre::*;
#[doc(inline)]
pub use self::infos::*;
#[doc(inline)]
pub use self::options::*;
#[doc(inline)]
pub use self::playlist::*;
#[doc(inline)]
pub use self::radio::*;
#[doc(inline)]
pub use self::track::*;
#[doc(inline)]
pub use self::user::*;
use std::ops::Deref;

pub mod album;
pub mod artist;
pub mod chart;
pub mod comment;
pub mod editorial;
pub mod genre;
pub mod infos;
pub mod options;
pub mod playlist;
pub mod radio;
pub mod track;
pub mod user;

/// Wrapper around deezer array types
///
/// Some deezer models return an object with a `data` property containing the actual array.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeezerArray<T> {
    pub data: Vec<T>,
}

impl<T> DeezerArray<T> {
    pub fn iter(&self) -> std::slice::Iter<'_, T> {
        self.data.iter()
    }
}

impl<T> Deref for DeezerArray<T> {
    type Target = [T];

    fn deref(&self) -> &[T] {
        self.data.deref()
    }
}

impl<T> AsRef<[T]> for DeezerArray<T> {
    fn as_ref(&self) -> &[T] {
        &self.data
    }
}

impl<T> IntoIterator for DeezerArray<T> {
    type Item = T;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.into_iter()
    }
}

/// A by id queryable api object of the deezer api
#[async_trait]
pub trait DeezerObject: serde::de::DeserializeOwned {
    /// Get a relative api url for the given `id`
    fn get_api_url(id: u64) -> String;

    /// Fetch an api object with the given `id`
    async fn get(id: u64) -> Result<Option<Self>> {
        let client = DeezerClient::new();

        client.get_entity(id).await
    }
}

// Represents an api object which has a list method
#[async_trait]
pub trait DeezerEnumerable: DeezerObject {
    fn get_all_api_url() -> String;

    async fn get_all() -> Result<Vec<Self>> {
        let client = DeezerClient::new();

        client.get_all().await
    }
}