Skip to main content

fanduel_data/models/
matchup.rs

1use crate::FanduelDataError;
2use nfl_model::TeamAbbr;
3use std::fmt::{Display, Formatter};
4use std::str::FromStr;
5
6/// The away and home teams in a game.
7///
8/// Parses from and displays as `AWAY@HOME` (for example, `BUF@NYJ`), using [`TeamAbbr`]
9/// for each team.
10#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
11pub struct Matchup {
12    /// Away team abbreviation.
13    pub away: TeamAbbr,
14    /// Home team abbreviation.
15    pub home: TeamAbbr,
16}
17
18impl FromStr for Matchup {
19    type Err = FanduelDataError;
20
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        let (away, home) = s
23            .split_once('@')
24            .ok_or(FanduelDataError::MatchupParseError(s.to_string()))?;
25        let home = home
26            .parse::<TeamAbbr>()
27            .map_err(|_| FanduelDataError::MatchupParseError(s.to_owned()))?;
28        let away = away
29            .parse::<TeamAbbr>()
30            .map_err(|_| FanduelDataError::MatchupParseError(s.to_owned()))?;
31        Ok(Matchup { away, home })
32    }
33}
34
35impl Display for Matchup {
36    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37        write!(f, "{}@{}", self.away, self.home)
38    }
39}