Skip to main content

nfl_model/models/
dfs_position.rs

1/// A daily fantasy sports roster position for an NFL player or team defense.
2///
3/// String parsing is ASCII case-insensitive and accepts `QB`, `RB`, `WR`, `TE`, and `DST`.
4/// `DEF` and `D` are also accepted as aliases for [`Dst`](Self::Dst). Conversion to [`String`]
5/// and SQLx storage use the uppercase position names, with `DST` for defense/special teams.
6///
7/// # Examples
8///
9/// ```
10/// use nfl_model::DfsPosition;
11///
12/// let position: DfsPosition = "def".parse()?;
13/// assert!(position.is_defense());
14/// assert_eq!(String::from(position), "DST");
15/// # Ok::<(), strum::ParseError>(())
16/// ```
17#[derive(
18    Debug, Clone, Copy, PartialEq, Eq, Hash, sqlx::Type, strum::IntoStaticStr, strum::EnumString,
19)]
20#[sqlx(rename_all = "UPPERCASE")]
21#[strum(serialize_all = "UPPERCASE", ascii_case_insensitive)]
22pub enum DfsPosition {
23    /// Quarterback (`QB`).
24    Qb,
25    /// Running back (`RB`).
26    Rb,
27    /// Wide receiver (`WR`).
28    Wr,
29    /// Tight end (`TE`).
30    Te,
31    /// Team defense and special teams (`DST`).
32    #[strum(
33        to_string = "DST",
34        serialize = "DST",
35        serialize = "DEF",
36        serialize = "D"
37    )]
38    Dst,
39}
40
41impl DfsPosition {
42    /// Return whether this position represents a team defense rather than an individual player.
43    pub fn is_defense(&self) -> bool {
44        matches!(self, DfsPosition::Dst)
45    }
46}
47
48impl From<DfsPosition> for String {
49    fn from(value: DfsPosition) -> Self {
50        let s: &str = value.into();
51        s.to_owned()
52    }
53}
54
55/// All DfsPositions + FLEX as an option
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
57pub enum DfsPositionSlotType {
58    Position(DfsPosition),
59    Flex,
60}
61
62impl DfsPositionSlotType {
63    pub fn allows(self, position: DfsPosition) -> bool {
64        match self {
65            Self::Position(slot) => slot == position,
66            Self::Flex => matches!(
67                position,
68                DfsPosition::Rb | DfsPosition::Wr | DfsPosition::Te
69            ),
70        }
71    }
72}
73
74impl From<DfsPosition> for DfsPositionSlotType {
75    fn from(position: DfsPosition) -> Self {
76        Self::Position(position)
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    #[test]
84    fn parses_positions_case_insensitively_and_accepts_defense_aliases() {
85        assert_eq!(DfsPosition::try_from("qb"), Ok(DfsPosition::Qb));
86        assert_eq!(DfsPosition::try_from("RB"), Ok(DfsPosition::Rb));
87        assert_eq!(DfsPosition::try_from("Wr"), Ok(DfsPosition::Wr));
88        assert_eq!(DfsPosition::try_from("TE"), Ok(DfsPosition::Te));
89        for alias in ["DST", "DEF", "D"] {
90            assert_eq!(DfsPosition::try_from(alias), Ok(DfsPosition::Dst));
91        }
92        assert!(DfsPosition::try_from("CPT").is_err());
93    }
94
95    #[test]
96    fn uses_dst_as_string() {
97        assert_eq!(String::from(DfsPosition::Dst), "DST".to_owned())
98    }
99}