Compare commits
2 Commits
637ff144c3
...
8172d4c769
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8172d4c769 | ||
|
|
29e85397a7 |
65
Cargo.lock
generated
65
Cargo.lock
generated
@@ -161,8 +161,10 @@ checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
@@ -276,7 +278,10 @@ dependencies = [
|
||||
name = "dancing-bot-teachers"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
"mwbot",
|
||||
"rand 0.9.2",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"thiserror 2.0.12",
|
||||
@@ -494,7 +499,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -568,6 +573,21 @@ dependencies = [
|
||||
"new_debug_unreachable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.31"
|
||||
@@ -575,6 +595,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -583,6 +604,34 @@ version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.104",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.31"
|
||||
@@ -601,10 +650,16 @@ version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1748,7 +1803,7 @@ dependencies = [
|
||||
"once_cell",
|
||||
"socket2",
|
||||
"tracing",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2034,7 +2089,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2417,7 +2472,7 @@ dependencies = [
|
||||
"getrandom 0.3.3",
|
||||
"once_cell",
|
||||
"rustix",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2989,7 +3044,7 @@ version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||
dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -11,7 +11,10 @@ keywords = ["mediawiki", "bot", "teacher", "score", "automation"]
|
||||
categories = ["web-programming", "api-bindings", "automation"]
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.41"
|
||||
futures = "0.3.31"
|
||||
mwbot = { git = "https://gitlab.wikimedia.org/repos/mwbot-rs/mwbot.git", rev = "05cbb12188f18e2da710de158d89a9a4f1b42689", default-features = false, features = ["generators", "mwbot_derive"] }
|
||||
rand = "0.9.2"
|
||||
reqwest = "0.12.22"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
thiserror = "2.0.12"
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
|
||||
pub enum DanceRole {
|
||||
Leader,
|
||||
@@ -48,35 +46,6 @@ impl DanceRank {
|
||||
}
|
||||
}
|
||||
|
||||
#[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,
|
||||
@@ -94,63 +63,4 @@ 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(¶ms)
|
||||
.send()
|
||||
.await
|
||||
.map_err(DanceInfoError::Request)?;
|
||||
|
||||
let x: DanceInfoParser = response.json().await.map_err(DanceInfoError::JsonParse)?;
|
||||
Ok(x.into())
|
||||
}
|
||||
|
||||
14
src/main.rs
14
src/main.rs
@@ -22,8 +22,11 @@ use std::{error::Error, path::Path};
|
||||
use crate::watchdog::watch_wanted;
|
||||
|
||||
mod dance_info;
|
||||
mod updater;
|
||||
mod watchdog;
|
||||
mod wikiinfo;
|
||||
mod wikipage;
|
||||
mod worldsdc;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[allow(clippy::print_stdout, reason = "We want to print here")]
|
||||
@@ -38,7 +41,7 @@ fn list_teacher_pages(bot: &Bot) -> tokio::sync::mpsc::Receiver<Result<mwbot::Pa
|
||||
pages.generate(bot)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
fn main() {
|
||||
tracing_subscriber::fmt()
|
||||
.with_level(true)
|
||||
.with_max_level(tracing::Level::INFO)
|
||||
@@ -51,7 +54,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
Ok(o) => o,
|
||||
Err(e) => {
|
||||
tracing::error!("Could not start runtime: {e}");
|
||||
return Ok(());
|
||||
return;
|
||||
}
|
||||
};
|
||||
rt.block_on(async {
|
||||
@@ -59,10 +62,9 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
return Ok(());
|
||||
return;
|
||||
}
|
||||
};
|
||||
watch_wanted(bot).await;
|
||||
Ok(())
|
||||
})
|
||||
futures::join!(watch_wanted(bot.clone()), updater::update_wsdc(bot));
|
||||
});
|
||||
}
|
||||
|
||||
24
src/updater.rs
Normal file
24
src/updater.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use mwbot::Bot;
|
||||
use rand::seq::SliceRandom as _;
|
||||
|
||||
use crate::{watchdog::generate_page, wikiinfo::index_wsdc_ids};
|
||||
|
||||
pub async fn update_wsdc(bot: Bot) -> ! {
|
||||
loop {
|
||||
let mut l = index_wsdc_ids(&bot).await;
|
||||
l.shuffle(&mut rand::rng());
|
||||
tracing::info!("We have to update {} pages", l.len());
|
||||
let wait_duration = Duration::from_secs(6 * 3600);
|
||||
for (index, page) in l {
|
||||
tracing::info!("Next up: {index}");
|
||||
tokio::time::sleep(wait_duration).await;
|
||||
if generate_page(index, page).await {
|
||||
tracing::info!("Updated {index}");
|
||||
} else {
|
||||
tracing::error!("Error updating {index}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +1,11 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{
|
||||
dance_info::{DanceInfo, fetch_wsdc_info},
|
||||
wikiinfo::wanted_ids,
|
||||
};
|
||||
use mwbot::{
|
||||
Bot,
|
||||
parsoid::{self, Template, Wikicode, map::IndexMap},
|
||||
};
|
||||
use mwbot::{SaveOptions, parsoid::WikinodeIterator};
|
||||
use crate::{wikiinfo::wanted_ids, wikipage::page_from_info, worldsdc::fetch_wsdc_info};
|
||||
use mwbot::Bot;
|
||||
use mwbot::SaveOptions;
|
||||
use tracing::Level;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum InfoCompileError {
|
||||
#[error("Could not compile wikipage: {0}")]
|
||||
CompileError(#[from] parsoid::Error),
|
||||
}
|
||||
|
||||
fn page_from_info(info: DanceInfo) -> Result<Wikicode, InfoCompileError> {
|
||||
let mut params = IndexMap::new();
|
||||
params.insert("name".to_string(), info.name());
|
||||
params.insert(
|
||||
"dominant_role".to_string(),
|
||||
info.dominant_role.as_str().to_string(),
|
||||
);
|
||||
params.insert(
|
||||
"allowed_rank".to_string(),
|
||||
info.dominant_role_comp.rank.as_str().to_string(),
|
||||
);
|
||||
params.insert(
|
||||
"dominant_rank".to_string(),
|
||||
info.dominant_role_comp.rank.as_str().to_string(),
|
||||
);
|
||||
params.insert(
|
||||
"dominant_points".to_string(),
|
||||
info.dominant_role_comp.points.to_string(),
|
||||
);
|
||||
if let Some(u) = info.non_dominant_role_comp {
|
||||
params.insert("non_dominant_rank".to_string(), u.rank.as_str().to_string());
|
||||
params.insert("non_dominant_points".to_string(), u.points.to_string());
|
||||
}
|
||||
let t = Template::new("Template:WSDCBox", ¶ms)?;
|
||||
let result = Wikicode::new("");
|
||||
result.append(&t);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn watch_wanted(bot: Bot) {
|
||||
pub async fn watch_wanted(bot: Bot) -> ! {
|
||||
let span = tracing::span!(Level::INFO, "wanted_watchdog");
|
||||
let _enter = span.enter();
|
||||
|
||||
@@ -71,8 +30,8 @@ pub async fn watch_wanted(bot: Bot) {
|
||||
}
|
||||
}
|
||||
|
||||
async fn generate_page(id: u32, page: mwbot::Page) -> bool {
|
||||
tracing::info!("Taking care of {id}");
|
||||
pub async fn generate_page(id: u32, page: mwbot::Page) -> bool {
|
||||
tracing::info!("Generating page for {id}");
|
||||
let info = match fetch_wsdc_info(id).await {
|
||||
Ok(o) => o,
|
||||
Err(e) => {
|
||||
|
||||
@@ -42,8 +42,20 @@ fn parse_wsdc_page_name(name: &str) -> Result<u32, TitleParseError> {
|
||||
}
|
||||
}
|
||||
|
||||
// fn get_wsdc_page_date(bot: &Bot, page: &Page) -> Option<SystemTime> {
|
||||
// todo!();
|
||||
// let prefix = "Updated-On: ";
|
||||
// page.filter_comments()
|
||||
// .iter()
|
||||
// .filter_map(|x| {
|
||||
// let c = x.text_contents();
|
||||
// if c.starts_with(prefix) { Some(c) } else { None }
|
||||
// })
|
||||
// .map(|x| x.trim_start_matches(prefix).parse::<u64>());
|
||||
// }
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn index_wsdc_ids(bot: &Bot) -> Vec<u32> {
|
||||
pub async fn index_wsdc_ids(bot: &Bot) -> Vec<(u32, Page)> {
|
||||
let mut gene = Search::new("WSDC/").generate(bot);
|
||||
let mut result = vec![];
|
||||
while let Some(x) = gene.recv().await {
|
||||
@@ -55,7 +67,7 @@ pub async fn index_wsdc_ids(bot: &Bot) -> Vec<u32> {
|
||||
}
|
||||
};
|
||||
if let Ok(n) = parse_wsdc_page_name(p.title()) {
|
||||
result.push(n);
|
||||
result.push((n, p));
|
||||
}
|
||||
}
|
||||
result
|
||||
|
||||
38
src/wikipage.rs
Normal file
38
src/wikipage.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::dance_info::DanceInfo;
|
||||
use mwbot::parsoid::WikinodeIterator;
|
||||
use mwbot::parsoid::{self, Template, Wikicode, map::IndexMap};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum InfoCompileError {
|
||||
#[error("Could not compile wikipage: {0}")]
|
||||
CompileError(#[from] parsoid::Error),
|
||||
}
|
||||
|
||||
pub fn page_from_info(info: DanceInfo) -> Result<Wikicode, InfoCompileError> {
|
||||
let mut params = IndexMap::new();
|
||||
params.insert("name".to_string(), info.name());
|
||||
params.insert(
|
||||
"dominant_role".to_string(),
|
||||
info.dominant_role.as_str().to_string(),
|
||||
);
|
||||
params.insert(
|
||||
"allowed_rank".to_string(),
|
||||
info.dominant_role_comp.rank.as_str().to_string(),
|
||||
);
|
||||
params.insert(
|
||||
"dominant_rank".to_string(),
|
||||
info.dominant_role_comp.rank.as_str().to_string(),
|
||||
);
|
||||
params.insert(
|
||||
"dominant_points".to_string(),
|
||||
info.dominant_role_comp.points.to_string(),
|
||||
);
|
||||
if let Some(u) = info.non_dominant_role_comp {
|
||||
params.insert("non_dominant_rank".to_string(), u.rank.as_str().to_string());
|
||||
params.insert("non_dominant_points".to_string(), u.points.to_string());
|
||||
}
|
||||
let t = Template::new("Template:WSDCBox", ¶ms)?;
|
||||
let result = Wikicode::new("");
|
||||
result.append(&t);
|
||||
Ok(result)
|
||||
}
|
||||
48
src/worldsdc/caching.rs
Normal file
48
src/worldsdc/caching.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::{collections::HashMap, path::Path};
|
||||
|
||||
use reqwest::{Client, ClientBuilder};
|
||||
|
||||
use crate::{dance_info::DanceInfo, worldsdc::DanceInfoError};
|
||||
|
||||
use super::DanceInfoParser;
|
||||
|
||||
struct CachingFetcher {
|
||||
hitcache: Vec<(u32, String)>,
|
||||
errorcache: Vec<(u32, String)>,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum CachingFetcherCreationError {
|
||||
#[error("Could not create client: {0}")]
|
||||
ClientError(#[from] reqwest::Error),
|
||||
}
|
||||
|
||||
impl CachingFetcher {
|
||||
pub fn new(cachepath: &Path) -> Result<Self, CachingFetcherCreationError> {
|
||||
let client = ClientBuilder::new().build()?;
|
||||
|
||||
Ok(Self {
|
||||
hitcache: vec![],
|
||||
errorcache: vec![],
|
||||
client,
|
||||
})
|
||||
}
|
||||
pub async fn fetch(&mut self, id: u32) -> Result<DanceInfo, DanceInfoError> {
|
||||
let mut params = HashMap::new();
|
||||
params.insert("q", id.to_string());
|
||||
let response = self
|
||||
.client
|
||||
.request(
|
||||
reqwest::Method::POST,
|
||||
"https://points.worldsdc.com/lookup2020/find",
|
||||
)
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await
|
||||
.map_err(DanceInfoError::Request)?;
|
||||
|
||||
let x: DanceInfoParser = response.json().await.map_err(DanceInfoError::JsonParse)?;
|
||||
Ok(x.into())
|
||||
}
|
||||
}
|
||||
89
src/worldsdc/mod.rs
Normal file
89
src/worldsdc/mod.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use reqwest::ClientBuilder;
|
||||
|
||||
use crate::dance_info::{CompState, DanceInfo, DanceRank, DanceRole};
|
||||
|
||||
// mod caching;
|
||||
pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
||||
let client = 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(¶ms)
|
||||
.send()
|
||||
.await
|
||||
.map_err(DanceInfoError::Request)?;
|
||||
|
||||
let x: DanceInfoParser = response.json().await.map_err(DanceInfoError::JsonParse)?;
|
||||
Ok(x.into())
|
||||
}
|
||||
|
||||
#[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),
|
||||
}
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user