use std::cmp::Ordering;
use std::fmt;
use crate::types::{BaseResponse, RankAttr, TrackImage};
use serde::{Deserialize, Serialize};
use crate::types::utils::{bool_from_str, u32_from_str};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct TopArtist {
pub name: String,
pub mbid: String,
pub url: String,
#[serde(deserialize_with = "u32_from_str")]
pub playcount: u32,
#[serde(deserialize_with = "bool_from_str")]
pub streamable: bool,
pub image: Vec<TrackImage>,
#[serde(rename = "@attr")]
pub attr: RankAttr,
}
impl fmt::Display for TopArtist {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"#{} - {} ({} plays)",
self.attr.rank, self.name, self.playcount
)
}
}
impl PartialEq for TopArtist {
fn eq(&self, other: &Self) -> bool {
self.playcount == other.playcount
}
}
impl Eq for TopArtist {}
impl PartialOrd for TopArtist {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for TopArtist {
fn cmp(&self, other: &Self) -> Ordering {
self.playcount.cmp(&other.playcount)
}
}
#[cfg(feature = "sqlite")]
impl crate::sqlite::SqliteExportable for TopArtist {
fn table_name() -> &'static str {
"top_artists"
}
fn create_table_sql() -> &'static str {
"CREATE TABLE IF NOT EXISTS top_artists (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
mbid TEXT NOT NULL,
url TEXT NOT NULL,
playcount INTEGER NOT NULL,
rank INTEGER NOT NULL
)"
}
fn insert_sql() -> &'static str {
"INSERT INTO top_artists (name, mbid, url, playcount, rank)
VALUES (?1, ?2, ?3, ?4, ?5)"
}
fn bind_and_execute(&self, stmt: &mut rusqlite::Statement<'_>) -> rusqlite::Result<usize> {
let rank: u32 = self.attr.rank.parse().unwrap_or_default();
stmt.execute(rusqlite::params![
self.name,
self.mbid,
self.url,
self.playcount,
rank,
])
}
}
#[cfg(feature = "sqlite")]
impl crate::sqlite::SqliteLoadable for TopArtist {
fn select_sql() -> &'static str {
"SELECT name, mbid, url, playcount, rank
FROM top_artists
ORDER BY rank ASC"
}
fn from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Self> {
Ok(Self {
name: row.get(0)?,
mbid: row.get(1)?,
url: row.get(2)?,
playcount: row.get(3)?,
attr: crate::types::RankAttr {
rank: row.get::<_, u32>(4)?.to_string(),
},
streamable: false,
image: vec![],
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct TopArtists {
pub artist: Vec<TopArtist>,
#[serde(rename = "@attr")]
pub attr: BaseResponse,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct UserTopArtists {
pub topartists: TopArtists,
}