mirror of
https://codeberg.org/demostf/api-client.git
synced 2026-06-03 08:34:15 +02:00
incompatible updates for 0.5
This commit is contained in:
parent
216ae4540e
commit
d2fee4d3eb
4 changed files with 413 additions and 953 deletions
74
src/lib.rs
74
src/lib.rs
|
|
@ -3,11 +3,13 @@ pub use client::ApiClient;
|
|||
use futures_util::{Stream, StreamExt};
|
||||
use md5::Context;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use std::borrow::Cow;
|
||||
use std::fmt::{self, Debug, Display, Formatter};
|
||||
use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
pub use steamid_ng::SteamID;
|
||||
use steamid_ng::{Instance, InstanceFlags, InstanceType};
|
||||
use thiserror::Error;
|
||||
use time::OffsetDateTime;
|
||||
use tinyvec::TinyVec;
|
||||
|
|
@ -180,7 +182,7 @@ impl UserRef {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct User {
|
||||
pub id: u32,
|
||||
#[serde(rename = "steamid")]
|
||||
#[serde(rename = "steamid", deserialize_with = "deserialize_steamid")]
|
||||
pub steam_id: SteamID,
|
||||
pub name: String,
|
||||
}
|
||||
|
|
@ -204,7 +206,7 @@ pub struct Player {
|
|||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct NestedPlayerUser {
|
||||
user_id: u32,
|
||||
#[serde(rename = "steamid")]
|
||||
#[serde(rename = "steamid", deserialize_with = "deserialize_steamid")]
|
||||
steam_id: SteamID,
|
||||
name: String,
|
||||
}
|
||||
|
|
@ -221,6 +223,14 @@ where
|
|||
})
|
||||
}
|
||||
|
||||
fn deserialize_steamid<'de, D>(deserializer: D) -> Result<SteamID, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = <Cow<'static, str>>::deserialize(deserializer)?;
|
||||
SteamID::from_str(&s).map_err(D::Error::custom)
|
||||
}
|
||||
|
||||
/// Player team, red or blue
|
||||
#[derive(Clone, Copy, Debug, Deserialize, PartialOrd, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
|
|
@ -333,24 +343,30 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct PlayerList(TinyVec<[SteamID; 2]>);
|
||||
#[derive(Debug, Default)]
|
||||
struct PlayerList(TinyVec<[Option<SteamID>; 2]>);
|
||||
|
||||
impl PlayerList {
|
||||
fn new<T: Into<SteamID>, I: IntoIterator<Item = T>>(players: I) -> Self {
|
||||
PlayerList(players.into_iter().map(Into::into).collect())
|
||||
fn new<T: IntoSteamId, I: IntoIterator<Item = T>>(players: I) -> Self {
|
||||
PlayerList(
|
||||
players
|
||||
.into_iter()
|
||||
.map(IntoSteamId::into_steam_id)
|
||||
.map(Some)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PlayerList {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
let mut first = true;
|
||||
for steam_id in &self.0 {
|
||||
for steam_id in self.0.iter().flatten() {
|
||||
if first {
|
||||
first = false;
|
||||
write!(f, "{}", u64::from(*steam_id))?;
|
||||
write!(f, "{}", steam_id.steam64())?;
|
||||
} else {
|
||||
write!(f, ",{}", u64::from(*steam_id))?;
|
||||
write!(f, ",{}", steam_id.steam64())?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -369,20 +385,50 @@ impl Serialize for PlayerList {
|
|||
|
||||
#[test]
|
||||
fn test_serialize_player_list() {
|
||||
fn id(id: u64) -> SteamID {
|
||||
SteamID::from_steam64(id).unwrap()
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
"76561198024494988",
|
||||
PlayerList::new([76561198024494988]).to_string()
|
||||
PlayerList::new([id(76561198024494988)]).to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"76561198024494988,76561197963701107",
|
||||
PlayerList::new([76561198024494988, 76561197963701107]).to_string()
|
||||
PlayerList::new([id(76561198024494988), id(76561197963701107)]).to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"76561198024494988,76561197963701107,76561197963701106",
|
||||
PlayerList::new([76561198024494988, 76561197963701107, 76561197963701106]).to_string()
|
||||
PlayerList::new([
|
||||
id(76561198024494988),
|
||||
id(76561197963701107),
|
||||
id(76561197963701106)
|
||||
])
|
||||
.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
pub trait IntoSteamId {
|
||||
fn into_steam_id(self) -> SteamID;
|
||||
}
|
||||
|
||||
impl IntoSteamId for SteamID {
|
||||
fn into_steam_id(self) -> SteamID {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoSteamId for u64 {
|
||||
fn into_steam_id(self) -> SteamID {
|
||||
SteamID::from_steam64(self).unwrap_or(SteamID::new(
|
||||
0,
|
||||
Instance::new(InstanceType::All, InstanceFlags::None),
|
||||
steamid_ng::AccountType::Invalid,
|
||||
steamid_ng::Universe::Invalid,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl ListParams {
|
||||
/// Specify the backend name to filter demos with
|
||||
#[must_use]
|
||||
|
|
@ -404,7 +450,7 @@ impl ListParams {
|
|||
|
||||
/// Specify the player steam ids to filter demos with
|
||||
#[must_use]
|
||||
pub fn with_players<T: Into<SteamID>, I: IntoIterator<Item = T>>(self, players: I) -> Self {
|
||||
pub fn with_players<T: IntoSteamId, I: IntoIterator<Item = T>>(self, players: I) -> Self {
|
||||
ListParams {
|
||||
players: PlayerList::new(players),
|
||||
..self
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue