Skip to main content

nfl_model/models/
team_abbr.rs

1use std::fmt::{Display, Formatter};
2
3/// An NFL team identified by its canonical abbreviation.
4///
5/// String parsing is ASCII case-insensitive. `JAC` is accepted as an alias for
6/// [`JAX`](Self::JAX), and `LAR` as an alias for [`LA`](Self::LA). Serde deserialization also
7/// accepts these uppercase aliases.
8///
9/// [`as_str`](Self::as_str), display formatting, Serde serialization, and SQLx storage use
10/// canonical uppercase abbreviations, including `JAX` for Jacksonville and `LA` for the Rams.
11///
12/// # Examples
13///
14/// ```
15/// use nfl_model::TeamAbbr;
16///
17/// let team: TeamAbbr = "lar".parse()?;
18/// assert_eq!(team, TeamAbbr::LA);
19/// assert_eq!(team.as_str(), "LA");
20/// assert_eq!(team.to_string(), "LA");
21/// # Ok::<(), strum::ParseError>(())
22/// ```
23#[derive(
24    Debug,
25    Clone,
26    Copy,
27    PartialEq,
28    Eq,
29    PartialOrd,
30    Ord,
31    Hash,
32    serde::Serialize,
33    serde::Deserialize,
34    sqlx::Type,
35    strum::AsRefStr,
36    strum::EnumString,
37)]
38#[allow(non_camel_case_types)]
39#[serde(rename_all = "UPPERCASE")]
40#[sqlx(type_name = "TEXT", rename_all = "UPPERCASE")]
41#[strum(serialize_all = "UPPERCASE", ascii_case_insensitive)]
42pub enum TeamAbbr {
43    /// Arizona Cardinals.
44    ARI,
45    /// Atlanta Falcons.
46    ATL,
47    /// Baltimore Ravens.
48    BAL,
49    /// Buffalo Bills.
50    BUF,
51    /// Carolina Panthers.
52    CAR,
53    /// Chicago Bears.
54    CHI,
55    /// Cincinnati Bengals.
56    CIN,
57    /// Cleveland Browns.
58    CLE,
59    /// Dallas Cowboys.
60    DAL,
61    /// Denver Broncos.
62    DEN,
63    /// Detroit Lions.
64    DET,
65    /// Green Bay Packers.
66    GB,
67    /// Houston Texans.
68    HOU,
69    /// Indianapolis Colts.
70    IND,
71    /// Jacksonville Jaguars; also accepts `JAC` when parsing.
72    #[serde(alias = "JAC")]
73    #[strum(serialize = "JAX", serialize = "JAC")]
74    JAX,
75    /// Kansas City Chiefs.
76    KC,
77    /// Los Angeles Chargers.
78    LAC,
79    /// Los Angeles Rams; also accepts `LAR` when parsing.
80    #[serde(alias = "LAR")]
81    #[strum(serialize = "LA", serialize = "LAR")]
82    LA,
83    /// Las Vegas Raiders.
84    LV,
85    /// Miami Dolphins.
86    MIA,
87    /// Minnesota Vikings.
88    MIN,
89    /// New England Patriots.
90    NE,
91    /// New Orleans Saints.
92    NO,
93    /// New York Giants.
94    NYG,
95    /// New York Jets.
96    NYJ,
97    /// Philadelphia Eagles.
98    PHI,
99    /// Pittsburgh Steelers.
100    PIT,
101    /// Seattle Seahawks.
102    SEA,
103    /// San Francisco 49ers.
104    SF,
105    /// Tampa Bay Buccaneers.
106    TB,
107    /// Tennessee Titans.
108    TEN,
109    /// Washington Commanders.
110    WAS,
111}
112
113impl TeamAbbr {
114    /// Return the canonical uppercase abbreviation, using `JAX` for Jacksonville and `LA`
115    /// for the Rams regardless of the alias used when parsing.
116    pub fn as_str(&self) -> &str {
117        match self {
118            Self::JAX => "JAX",
119            Self::LA => "LA",
120            _ => self.as_ref(),
121        }
122    }
123}
124
125impl Display for TeamAbbr {
126    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
127        f.write_str(self.as_str())
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::TeamAbbr;
134
135    #[test]
136    fn parses_legacy_abbreviations_to_canonical_variants() {
137        assert_eq!("JAC".parse(), Ok(TeamAbbr::JAX));
138        assert_eq!("LAR".parse(), Ok(TeamAbbr::LA));
139        assert_eq!(TeamAbbr::JAX.as_str(), "JAX");
140        assert_eq!(TeamAbbr::LA.as_str(), "LA");
141    }
142}