More parsing

This commit is contained in:
Lukas Wölfer
2025-10-04 16:16:43 +02:00
parent a5ef3f6569
commit c8a56c08d2
2 changed files with 50 additions and 10 deletions

View File

@@ -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<Self, Self::Err> {
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,

View File

@@ -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<String>]) {
(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::<Vec<_>>());
let (all_time, rest) = chapters.partition::<Vec<_>, _>(|(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::<Vec<_>>();
dbg!(&sorted_chapters);
// dbg!(chapters.collect::<Vec<_>>());
}
fn extract_tables(html: &str) -> Vec<(String, Vec<Vec<String>>)> {