Worked on safer fetching
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user