osu_v2/src/user.rs

172 lines
4.6 KiB
Rust

use super::Error;
use async_trait::async_trait;
use serde::{Deserialize, Deserializer};
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct User {
pub id: i64,
pub username: String,
pub profile_colour: Option<String>,
pub avatar_url: String,
pub country_code: String,
pub default_group: String,
pub is_active: bool,
pub is_bot: bool,
pub is_deleted: bool,
pub is_online: bool,
pub is_supporter: bool,
pub last_visit: Option<String>,
pub pm_friends_only: bool,
pub cover_url: String,
pub discord: Option<String>,
pub has_supported: bool,
pub interests: Option<String>,
pub join_date: String,
pub kudosu: Kudosu,
pub location: Option<String>,
pub max_blocks: u64,
pub max_friends: u64,
pub occupation: Option<String>,
pub playmode: String,
pub playstyle: Option<Vec<String>>,
pub post_count: u64,
pub profile_order: Vec<String>,
pub skype: Option<String>,
pub title: Option<String>,
pub twitter: Option<String>,
pub website: Option<String>,
pub country: Country,
pub cover: Cover,
pub is_admin: Option<bool>,
pub is_bng: Option<bool>,
pub is_full_bn: Option<bool>,
pub is_gmt: Option<bool>,
pub is_limited_bn: Option<bool>,
pub is_moderator: Option<bool>,
pub is_nat: Option<bool>,
pub is_restricted: Option<bool>,
pub is_silenced: Option<bool>,
pub statistics: UserStatistics,
pub rank_history: Option<RankHistory>,
pub user_achivements: Option<Vec<UserAchivement>>,
pub beatmap_playcounts_count: u64,
pub favourite_beatmapset_count: u64,
pub follower_count: u64,
pub graveyard_beatmapset_count: u64,
pub monthly_playcounts: Option<Vec<MonthlyPlayCount>>,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct CompactUser {
pub avatar_url: String,
pub country_code: String,
pub default_group: String,
pub id: u64,
pub is_active: bool,
pub is_bot: bool,
pub is_deleted: bool,
pub is_online: bool,
pub is_supporter: bool,
pub last_visit: Option<String>,
pub pm_friends_only: bool,
pub profile_colour: Option<String>,
pub username: String,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct Kudosu {
pub total: u64,
pub available: u64,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct RankHistory {
pub mode: String,
pub data: Vec<u32>,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct MonthlyPlayCount {
pub start_date: String,
pub count: u64,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct UserAchivement {
pub achieved_at: String,
pub achievement_id: u32,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct SearchResult {
pub user: SearchData,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct SearchData {
pub data: Vec<CompactUser>,
pub total: u32,
}
#[derive(Debug, Deserialize, Clone, PartialEq)]
pub struct UserStatistics {
pub grade_counts: GradeCounts,
pub hit_accuracy: f32,
pub is_ranked: bool,
pub level: Level,
pub maximum_combo: u64,
pub play_count: u64,
pub play_time: Option<u64>,
pub pp: f32,
pub ranked_score: u64,
pub replays_watched_by_others: u64,
pub total_hits: u64,
pub total_score: u64,
pub rank: UserRank,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct UserRank {
pub global: Option<u64>,
pub country: Option<u64>,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct Level {
pub current: u8,
pub progress: u16,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct GradeCounts {
pub a: u64,
pub s: u64,
pub sh: u64,
pub ss: u64,
pub ssh: u64,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct Country {
pub code: String,
pub name: String,
}
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct Cover {
custom_url: Option<String>,
url: String,
#[serde(default)]
#[serde(deserialize_with = "parse_to_int")]
id: Option<u64>,
}
fn parse_to_int<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
where
D: Deserializer<'de>,
{
let stri: Option<String> = Option::deserialize(deserializer)?;
if let Some(s) = stri {
match s.parse::<u64>() {
Ok(v) => return Ok(Some(v)),
Err(_) => return Ok(None),
}
}
Ok(None)
}
#[async_trait]
pub trait UserMethods {
async fn get_user<'a>(&'a mut self, id: u64, m: String) -> Result<Box<User>, Error>;
async fn search_user<'a>(&'a mut self, name: String) -> Result<Box<SearchResult>, Error>;
}