From c8a56c08d2e9416ffe77ee8b15e3a510dc5e2271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6lfer?= Date: Sat, 4 Oct 2025 16:16:43 +0200 Subject: [PATCH] More parsing --- src/dance_info.rs | 31 ++++++++++++++++++++++++++++++- src/worldsdc/scoringdance.rs | 29 ++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/dance_info.rs b/src/dance_info.rs index d0ee0bf..0e5d629 100644 --- a/src/dance_info.rs +++ b/src/dance_info.rs @@ -1,3 +1,5 @@ +use std::fmt; + #[derive(serde::Deserialize, Debug, PartialEq, Eq)] pub enum DanceRole { Leader, @@ -43,7 +45,7 @@ impl TryFrom<&str> for DanceRole { } } -#[derive(serde::Deserialize, Debug, PartialEq, Eq)] +#[derive(serde::Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum DanceRank { Newcomer, Novice, @@ -68,6 +70,33 @@ impl DanceRank { } } +#[derive(Debug)] +pub struct ParseDanceRankError; + +impl fmt::Display for ParseDanceRankError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "failed to parse DanceRank") + } +} + +impl std::error::Error for ParseDanceRankError {} + +impl std::str::FromStr for DanceRank { + type Err = ParseDanceRankError; + + fn from_str(s: &str) -> Result { + match s { + "Newcomer" => Ok(Self::Newcomer), + "Novice" => Ok(Self::Novice), + "Intermediate" => Ok(Self::Intermediate), + "Advanced" => Ok(Self::Advanced), + "All-Stars" => Ok(Self::AllStars), + "Champions" => Ok(Self::Champions), + _ => Err(ParseDanceRankError), + } + } +} + #[derive(Debug)] pub struct CompState { pub rank: DanceRank, diff --git a/src/worldsdc/scoringdance.rs b/src/worldsdc/scoringdance.rs index ab0a232..d26f930 100644 --- a/src/worldsdc/scoringdance.rs +++ b/src/worldsdc/scoringdance.rs @@ -1,3 +1,5 @@ +use std::{collections, str::FromStr}; + use reqwest::ClientBuilder; use scraper::{ElementRef, Html, Selector}; @@ -59,15 +61,24 @@ fn parse_stats(d: &[Vec]) { (a, b) }); - hash_map! { - "Champions" => DanceRank::Champions, - "All-Stars" => DanceRank::AllStars, - "Advanced" => DanceRank::Advanced, - "Intermediate" => DanceRank::Intermediate, - "Newcomer" => DanceRank::Newcomer, - "Novice" => DanceRank::Novice, - }; - dbg!(chapters.collect::>()); + let (all_time, rest) = chapters.partition::, _>(|(a, b)| a.to_lowercase() == "all time"); + let all_time = &all_time.split_first().unwrap().0.1; + let sorted_chapters = rest + .into_iter() + .map(|(chapter, items)| { + let rank = DanceRank::from_str(chapter).map_err(|_| chapter.to_owned())?; + Ok::<(DanceRank, Vec<[&String; 2]>), String>((rank, items)) + }) + .filter_map(|v| match v { + Ok(v) => Some(v), + Err(e) => { + tracing::warn!("Unknown chapter in parsed html: {e}"); + None + } + }) + .collect::>(); + dbg!(&sorted_chapters); + // dbg!(chapters.collect::>()); } fn extract_tables(html: &str) -> Vec<(String, Vec>)> {