Implemented watchdog

This commit is contained in:
Lukas Wölfer
2025-07-22 01:39:41 +02:00
parent 8bae1fdfbb
commit 618a6c1d02
6 changed files with 373 additions and 211 deletions

156
src/dance_info.rs Normal file
View File

@@ -0,0 +1,156 @@
use std::collections::HashMap;
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub enum DanceRole {
Leader,
Follower,
}
impl DanceRole {
pub const fn as_str(&self) -> &str {
match self {
Self::Leader => "Leader",
Self::Follower => "Follower",
}
}
#[allow(dead_code)]
pub const fn other(&self) -> Self {
match self {
Self::Leader => Self::Follower,
Self::Follower => Self::Leader,
}
}
}
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub enum DanceRank {
Newcomer,
Novice,
Intermediate,
#[serde(rename = "Advance")]
Advanced,
#[serde(rename = "All Star")]
AllStars,
Champions,
}
impl DanceRank {
pub const fn as_str(&self) -> &str {
match self {
Self::Newcomer => "Newcomer",
Self::Novice => "Novice",
Self::Intermediate => "Intermediate",
Self::Advanced => "Advanced",
Self::AllStars => "All-Stars",
Self::Champions => "Champions",
}
}
}
#[derive(serde::Deserialize, Debug)]
enum OptionalDanceRank {
#[serde(rename = "N/A")]
NotAvailable,
#[serde(untagged)]
Rank(DanceRank),
}
#[derive(serde::Deserialize, Debug)]
enum OptionalDancePoints {
#[serde(rename = "N/A")]
NotAvailable,
#[serde(untagged)]
Points(u16),
}
#[derive(serde::Deserialize, Debug)]
struct DanceInfoParser {
pub dancer_first: String,
pub dancer_last: String,
pub short_dominate_role: DanceRole,
#[allow(dead_code)]
pub short_non_dominate_role: DanceRole,
pub dominate_role_highest_level_points: u16,
pub dominate_role_highest_level: DanceRank,
pub non_dominate_role_highest_level_points: OptionalDancePoints,
pub non_dominate_role_highest_level: OptionalDanceRank,
}
#[derive(Debug)]
pub struct CompState {
pub rank: DanceRank,
pub points: u16,
}
pub struct DanceInfo {
pub firstname: String,
pub lastname: String,
pub dominant_role: DanceRole,
pub dominant_role_comp: CompState,
pub non_dominant_role_comp: Option<CompState>,
}
impl DanceInfo {
pub fn name(&self) -> String {
format!("{} {}", self.firstname, self.lastname)
}
#[allow(dead_code)]
pub const fn non_dominant_role(&self) -> DanceRole {
self.dominant_role.other()
}
}
impl From<DanceInfoParser> for DanceInfo {
fn from(value: DanceInfoParser) -> Self {
let non_dominant_role_comp = if let OptionalDanceRank::Rank(r) =
value.non_dominate_role_highest_level
&& let OptionalDancePoints::Points(l) = value.non_dominate_role_highest_level_points
{
Some(CompState { rank: r, points: l })
} else {
None
};
Self {
firstname: value.dancer_first,
lastname: value.dancer_last,
dominant_role: value.short_dominate_role,
dominant_role_comp: CompState {
rank: value.dominate_role_highest_level,
points: value.dominate_role_highest_level_points,
},
non_dominant_role_comp,
}
}
}
#[derive(thiserror::Error, Debug)]
pub enum DanceInfoError {
#[error("Failed to build client: {0}")]
ClientBuild(reqwest::Error),
#[error("Request error: {0}")]
Request(reqwest::Error),
#[error("Failed to parse response: {0}")]
JsonParse(reqwest::Error),
}
pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
let client = reqwest::ClientBuilder::new()
.build()
.map_err(DanceInfoError::ClientBuild)?;
let mut params = HashMap::new();
params.insert("q", id.to_string());
let response = client
.request(
reqwest::Method::POST,
"https://points.worldsdc.com/lookup2020/find",
)
.form(&params)
.send()
.await
.map_err(DanceInfoError::Request)?;
let x: DanceInfoParser = response.json().await.map_err(DanceInfoError::JsonParse)?;
Ok(x.into())
}