subject postprocessing

This commit is contained in:
Robin Appelman 2021-08-07 19:28:58 +02:00
commit 747ee7d1a9
10 changed files with 2970 additions and 22 deletions

View file

@ -1,12 +1,31 @@
use crate::raw_event::RawSubject;
use std::convert::TryFrom;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use steamid_ng::SteamID;
use thiserror::Error;
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Ord, PartialOrd)]
pub enum Team {
Red,
Blue,
}
impl Team {
pub fn as_str(self) -> &'static str {
match self {
Team::Red => "Red",
Team::Blue => "Blue",
}
}
}
impl Display for Team {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl FromStr for Team {
type Err = ();
@ -20,7 +39,7 @@ impl FromStr for Team {
}
/// Optimized subject id
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Ord, PartialOrd)]
pub enum SubjectId {
Player(u8),
Team(Team),
@ -40,3 +59,52 @@ impl From<&RawSubject<'_>> for SubjectId {
}
}
}
#[derive(Debug, PartialEq)]
pub enum SubjectData {
Player {
name: String,
user_id: u8,
steam_id: SteamID,
team: Team,
},
Team(Team),
System(String),
Console,
World,
}
#[derive(Debug, Error)]
pub enum SubjectError {
#[error("Invalid user id")]
InvalidUserId,
#[error("Invalid steam id")]
InvalidSteamId,
#[error("Invalid team name")]
InvalidTeam,
}
impl TryFrom<&RawSubject<'_>> for SubjectData {
type Error = SubjectError;
fn try_from(raw: &RawSubject<'_>) -> Result<Self, Self::Error> {
Ok(match raw {
RawSubject::Player {
name,
user_id,
steam_id,
team,
} => SubjectData::Player {
name: name.to_string(),
user_id: user_id.parse().map_err(|_| SubjectError::InvalidUserId)?,
steam_id: SteamID::from_steam3(steam_id)
.map_err(|_| SubjectError::InvalidSteamId)?,
team: team.parse().map_err(|_| SubjectError::InvalidTeam)?,
},
RawSubject::Team(team) => SubjectData::Team(team.parse().unwrap()),
RawSubject::System(name) => SubjectData::System(name.to_string()),
RawSubject::Console => SubjectData::Console,
RawSubject::World => SubjectData::World,
})
}
}