use serde::Deserialize;
use crate::types::TrackImage;
use crate::types::utils::{bool_from_str, u32_from_str};
#[derive(Deserialize, Debug)]
pub(crate) struct FriendsResponse {
friends: FriendsRaw,
}
#[derive(Deserialize, Debug)]
struct FriendsRaw {
user: Vec<FriendRaw>,
#[serde(rename = "@attr")]
attr: FriendsAttr,
}
#[derive(Deserialize, Debug)]
struct FriendsAttr {
#[serde(deserialize_with = "u32_from_str")]
total: u32,
#[serde(deserialize_with = "u32_from_str")]
page: u32,
#[serde(rename = "totalPages", deserialize_with = "u32_from_str")]
total_pages: u32,
#[serde(rename = "perPage", deserialize_with = "u32_from_str")]
per_page: u32,
}
#[derive(Deserialize, Debug)]
struct RegisteredRaw {
#[serde(rename = "unixtime", deserialize_with = "u32_from_str")]
unixtime: u32,
}
#[derive(Deserialize, Debug)]
struct FriendRaw {
name: String,
realname: String,
url: String,
country: String,
#[serde(deserialize_with = "bool_from_str")]
subscriber: bool,
image: Vec<TrackImage>,
registered: RegisteredRaw,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct FriendProfile {
pub name: String,
pub real_name: String,
pub url: String,
pub country: String,
pub subscriber: bool,
pub images: Vec<TrackImage>,
pub registered: u32,
}
impl From<FriendRaw> for FriendProfile {
fn from(r: FriendRaw) -> Self {
Self {
name: r.name,
real_name: r.realname,
url: r.url,
country: r.country,
subscriber: r.subscriber,
images: r.image,
registered: r.registered.unixtime,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct FriendsPage {
pub friends: Vec<FriendProfile>,
pub total: u32,
pub page: u32,
pub total_pages: u32,
pub per_page: u32,
}
impl From<FriendsResponse> for FriendsPage {
fn from(r: FriendsResponse) -> Self {
Self {
total: r.friends.attr.total,
page: r.friends.attr.page,
total_pages: r.friends.attr.total_pages,
per_page: r.friends.attr.per_page,
friends: r
.friends
.user
.into_iter()
.map(FriendProfile::from)
.collect(),
}
}
}