2021-12-26 20:18:42 +00:00
|
|
|
use std::{
|
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
fmt::Debug,
|
|
|
|
ops::Deref,
|
2021-12-07 22:22:24 +00:00
|
|
|
};
|
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use crate::{request::RequestResult, track::Tracks, util::try_from_repeated_message, Metadata};
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_core::{Error, Session, SpotifyId};
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_protocol as protocol;
|
|
|
|
use protocol::metadata::ArtistWithRole as ArtistWithRoleMessage;
|
2021-12-07 22:22:24 +00:00
|
|
|
pub use protocol::metadata::ArtistWithRole_ArtistRole as ArtistRole;
|
2021-12-26 20:18:42 +00:00
|
|
|
use protocol::metadata::TopTracks as TopTracksMessage;
|
2021-12-07 22:22:24 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Artist {
|
|
|
|
pub id: SpotifyId,
|
|
|
|
pub name: String,
|
|
|
|
pub top_tracks: CountryTopTracks,
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:44:43 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2022-06-29 22:22:27 +00:00
|
|
|
pub struct Artists(pub Vec<Artist>);
|
2021-12-07 22:22:24 +00:00
|
|
|
|
|
|
|
impl Deref for Artists {
|
2022-06-29 22:22:27 +00:00
|
|
|
type Target = Vec<Artist>;
|
2021-12-07 22:22:24 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ArtistWithRole {
|
|
|
|
pub id: SpotifyId,
|
|
|
|
pub name: String,
|
|
|
|
pub role: ArtistRole,
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:44:43 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2021-12-07 22:22:24 +00:00
|
|
|
pub struct ArtistsWithRole(pub Vec<ArtistWithRole>);
|
|
|
|
|
|
|
|
impl Deref for ArtistsWithRole {
|
|
|
|
type Target = Vec<ArtistWithRole>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct TopTracks {
|
|
|
|
pub country: String,
|
|
|
|
pub tracks: Tracks,
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:44:43 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2021-12-07 22:22:24 +00:00
|
|
|
pub struct CountryTopTracks(pub Vec<TopTracks>);
|
|
|
|
|
|
|
|
impl Deref for CountryTopTracks {
|
|
|
|
type Target = Vec<TopTracks>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CountryTopTracks {
|
|
|
|
pub fn for_country(&self, country: &str) -> Tracks {
|
|
|
|
if let Some(country) = self.0.iter().find(|top_track| top_track.country == country) {
|
|
|
|
return country.tracks.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(global) = self.0.iter().find(|top_track| top_track.country.is_empty()) {
|
|
|
|
return global.tracks.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tracks(vec![]) // none found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Metadata for Artist {
|
|
|
|
type Message = protocol::metadata::Artist;
|
|
|
|
|
|
|
|
async fn request(session: &Session, artist_id: SpotifyId) -> RequestResult {
|
2021-12-26 20:18:42 +00:00
|
|
|
session.spclient().get_artist_metadata(artist_id).await
|
2021-12-07 22:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
fn parse(msg: &Self::Message, _: SpotifyId) -> Result<Self, Error> {
|
2021-12-07 22:22:24 +00:00
|
|
|
Self::try_from(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&<Self as Metadata>::Message> for Artist {
|
2021-12-26 20:18:42 +00:00
|
|
|
type Error = librespot_core::Error;
|
2021-12-07 22:22:24 +00:00
|
|
|
fn try_from(artist: &<Self as Metadata>::Message) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
id: artist.try_into()?,
|
|
|
|
name: artist.get_name().to_owned(),
|
|
|
|
top_tracks: artist.get_top_track().try_into()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try_from_repeated_message!(<Artist as Metadata>::Message, Artists);
|
|
|
|
|
|
|
|
impl TryFrom<&ArtistWithRoleMessage> for ArtistWithRole {
|
2021-12-26 20:18:42 +00:00
|
|
|
type Error = librespot_core::Error;
|
2021-12-07 22:22:24 +00:00
|
|
|
fn try_from(artist_with_role: &ArtistWithRoleMessage) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
id: artist_with_role.try_into()?,
|
|
|
|
name: artist_with_role.get_artist_name().to_owned(),
|
|
|
|
role: artist_with_role.get_role(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try_from_repeated_message!(ArtistWithRoleMessage, ArtistsWithRole);
|
|
|
|
|
|
|
|
impl TryFrom<&TopTracksMessage> for TopTracks {
|
2021-12-26 20:18:42 +00:00
|
|
|
type Error = librespot_core::Error;
|
2021-12-07 22:22:24 +00:00
|
|
|
fn try_from(top_tracks: &TopTracksMessage) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
country: top_tracks.get_country().to_owned(),
|
|
|
|
tracks: top_tracks.get_track().try_into()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try_from_repeated_message!(TopTracksMessage, CountryTopTracks);
|