scriptly typed team format

This commit is contained in:
Robin Appelman 2025-04-14 15:34:21 +02:00
commit 98b690356c
12 changed files with 86 additions and 25 deletions

27
types/Cargo.lock generated
View file

@ -229,7 +229,7 @@ dependencies = [
"regex",
"serde",
"serde_derive",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@ -249,7 +249,16 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl",
"thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
]
[[package]]
@ -263,6 +272,17 @@ dependencies = [
"syn",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "time"
version = "0.3.41"
@ -296,10 +316,11 @@ dependencies = [
[[package]]
name = "ugc-scraper-types"
version = "0.1.2"
version = "0.2.0"
dependencies = [
"serde",
"steamid-ng",
"thiserror 2.0.12",
"time",
]

View file

@ -1,6 +1,6 @@
[package]
name = "ugc-scraper-types"
version = "0.1.2"
version = "0.2.0"
edition = "2021"
rust-version = "1.71.1"
description = "Scraper for ugcleague.com - data types"
@ -11,6 +11,7 @@ homepage = "https://github.com/icewind1991/ugc-scaper"
steamid-ng = "1.0.0"
serde = { version = "1.0.215", features = ["derive"], optional = true }
time = { version = "0.3.36", features = ["parsing", "macros"] }
thiserror = "2.0.12"
[features]
serde = ["dep:serde", "time/serde", "time/formatting"]

View file

@ -1,6 +1,9 @@
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::Display;
use std::str::FromStr;
pub use steamid_ng::SteamID;
use thiserror::Error;
use time::{Date, OffsetDateTime};
#[cfg(feature = "serde")]
@ -111,7 +114,7 @@ pub struct Team {
pub name: String,
pub tag: String,
pub image: String,
pub format: String,
pub format: GameMode,
pub timezone: Option<String>,
pub steam_group: Option<String>,
pub division: String,
@ -267,6 +270,12 @@ pub struct MatchInfo {
pub score_away: u8,
}
#[derive(Debug, Clone, Error)]
#[error("Invalid game mode {text}")]
pub struct InvalidGameMode {
pub text: String,
}
#[derive(Debug, Clone, Copy)]
pub enum GameMode {
Highlander,
@ -276,7 +285,7 @@ pub enum GameMode {
}
impl FromStr for GameMode {
type Err = ();
type Err = InvalidGameMode;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
@ -284,7 +293,14 @@ impl FromStr for GameMode {
"6v6" => Ok(GameMode::Sixes),
"4v4" => Ok(GameMode::Fours),
"2v2" => Ok(GameMode::Ultiduo),
_ => Err(()),
"TF2 Highlander" => Ok(GameMode::Highlander),
"ASIA TF2-H" => Ok(GameMode::Highlander),
"TF2 6vs6" => Ok(GameMode::Sixes),
"TF2 4vs4" => Ok(GameMode::Fours),
"TF2 2vs2" => Ok(GameMode::Ultiduo),
_ => Err(InvalidGameMode {
text: s.to_string(),
}),
}
}
}
@ -309,6 +325,25 @@ impl GameMode {
}
}
impl Serialize for GameMode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.as_str().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for GameMode {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = <&str>::deserialize(deserializer)?;
Self::from_str(s).map_err(D::Error::custom)
}
}
impl Display for GameMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())