use serde::Deserialize;
use crate::types::utils::u32_from_str;
#[derive(Deserialize, Debug)]
pub(crate) struct WeeklyChartListResponse {
weeklychartlist: WeeklyChartListRaw,
}
#[derive(Deserialize, Debug)]
struct WeeklyChartListRaw {
chart: Vec<WeeklyChartRangeRaw>,
}
#[derive(Deserialize, Debug)]
struct WeeklyChartRangeRaw {
#[serde(deserialize_with = "u32_from_str")]
from: u32,
#[serde(deserialize_with = "u32_from_str")]
to: u32,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct WeeklyChartRange {
pub from: u32,
pub to: u32,
}
impl From<WeeklyChartRangeRaw> for WeeklyChartRange {
fn from(r: WeeklyChartRangeRaw) -> Self {
Self {
from: r.from,
to: r.to,
}
}
}
impl From<WeeklyChartListResponse> for Vec<WeeklyChartRange> {
fn from(r: WeeklyChartListResponse) -> Self {
r.weeklychartlist
.chart
.into_iter()
.map(WeeklyChartRange::from)
.collect()
}
}
#[derive(Deserialize, Debug)]
pub(crate) struct WeeklyTrackChartResponse {
weeklytrackchart: WeeklyTrackChartRaw,
}
#[derive(Deserialize, Debug)]
struct WeeklyTrackChartRaw {
track: Vec<WeeklyTrackRaw>,
}
#[derive(Deserialize, Debug)]
struct WeeklyTrackRaw {
name: String,
mbid: String,
url: String,
#[serde(deserialize_with = "u32_from_str")]
playcount: u32,
artist: WeeklyArtistRef,
#[serde(rename = "@attr")]
attr: WeeklyRankAttr,
}
#[derive(Deserialize, Debug)]
struct WeeklyArtistRef {
#[serde(rename = "#text")]
name: String,
mbid: String,
}
#[derive(Deserialize, Debug)]
struct WeeklyRankAttr {
#[serde(deserialize_with = "u32_from_str")]
rank: u32,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct WeeklyTrack {
pub name: String,
pub mbid: String,
pub url: String,
pub playcount: u32,
pub artist_name: String,
pub artist_mbid: String,
pub rank: u32,
}
impl From<WeeklyTrackRaw> for WeeklyTrack {
fn from(r: WeeklyTrackRaw) -> Self {
Self {
name: r.name,
mbid: r.mbid,
url: r.url,
playcount: r.playcount,
artist_name: r.artist.name,
artist_mbid: r.artist.mbid,
rank: r.attr.rank,
}
}
}
impl From<WeeklyTrackChartResponse> for Vec<WeeklyTrack> {
fn from(r: WeeklyTrackChartResponse) -> Self {
r.weeklytrackchart
.track
.into_iter()
.map(WeeklyTrack::from)
.collect()
}
}
#[derive(Deserialize, Debug)]
pub(crate) struct WeeklyArtistChartResponse {
weeklyartistchart: WeeklyArtistChartRaw,
}
#[derive(Deserialize, Debug)]
struct WeeklyArtistChartRaw {
artist: Vec<WeeklyArtistRaw>,
}
#[derive(Deserialize, Debug)]
struct WeeklyArtistRaw {
name: String,
mbid: String,
url: String,
#[serde(deserialize_with = "u32_from_str")]
playcount: u32,
#[serde(rename = "@attr")]
attr: WeeklyRankAttr,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct WeeklyArtist {
pub name: String,
pub mbid: String,
pub url: String,
pub playcount: u32,
pub rank: u32,
}
impl From<WeeklyArtistRaw> for WeeklyArtist {
fn from(r: WeeklyArtistRaw) -> Self {
Self {
name: r.name,
mbid: r.mbid,
url: r.url,
playcount: r.playcount,
rank: r.attr.rank,
}
}
}
impl From<WeeklyArtistChartResponse> for Vec<WeeklyArtist> {
fn from(r: WeeklyArtistChartResponse) -> Self {
r.weeklyartistchart
.artist
.into_iter()
.map(WeeklyArtist::from)
.collect()
}
}
#[derive(Deserialize, Debug)]
pub(crate) struct WeeklyAlbumChartResponse {
weeklyalbumchart: WeeklyAlbumChartRaw,
}
#[derive(Deserialize, Debug)]
struct WeeklyAlbumChartRaw {
album: Vec<WeeklyAlbumRaw>,
}
#[derive(Deserialize, Debug)]
struct WeeklyAlbumRaw {
name: String,
mbid: String,
url: String,
#[serde(deserialize_with = "u32_from_str")]
playcount: u32,
artist: WeeklyArtistRef,
#[serde(rename = "@attr")]
attr: WeeklyRankAttr,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct WeeklyAlbum {
pub name: String,
pub mbid: String,
pub url: String,
pub playcount: u32,
pub artist_name: String,
pub artist_mbid: String,
pub rank: u32,
}
impl From<WeeklyAlbumRaw> for WeeklyAlbum {
fn from(r: WeeklyAlbumRaw) -> Self {
Self {
name: r.name,
mbid: r.mbid,
url: r.url,
playcount: r.playcount,
artist_name: r.artist.name,
artist_mbid: r.artist.mbid,
rank: r.attr.rank,
}
}
}
impl From<WeeklyAlbumChartResponse> for Vec<WeeklyAlbum> {
fn from(r: WeeklyAlbumChartResponse) -> Self {
r.weeklyalbumchart
.album
.into_iter()
.map(WeeklyAlbum::from)
.collect()
}
}