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)] #[derive(serde::Deserialize, Debug, PartialEq, Eq)]
pub enum DanceRole { pub enum DanceRole {
Leader, 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 { pub enum DanceRank {
Newcomer, Newcomer,
Novice, 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)] #[derive(Debug)]
pub struct CompState { pub struct CompState {
pub rank: DanceRank, pub rank: DanceRank,

View File

@@ -1,3 +1,5 @@
use std::{collections, str::FromStr};
use reqwest::ClientBuilder; use reqwest::ClientBuilder;
use scraper::{ElementRef, Html, Selector}; use scraper::{ElementRef, Html, Selector};
@@ -59,15 +61,24 @@ fn parse_stats(d: &[Vec<String>]) {
(a, b) (a, b)
}); });
hash_map! { let (all_time, rest) = chapters.partition::<Vec<_>, _>(|(a, b)| a.to_lowercase() == "all time");
"Champions" => DanceRank::Champions, let all_time = &all_time.split_first().unwrap().0.1;
"All-Stars" => DanceRank::AllStars, let sorted_chapters = rest
"Advanced" => DanceRank::Advanced, .into_iter()
"Intermediate" => DanceRank::Intermediate, .map(|(chapter, items)| {
"Newcomer" => DanceRank::Newcomer, let rank = DanceRank::from_str(chapter).map_err(|_| chapter.to_owned())?;
"Novice" => DanceRank::Novice, Ok::<(DanceRank, Vec<[&String; 2]>), String>((rank, items))
}; })
dbg!(chapters.collect::<Vec<_>>()); .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>>)> { fn extract_tables(html: &str) -> Vec<(String, Vec<Vec<String>>)> {